Browse Source

make logging system work better

master
Alison Watson 3 years ago
parent
commit
790d086717
  1. 10
      source/main.rs
  2. 60
      source/util/log.rs

10
source/main.rs

@ -194,7 +194,7 @@ fn draw_frame<'a>(
fn seize_device(lg: &log::Log, device: &Device) {
let seize = unsafe { device.device_wait_idle() };
if let Err(e) = seize {
lg!(lg, Level::Error, "Error seizing renderer state: {}", e);
lg!(lg, log::Level::Error, "Error seizing renderer state: {}", e);
}
}
@ -294,7 +294,7 @@ fn fallback_main(
)?;
}
Err(res) => {
lg!(lg, Level::Error, "Error rendering frame: {}", res);
lg!(lg, log::Level::Error, "Error rendering frame: {}", res);
}
}
@ -307,7 +307,7 @@ fn fallback_main(
}
fn main() {
let lg = log::Log::new().insert_mode("Std", log::Std);
let lg = log::Log::new();
#[cfg(feature = "color-log")]
let lg = lg.insert_mode("Color", log::Color).set_mode("Color").unwrap();
@ -318,12 +318,12 @@ fn main() {
_ => log::Level::Error,
};
lg!(&lg, level, "{}", err);
lg!(&lg, Level::Notice, "Using default configuration.");
lg!(&lg, log::Level::Notice, "Using default configuration.");
conf::Conf::default()
});
if let Err(e) = fallback_main(&conf, &lg) {
lg!(&lg, Level::Critical, "Uncaught error: {}", e);
lg!(&lg, log::Level::Critical, "Uncaught error: {}", e);
}
}

60
source/util/log.rs

@ -10,22 +10,19 @@ use std::collections::HashMap;
#[macro_export]
macro_rules! lg {
($to:expr, $level:expr, $($args:tt)+) => {
{
#[allow(unused_imports)]
use $crate::util::log::Level;
let (level, to) = ($level, $to);
if to.enabled(level) {
let record = $crate::util::log::Record {
level: level,
module: std::module_path!(),
file: std::file!(),
line: std::line!(),
column: std::column!(),
};
to.log(std::format_args!($($args)+), record);
match ($level, $to) {
(level, to) => {
if to.enabled(level) {
let record = $crate::util::log::Record {
level: level,
module: std::module_path!(),
file: std::file!(),
line: std::line!(),
column: std::column!(),
};
to.log(std::format_args!($($args)+), record);
}
}
}
};
@ -34,24 +31,25 @@ macro_rules! lg {
#[macro_export]
macro_rules! trace {
($to:expr) => {
#[cfg(debug_assertions)]
{
$crate::lg!($to, Level::Trace, "");
#[cfg(debug_assertions)]
$crate::lg!($to, $crate::util::log::Level::Trace, "");
}
};
($to:expr, $val:expr) => {
#[cfg(debug_assertions)]
{
let val = $val;
$crate::lg!(
$to,
Level::Trace,
"{} = {:#?}",
std::stringify!($val),
&val,
);
val
match $val {
val => {
#[cfg(debug_assertions)]
$crate::lg!(
$to,
$crate::util::log::Level::Trace,
"{} = {:#?}",
std::stringify!($val),
&val,
);
val
}
}
};
@ -81,8 +79,10 @@ pub trait LogMode {
impl Log {
pub fn new() -> Self {
let mut modes: HashMap<_, Box<dyn LogMode>> = HashMap::new();
modes.insert("Std", Box::new(Std));
Self {
modes: HashMap::new(),
modes,
level: Level::default(),
begin: std::time::Instant::now(),
mode: "Std",

Loading…
Cancel
Save