bodo_connect/logger/
mod.rs1#[cfg(feature = "log")]
2mod inner {
3 use colored::Colorize;
4 use log::{Level, Metadata, Record};
5 use std::collections::HashMap;
6 use std::string::ToString;
7
8 lazy_static::lazy_static! {
9 pub static ref LOGGER_COLORS: HashMap<Level, String> = HashMap::from([
10 (Level::Debug, "green".to_string()),
11 (Level::Info, "blue".to_string()),
12 (Level::Warn, "yellow".to_string()),
13 (Level::Error, "red".to_string()),
14 ]);
15 }
16
17 pub static CONSOLE_LOGGER: ConsoleLogger = ConsoleLogger;
18 pub struct ConsoleLogger;
19
20 impl log::Log for ConsoleLogger {
21 fn enabled(&self, metadata: &Metadata) -> bool {
22 metadata.level() <= Level::Debug
23 }
24
25 fn log(&self, record: &Record) {
26 if self.enabled(record.metadata())
27 && record
28 .module_path()
29 .unwrap_or("")
30 .starts_with("bodo_connect::")
31 {
32 let level = format!("{:>7}", record.level());
33 eprintln!(
34 "{}: {}",
35 match LOGGER_COLORS.get(&record.level()) {
36 Some(c) => level.color(&**c).to_string(),
37 None => level,
38 },
39 record.args()
40 )
41 }
42 }
43
44 fn flush(&self) {}
45 }
46}
47
48#[cfg(feature = "log")]
49pub use inner::{ConsoleLogger, CONSOLE_LOGGER};
50
51#[cfg(not(feature = "log"))]
52mod dummy;