diff options
author | Ben Bridle <ben@derelict.engineering> | 2025-02-03 09:51:32 +1300 |
---|---|---|
committer | Ben Bridle <ben@derelict.engineering> | 2025-02-03 09:51:32 +1300 |
commit | a2408cd14317e1c0969953f8745034d78b2b9bcf (patch) | |
tree | af1224a20b8d4d62790c9a6935986b84f1bb87d7 | |
parent | 0070a300bc281d15a695709b7dd2e1cb9017913a (diff) | |
download | bedrock-pc-a2408cd14317e1c0969953f8745034d78b2b9bcf.zip |
Use log crate for printing info and error messages
-rw-r--r-- | Cargo.lock | 24 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/bin/br.rs | 86 | ||||
-rw-r--r-- | src/debug.rs | 11 | ||||
-rw-r--r-- | src/devices/file_device/buffered_file.rs | 4 | ||||
-rw-r--r-- | src/emulators/graphical_emulator.rs | 8 | ||||
-rw-r--r-- | src/emulators/headless_emulator.rs | 6 | ||||
-rw-r--r-- | src/lib.rs | 7 |
8 files changed, 53 insertions, 94 deletions
@@ -28,7 +28,7 @@ dependencies = [ "jni", "jni-sys", "libc", - "log", + "log 0.4.20", "ndk", "ndk-context", "ndk-sys", @@ -97,6 +97,7 @@ dependencies = [ "bedrock-core", "chrono", "geometry", + "log 1.1.1", "phosphor", "windows", "xflags", @@ -171,7 +172,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b99da2f8558ca23c71f4fd15dc57c906239752dd27ff3c00a1d56b685b7cbfec" dependencies = [ "bitflags 2.6.0", - "log", + "log 0.4.20", "polling", "rustix", "slab", @@ -556,7 +557,7 @@ dependencies = [ "cfg-if", "combine", "jni-sys", - "log", + "log 0.4.20", "thiserror", "walkdir", "windows-sys 0.45.0", @@ -632,6 +633,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" [[package]] +name = "log" +version = "1.1.1" +source = "git+git://benbridle.com/log?tag=v1.1.1#930f3d0e2b82df1243f423c092a38546ea7533c3" + +[[package]] name = "memchr" version = "2.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -654,7 +660,7 @@ checksum = "c3f42e7bbe13d351b6bead8286a43aac9534b82bd3cc43e47037f012ebfd62d4" dependencies = [ "bitflags 2.6.0", "jni-sys", - "log", + "log 0.4.20", "ndk-sys", "num_enum", "raw-window-handle", @@ -1129,7 +1135,7 @@ dependencies = [ "calloop-wayland-source", "cursor-icon", "libc", - "log", + "log 0.4.20", "memmap2", "rustix", "thiserror", @@ -1166,7 +1172,7 @@ dependencies = [ "fastrand", "foreign-types", "js-sys", - "log", + "log 0.4.20", "memmap2", "objc2", "objc2-foundation", @@ -1316,7 +1322,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826" dependencies = [ "bumpalo", - "log", + "log 0.4.20", "once_cell", "proc-macro2", "quote", @@ -1469,7 +1475,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "efa8ac0d8e8ed3e3b5c9fc92c7881406a268e11555abe36493efabe649a29e09" dependencies = [ "dlib", - "log", + "log 0.4.20", "once_cell", "pkg-config", ] @@ -1911,7 +1917,7 @@ checksum = "d039de8032a9a8856a6be89cea3e5d12fdd82306ab7c94d74e6deab2460651c5" dependencies = [ "bitflags 2.6.0", "dlib", - "log", + "log 0.4.20", "once_cell", "xkeysym", ] @@ -10,6 +10,7 @@ bedrock-asm = { git = "git://benbridle.com/bedrock-asm", tag = "v4.0.6" } bedrock-core = { git = "git://benbridle.com/bedrock-core", tag = "v5.0.0" } phosphor = { git = "git://benbridle.com/phosphor", tag = "v3.2.2" } geometry = { git = "git://benbridle.com/geometry", tag = "v1.0.0" } +log = { git = "git://benbridle.com/log", tag = "v1.1.1" } chrono = { version = "0.4.38" } xflags = "0.4.0-pre.1" diff --git a/src/bin/br.rs b/src/bin/br.rs index 8eb4135..8218286 100644 --- a/src/bin/br.rs +++ b/src/bin/br.rs @@ -6,28 +6,7 @@ use std::io::{Read, Write}; use std::path::{Path, PathBuf}; use std::process::exit; - -const NORMAL: &str = "\x1b[0m"; -const BOLD: &str = "\x1b[1m"; -const WHITE: &str = "\x1b[37m"; -const RED: &str = "\x1b[31m"; -const BLUE: &str = "\x1b[34m"; - -static mut VERBOSE: bool = false; - -macro_rules! verbose { - ($($tokens:tt)*) => { if unsafe { VERBOSE } { - eprint!("{BOLD}{BLUE}[INFO]{NORMAL}: "); eprint!($($tokens)*); - eprintln!("{NORMAL}"); - } }; -} -macro_rules! error { - ($($tokens:tt)*) => {{ - eprint!("{BOLD}{RED}[ERROR]{WHITE}: "); eprint!($($tokens)*); - eprintln!("{NORMAL}"); - }}; -} - +use log::{info, fatal}; fn main() { let args = Arguments::from_env_or_exit(); @@ -39,7 +18,7 @@ fn main() { std::process::exit(0); } if args.verbose { - unsafe { VERBOSE = true; } + log::set_log_level(log::LogLevel::Info); } match args.subcommand { ArgumentsCmd::Run(run) => main_run(run), @@ -58,7 +37,7 @@ fn main_run(args: Run) { let metadata = parse_metadata(&bytecode); if metadata.is_none() { - verbose!("Could not read program metadata"); + info!("Could not read program metadata"); } let mut config = EmulatorConfig { @@ -75,7 +54,7 @@ fn main_run(args: Run) { let phosphor = Phosphor::new(); if phosphor.is_ok() && args.dimensions().is_some() { - verbose!("Starting graphical emulator"); + info!("Starting graphical emulator"); let mut phosphor = phosphor.unwrap(); config.dimensions = args.dimensions().unwrap(); let cursor = match config.show_cursor { @@ -83,7 +62,7 @@ fn main_run(args: Run) { false => None, }; - let mut graphical = GraphicalEmulator::new(&config, args.debug, unsafe {VERBOSE}); + let mut graphical = GraphicalEmulator::new(&config, args.debug); graphical.load_program(&bytecode); if let EmulatorSignal::Promote = graphical.run() { let program_name = match &metadata { @@ -108,8 +87,8 @@ fn main_run(args: Run) { phosphor.run().unwrap(); } } else { - verbose!("Starting headless emulator"); - let mut headless = HeadlessEmulator::new(&config, args.debug, unsafe {VERBOSE}); + info!("Starting headless emulator"); + let mut headless = HeadlessEmulator::new(&config, args.debug); headless.load_program(&bytecode); headless.run(args.debug); }; @@ -123,26 +102,24 @@ fn load_bytecode(path: Option<&Path>) -> Bytecode { if let Ok(bytecode) = load_bytecode_from_file(path) { let length = bytecode.bytes.len(); let path = bytecode.path(); - verbose!("Loaded program from {path:?} ({length} bytes)"); + info!("Loaded program from {path:?} ({length} bytes)"); return bytecode; } else if let Some(bytecode) = load_bytecode_from_bedrock_path(path) { let length = bytecode.bytes.len(); let path = bytecode.path(); - verbose!("Loaded program from {path:?} ({length} bytes)"); + info!("Loaded program from {path:?} ({length} bytes)"); return bytecode; } else { - error!("Could not read program from {path:?}, exiting"); - exit(1); + fatal!("Could not read program from {path:?}"); } } else { - verbose!("Reading program from standard input..."); + info!("Reading program from standard input..."); if let Ok(bytecode) = load_bytecode_from_stdin() { let length = bytecode.bytes.len(); - verbose!("Loaded program from standard input ({length} bytes)"); + info!("Loaded program from standard input ({length} bytes)"); return bytecode; } else { - error!("Could not read program from standard input, exiting"); - exit(1); + fatal!("Could not read program from standard input"); } } } @@ -154,13 +131,13 @@ fn load_bytecode_from_bedrock_path(path: &Path) -> Option<Bytecode> { let mut base_path = PathBuf::from(base_path); if !base_path.is_absolute() { continue; } base_path.push(path); - verbose!("Attempting to load program from {base_path:?}"); + info!("Attempting to load program from {base_path:?}"); if let Ok(bytecode) = load_bytecode_from_file(&base_path) { return Some(bytecode); } if path.extension().is_some() { continue; } base_path.set_extension("br"); - verbose!("Attempting to load program from {base_path:?}"); + info!("Attempting to load program from {base_path:?}"); if let Ok(bytecode) = load_bytecode_from_file(&base_path) { return Some(bytecode); } @@ -220,29 +197,26 @@ fn main_asm(args: Asm) { Ok(source_unit) => SymbolResolver::from_source_unit(source_unit), Err(err) => { match err { - ParseError::InvalidExtension => error!( + ParseError::InvalidExtension => fatal!( "File {path:?} has invalid extension, must be '.{ext}'"), - ParseError::NotFound => error!( + ParseError::NotFound => fatal!( "File {path:?} was not found"), - ParseError::InvalidUtf8 => error!( + ParseError::InvalidUtf8 => fatal!( "File {path:?} does not contain valid UTF-8 text"), - ParseError::NotReadable => error!( + ParseError::NotReadable => fatal!( "File {path:?} is not readable"), - ParseError::IsADirectory => error!( + ParseError::IsADirectory => fatal!( "File {path:?} is a directory"), - ParseError::Unknown => error!( + ParseError::Unknown => fatal!( "Unknown error while attempting to read from {path:?}") }; - exit(1); } } } else { let mut source_code = String::new(); - verbose!("Reading program source from standard input"); + info!("Reading program source from standard input"); if let Err(err) = std::io::stdin().read_to_string(&mut source_code) { - error!("Could not read from standard input"); - eprintln!("{err:?}"); - exit(1); + fatal!("Could not read from standard input\n{err:?}"); } let path = "<standard input>"; let source_unit = SourceUnit::from_source_code(source_code, path); @@ -300,17 +274,17 @@ fn main_asm(args: Asm) { symbols_path.set_extension("br.sym"); let symbols = generate_symbols_file(&semantic_tokens); if let Err(err) = std::fs::write(&symbols_path, symbols) { - verbose!("Could not write to symbols path {symbols_path:?}"); + info!("Could not write to symbols path {symbols_path:?}"); eprintln!("{err:?}"); } else { - verbose!("Saved debug symbols to {symbols_path:?}"); + info!("Saved debug symbols to {symbols_path:?}"); } } } let length = bytecode.len(); let percentage = (length as f32 / 65536.0 * 100.0).round() as u16; - verbose!("Assembled program in {length} bytes ({percentage}% of maximum)"); + info!("Assembled program in {length} bytes ({percentage}% of maximum)"); if !args.check { write_bytes_and_exit(&bytecode, args.output.as_ref()); @@ -320,15 +294,11 @@ fn main_asm(args: Asm) { fn write_bytes_and_exit<P: AsRef<Path>>(bytes: &[u8], path: Option<&P>) -> ! { if let Some(path) = path { if let Err(err) = std::fs::write(path, bytes) { - error!("Could not write to path {:?}", path.as_ref()); - eprintln!("{err:?}"); - exit(1); + fatal!("Could not write to path {:?}\n{err:?}", path.as_ref()); } } else { if let Err(err) = std::io::stdout().write_all(bytes) { - error!("Could not write to standard output"); - eprintln!("{err:?}"); - exit(1); + fatal!("Could not write to standard output\n{err:?}"); } } exit(0); diff --git a/src/debug.rs b/src/debug.rs index 6270948..7fd4ea5 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -5,7 +5,6 @@ use std::time::Instant; const NORMAL: &str = "\x1b[0m"; -const BOLD: &str = "\x1b[1m"; const DIM: &str = "\x1b[2m"; const YELLOW: &str = "\x1b[33m"; const BLUE: &str = "\x1b[34m"; @@ -13,29 +12,21 @@ const BLUE: &str = "\x1b[34m"; pub struct DebugState { pub enabled: bool, - pub verbose: bool, last_cycle: usize, last_mark: Instant, symbols: DebugSymbols, } impl DebugState { - pub fn new<P: AsRef<Path>>(enabled: bool, verbose: bool, symbols_path: Option<P>) -> Self { + pub fn new<P: AsRef<Path>>(enabled: bool, symbols_path: Option<P>) -> Self { Self { enabled, - verbose, last_cycle: 0, last_mark: Instant::now(), symbols: DebugSymbols::from_path_opt(symbols_path), } } - pub fn info(&self, string: &str) { - if self.verbose { - eprintln!("{BOLD}{BLUE}[INFO]{NORMAL}: {string}{NORMAL}"); - } - } - pub fn debug_summary(&mut self, core: &BedrockCore) { if self.enabled { let prev_pc = core.mem.pc.wrapping_sub(1); diff --git a/src/devices/file_device/buffered_file.rs b/src/devices/file_device/buffered_file.rs index f965950..29e1fa3 100644 --- a/src/devices/file_device/buffered_file.rs +++ b/src/devices/file_device/buffered_file.rs @@ -3,8 +3,6 @@ use std::io::{BufReader, BufWriter}; use std::io::{Read, Write}; use std::io::{ErrorKind, Seek, SeekFrom}; -use crate::*; - pub struct BufferedFile { file: AccessMode, @@ -42,7 +40,7 @@ impl BufferedFile { Ok(_) => buffer[0], Err(error) => match error.kind() { ErrorKind::UnexpectedEof => 0, - _ => { error!("BufferedFile::read", "{error:?}"); 0 }, + _ => { log::error!("BufferedFile::read: {error:?}"); 0 }, } } } diff --git a/src/emulators/graphical_emulator.rs b/src/emulators/graphical_emulator.rs index 14848c6..1c58a34 100644 --- a/src/emulators/graphical_emulator.rs +++ b/src/emulators/graphical_emulator.rs @@ -111,11 +111,11 @@ pub struct GraphicalEmulator { } impl GraphicalEmulator { - pub fn new(config: &EmulatorConfig, debug: bool, verbose: bool) -> Self { + pub fn new(config: &EmulatorConfig, debug: bool) -> Self { let devices = GraphicalDeviceBus::new(config); Self { br: BedrockEmulator::new(devices), - debug: DebugState::new(debug, verbose, config.symbols_path.as_ref()), + debug: DebugState::new(debug, config.symbols_path.as_ref()), dimensions: config.dimensions, fullscreen: config.fullscreen, scale: config.scale, @@ -147,7 +147,7 @@ impl GraphicalEmulator { } Some(Signal::Halt) => { self.br.dev.loc.flush(); - self.debug.info("Program halted, exiting."); + log::info!("Program halted, exiting."); self.debug.debug_summary(&self.br.core); return EmulatorSignal::Halt; } @@ -286,7 +286,7 @@ impl WindowProgram for GraphicalEmulator { } Some(Signal::Halt) => { self.br.dev.loc.flush(); - self.debug.info("Program halted, exiting."); + log::info!("Program halted, exiting."); self.debug.debug_summary(&self.br.core); requests.write(Request::CloseWindow); break; diff --git a/src/emulators/headless_emulator.rs b/src/emulators/headless_emulator.rs index f215db3..9207b3d 100644 --- a/src/emulators/headless_emulator.rs +++ b/src/emulators/headless_emulator.rs @@ -86,10 +86,10 @@ pub struct HeadlessEmulator { } impl HeadlessEmulator { - pub fn new(config: &EmulatorConfig, debug: bool, verbose: bool) -> Self { + pub fn new(config: &EmulatorConfig, debug: bool) -> Self { Self { br: BedrockEmulator::new(HeadlessDeviceBus::new(config)), - debug: DebugState::new(debug, verbose, config.symbols_path.as_ref()), + debug: DebugState::new(debug, config.symbols_path.as_ref()), } } @@ -111,7 +111,7 @@ impl HeadlessEmulator { } Some(Signal::Halt) => { self.br.dev.loc.flush(); - self.debug.info("Program halted, exiting."); + log::info!("Program halted, exiting."); self.debug.debug_summary(&self.br.core); return EmulatorSignal::Halt; } @@ -25,10 +25,3 @@ pub const DEFAULT_SCREEN_SCALE: NonZeroU32 = unsafe { NonZeroU32::new_unchecked( pub type ScreenPosition = geometry::Point<u16>; pub type ScreenDimensions = geometry::Dimensions<u16>; -#[macro_export] -macro_rules! error { - ($source:expr, $($tokens:tt)*) => {{ - eprint!("[ERROR] [{}]: ", $source); - eprintln!($($tokens)*); - }}; -} |