diff --git a/Cargo.lock b/Cargo.lock index 854127fd..df04ae3b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -126,6 +126,7 @@ dependencies = [ "hashbrown", "hbbytecode", "hbvm", + "log", "regalloc2", ] diff --git a/hblang/Cargo.toml b/hblang/Cargo.toml index c0b996d0..01c24f80 100644 --- a/hblang/Cargo.toml +++ b/hblang/Cargo.toml @@ -11,6 +11,7 @@ path = "src/main.rs" hashbrown = { version = "0.14.5", default-features = false } hbbytecode = { version = "0.1.0", path = "../hbbytecode" } hbvm = { path = "../hbvm", features = ["nightly"] } +log = { version = "0.4.22", features = ["release_max_level_error"] } regalloc2 = { git = "https://github.com/jakubDoka/regalloc2", branch = "reuse-allocations", features = [] } [dev-dependencies] diff --git a/hblang/src/codegen.rs b/hblang/src/codegen.rs index 4ea71255..f022f6da 100644 --- a/hblang/src/codegen.rs +++ b/hblang/src/codegen.rs @@ -4,7 +4,6 @@ use { ident::{self, Ident}, instrs::{self, *}, lexer::TokenKind, - log, parser::{ self, find_symbol, idfl, CommentOr, CtorField, Expr, ExprRef, FileId, Pos, StructField, }, @@ -1305,7 +1304,7 @@ impl Codegen { self.assign_pattern(left, value) } E::Call { func: fast, args, .. } => { - log::trc!("call {}", self.ast_display(fast)); + log::trace!("call {}", self.ast_display(fast)); let func_ty = self.ty(fast); let ty::Kind::Func(mut func) = func_ty.expand() else { self.report(fast.pos(), "can't call this, maybe in the future"); @@ -2408,7 +2407,7 @@ impl Codegen { name: Result, lit_name: &str, ) -> ty::Kind { - log::trc!("find_or_declare: {lit_name} {file}"); + log::trace!("find_or_declare: {lit_name} {file}"); let f = self.files[file as usize].clone(); let Some((expr, ident)) = f.find_decl(name) else { match name { @@ -2539,7 +2538,7 @@ impl Codegen { ci: ItemCtx, compile: impl FnOnce(&mut Self, &mut ItemCtx) -> Result, ) -> Result { - log::trc!("eval"); + log::trace!("eval"); let mut prev_ci = core::mem::replace(&mut self.ci, ci); self.ci.task_base = self.tasks.len(); @@ -2584,7 +2583,7 @@ impl Codegen { }) { panic!("{e} {}", vc); } else { - log::trc!("{}", vc); + log::trace!("{}", vc); } } @@ -2596,7 +2595,7 @@ impl Codegen { self.pool.cis.push(core::mem::replace(&mut self.ci, prev_ci)); - log::trc!("eval-end"); + log::trace!("eval-end"); ret } @@ -2650,7 +2649,7 @@ impl Codegen { fn report_log(&self, pos: Pos, msg: impl core::fmt::Display) { let mut out = String::new(); self.cfile().report_to(pos, msg, &mut out); - crate::log::eprintln!("{out}"); + log::error!("{out}"); } #[track_caller] diff --git a/hblang/src/fs.rs b/hblang/src/fs.rs index 7f2f48e6..0bc39012 100644 --- a/hblang/src/fs.rs +++ b/hblang/src/fs.rs @@ -8,6 +8,7 @@ use { hashbrown::hash_map, std::{ collections::VecDeque, + eprintln, ffi::OsStr, io, path::{Path, PathBuf}, @@ -16,6 +17,20 @@ use { }, }; +pub struct Logger; + +impl log::Log for Logger { + fn enabled(&self, metadata: &log::Metadata) -> bool { + log::max_level() >= metadata.level() + } + + fn log(&self, record: &log::Record) { + eprintln!("{}", record.args()) + } + + fn flush(&self) {} +} + #[derive(Default)] pub struct Options { pub fmt: bool, diff --git a/hblang/src/lib.rs b/hblang/src/lib.rs index e3c18dd3..12482f2d 100644 --- a/hblang/src/lib.rs +++ b/hblang/src/lib.rs @@ -116,74 +116,6 @@ mod ident { } } -mod log { - #![allow(unused_macros)] - - #[derive(PartialOrd, PartialEq, Ord, Eq, Debug)] - pub enum Level { - Err, - Wrn, - Inf, - Dbg, - Trc, - } - - pub const LOG_LEVEL: Level = match option_env!("LOG_LEVEL") { - Some(val) => match val.as_bytes()[0] { - b'e' => Level::Err, - b'w' => Level::Wrn, - b'i' => Level::Inf, - b'd' => Level::Dbg, - b't' => Level::Trc, - _ => panic!("Invalid log level."), - }, - None => { - if cfg!(debug_assertions) { - Level::Dbg - } else { - Level::Err - } - } - }; - - macro_rules! eprintln { - ($($tt:tt)*) => { - #[cfg(test)] - { - //std::eprintln!($($tt)*) - format_args!($($tt)*) - } - #[cfg(not(test))] - { - format_args!($($tt)*) - } - }; - } - - macro_rules! log { - ($level:expr, $fmt:literal $($expr:tt)*) => { - if $level <= $crate::log::LOG_LEVEL { - $crate::log::eprintln!("{:?}: {}", $level, format_args!($fmt $($expr)*)); - } - }; - - ($level:expr, $($arg:expr),+) => { - if $level <= $crate::log::LOG_LEVEL { - $(eprintln!("[{}:{}:{}][{:?}]: {} = {:?}", line!(), column!(), file!(), $level, stringify!($arg), $arg);)* - } - }; - } - - macro_rules! err { ($($arg:tt)*) => { $crate::log::log!($crate::log::Level::Err, $($arg)*) }; } - macro_rules! wrn { ($($arg:tt)*) => { $crate::log::log!($crate::log::Level::Wrn, $($arg)*) }; } - macro_rules! inf { ($($arg:tt)*) => { $crate::log::log!($crate::log::Level::Inf, $($arg)*) }; } - macro_rules! dbg { ($($arg:tt)*) => { $crate::log::log!($crate::log::Level::Dbg, $($arg)*) }; } - macro_rules! trc { ($($arg:tt)*) => { $crate::log::log!($crate::log::Level::Trc, $($arg)*) }; } - - #[allow(unused_imports)] - pub(crate) use {dbg, eprintln, err, inf, log, trc, wrn}; -} - mod ty { use { crate::{ @@ -1318,7 +1250,7 @@ impl LoggedMem { write!(self.disp_buf, "({})", regs[r as usize].0).unwrap() } } - log::trc!("read-typed: {:x}: {}", addr.get(), self.disp_buf); + log::trace!("read-typed: {:x}: {}", addr.get(), self.disp_buf); } } @@ -1329,7 +1261,7 @@ impl hbvm::mem::Memory for LoggedMem { target: *mut u8, count: usize, ) -> Result<(), hbvm::mem::LoadError> { - log::trc!( + log::trace!( "load: {:x} {}", addr.get(), AsHex(core::slice::from_raw_parts(addr.get() as *const u8, count)) @@ -1343,12 +1275,16 @@ impl hbvm::mem::Memory for LoggedMem { source: *const u8, count: usize, ) -> Result<(), hbvm::mem::StoreError> { - log::trc!("store: {:x} {}", addr.get(), AsHex(core::slice::from_raw_parts(source, count))); + log::trace!( + "store: {:x} {}", + addr.get(), + AsHex(core::slice::from_raw_parts(source, count)) + ); self.mem.store(addr, source, count) } unsafe fn prog_read(&mut self, addr: hbvm::mem::Address) -> T { - if log::LOG_LEVEL == log::Level::Trc { + if log::log_enabled!(log::Level::Trace) { if core::any::TypeId::of::() == core::any::TypeId::of::() { if let Some(instr) = self.prev_instr { self.display_instr::<()>(instr, addr); diff --git a/hblang/src/main.rs b/hblang/src/main.rs index 3303417f..802e5369 100644 --- a/hblang/src/main.rs +++ b/hblang/src/main.rs @@ -2,6 +2,8 @@ fn main() -> std::io::Result<()> { use std::{io::Write, num::NonZeroUsize}; + log::set_logger(&hblang::Logger).unwrap(); + let args = std::env::args().collect::>(); let args = args.iter().map(String::as_str).collect::>(); diff --git a/hblang/src/parser.rs b/hblang/src/parser.rs index bcf3046b..faf35ed3 100644 --- a/hblang/src/parser.rs +++ b/hblang/src/parser.rs @@ -108,7 +108,7 @@ impl<'a, 'b> Parser<'a, 'b> { if !errors.is_empty() { // TODO: we need error recovery - crate::log::eprintln!("{errors}"); + log::error!("{errors}"); unreachable!(); } @@ -576,7 +576,7 @@ impl<'a, 'b> Parser<'a, 'b> { fn report(&self, pos: Pos, msg: impl fmt::Display) -> ! { let mut str = String::new(); report_to(self.lexer.source(), self.path, pos, msg, &mut str); - crate::log::eprintln!("{str}"); + log::error!("{str}"); unreachable!(); } diff --git a/hblang/src/son.rs b/hblang/src/son.rs index b43bc381..9b172114 100644 --- a/hblang/src/son.rs +++ b/hblang/src/son.rs @@ -3,7 +3,6 @@ use { ident::Ident, instrs, lexer::{self, TokenKind}, - log, parser::{ self, idfl::{self}, @@ -568,14 +567,14 @@ impl Nodes { let mut out = String::new(); self.visited.clear(self.values.len()); self.basic_blocks_low(&mut out, VOID).unwrap(); - log::inf!("{out}"); + log::info!("{out}"); } #[allow(dead_code)] fn graphviz(&self) { let out = &mut String::new(); _ = self.graphviz_low(out); - log::inf!("{out}"); + log::info!("{out}"); } fn is_cfg(&self, o: Nid) -> bool { @@ -2016,7 +2015,7 @@ impl Codegen { name: Option, lit_name: &str, ) -> ty::Kind { - log::trc!("find_or_declare: {lit_name} {file}"); + log::trace!("find_or_declare: {lit_name} {file}"); let f = self.files[file as usize].clone(); let Some((expr, ident)) = f.find_decl(name.ok_or(lit_name)) else {