diff options
author | Daniel <kingdread@gmx.de> | 2020-04-09 13:01:10 +0200 |
---|---|---|
committer | Daniel <kingdread@gmx.de> | 2020-04-09 13:01:10 +0200 |
commit | 79eae45196e89672a8d564e7595330cd7743fe17 (patch) | |
tree | b722e46463a8e42f896becf50752ca0aa08e6f91 /src | |
parent | d9b7433d168f3a5254e9bd10efd8c2875a3ff366 (diff) | |
download | raidgrep-79eae45196e89672a8d564e7595330cd7743fe17.tar.gz raidgrep-79eae45196e89672a8d564e7595330cd7743fe17.tar.bz2 raidgrep-79eae45196e89672a8d564e7595330cd7743fe17.zip |
use log crate instead of own debug! macro
This also does away with the scary unsafe{} blocks just to set/get the
DEBUG flag.
Diffstat (limited to 'src')
-rw-r--r-- | src/logger.rs | 30 | ||||
-rw-r--r-- | src/main.rs | 28 |
2 files changed, 36 insertions, 22 deletions
diff --git a/src/logger.rs b/src/logger.rs new file mode 100644 index 0000000..f47d19e --- /dev/null +++ b/src/logger.rs @@ -0,0 +1,30 @@ +use log::{Level, LevelFilter, Metadata, Record}; + +struct StderrLogger(LevelFilter); + +impl log::Log for StderrLogger { + fn enabled(&self, metadata: &Metadata) -> bool { + metadata.level() <= self.0 + } + + fn log(&self, record: &Record) { + if self.enabled(record.metadata()) { + eprintln!("{} - {}", record.level(), record.args()); + } + } + + fn flush(&self) {} +} + +/// Initializes the logging with the desired level. +/// +/// Note that this should only be called once per program start. +/// +/// The given level is the maximum level that you want to output. Messages higher than that will be +/// discarded. +pub fn initialize(max_level: Level) { + let filter = max_level.to_level_filter(); + let logger = Box::new(StderrLogger(filter)); + log::set_boxed_logger(logger).expect("Failed to set logger"); + log::set_max_level(filter); +} diff --git a/src/main.rs b/src/main.rs index f82e522..bbf4b70 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,12 +10,14 @@ use num_traits::cast::FromPrimitive; use regex::Regex; use structopt::StructOpt; use walkdir::{DirEntry, WalkDir}; +use log::debug; use evtclib::{AgentKind, AgentName, EventKind, Log}; +mod filters; mod guilds; +mod logger; mod output; -mod filters; mod csl; use csl::CommaSeparatedList; @@ -30,25 +32,6 @@ macro_rules! unwrap { }; } -macro_rules! debug { - ($($arg:tt)*) => { - if debug_enabled() { - use std::io::Write; - let stderr = ::std::io::stderr(); - let mut lock = stderr.lock(); - write!(lock, "[d] ").expect("debug write failed"); - writeln!(lock, $($arg)*).expect("debug write failed"); - } - } -} - -static mut DEBUG_ENABLED: bool = false; - -/// Return whether or not debug output should be enabled. -#[inline] -fn debug_enabled() -> bool { - unsafe { DEBUG_ENABLED } -} /// A program that allows you to search through all your evtc logs for specific /// people. @@ -244,8 +227,9 @@ fn main() { } if opt.debug { - // We haven't started any threads here yet, so this is fine. - unsafe { DEBUG_ENABLED = true }; + logger::initialize(log::Level::Debug); + } else { + logger::initialize(log::Level::Info); } if opt.guilds { |