summaryrefslogtreecommitdiff
path: root/src/bin/br.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/br.rs')
-rw-r--r--src/bin/br.rs86
1 files changed, 28 insertions, 58 deletions
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);