From 79eae45196e89672a8d564e7595330cd7743fe17 Mon Sep 17 00:00:00 2001
From: Daniel <kingdread@gmx.de>
Date: Thu, 9 Apr 2020 13:01:10 +0200
Subject: use log crate instead of own debug! macro

This also does away with the scary unsafe{} blocks just to set/get the
DEBUG flag.
---
 Cargo.toml    |  1 +
 src/logger.rs | 30 ++++++++++++++++++++++++++++++
 src/main.rs   | 28 ++++++----------------------
 3 files changed, 37 insertions(+), 22 deletions(-)
 create mode 100644 src/logger.rs

diff --git a/Cargo.toml b/Cargo.toml
index 85b7792..b1b9a12 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -22,3 +22,4 @@ ureq = { version = "0.12", features = ["json"] }
 serde = { version = "1.0", features = ["derive"] }
 serde_json = "1.0"
 dirs = "2.0"
+log = { version = "0.4", features = ["std"] }
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 {
-- 
cgit v1.2.3