summaryrefslogtreecommitdiffstats
path: root/src/utils/logging.rs
blob: 0b56591ca40eb560a5505f6acee210c83c22a245 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#[cfg(feature = "logging")]
pub static OFFSET: once_cell::sync::Lazy<time::UtcOffset> = once_cell::sync::Lazy::new(|| {
    use time::util::local_offset::Soundness;

    // SAFETY: We only invoke this once, quickly, and it should be invoked in a single-thread context.
    // We also should only ever hit this logging at all in a debug context which is generally fine,
    // release builds should have this logging disabled entirely for now.
    unsafe {
        // XXX: If we ever DO add general logging as a release feature, evaluate this again and whether this is
        // something we want enabled in release builds! What might be safe is falling back to the non-set-soundness
        // mode when specifically using certain feature flags (e.g. dev-logging feature enables this behaviour).

        time::util::local_offset::set_soundness(Soundness::Unsound);
        let res = time::UtcOffset::current_local_offset().unwrap_or(time::UtcOffset::UTC);
        time::util::local_offset::set_soundness(Soundness::Sound);

        res
    }
});

#[cfg(feature = "logging")]
pub fn init_logger(
    min_level: log::LevelFilter, debug_file_name: Option<&std::ffi::OsStr>,
) -> Result<(), fern::InitError> {
    let dispatch = fern::Dispatch::new()
        .format(|out, message, record| {
            let offset_time = {
                let utc = time::OffsetDateTime::now_utc();
                utc.checked_to_offset(*OFFSET).unwrap_or(utc)
            };

            out.finish(format_args!(
                "{}[{}][{}] {}",
                offset_time
                    .format(&time::macros::format_description!(
                        // The weird "[[[" is because we need to escape a bracket ("[[") to show one "[".
                        // See https://time-rs.github.io/book/api/format-description.html
                        "[[[year]-[month]-[day]][[[hour]:[minute]:[second][subsecond digits:9]]"
                    ))
                    .unwrap(),
                record.target(),
                record.level(),
                message
            ))
        })
        .level(min_level);

    if let Some(debug_file_name) = debug_file_name {
        dispatch.chain(fern::log_file(debug_file_name)?).apply()?;
    } else {
        dispatch.chain(std::io::stdout()).apply()?;
    }

    Ok(())
}

#[macro_export]
macro_rules! error {
    ($($x:tt)*) => {
        #[cfg(feature = "logging")]
        {
            log::error!($($x)*)
        }
    };
}

#[macro_export]
macro_rules! warn {
    ($($x:tt)*) => {
        #[cfg(feature = "logging")]
        {
            log::warn!($($x)*)
        }
    };
}

#[macro_export]
macro_rules! info {
    ($($x:tt)*) => {
        #[cfg(feature = "logging")]
        {
            log::info!($($x)*)
        }
    };
}

#[macro_export]
macro_rules! debug {
    ($($x:tt)*) => {
        #[cfg(feature = "logging")]
        {
            log::debug!($($x)*)
        }
    };
}

#[macro_export]
macro_rules! trace {
    ($($x:tt)*) => {
        #[cfg(feature = "logging")]
        {
            log::trace!($($x)*)
        }
    };
}

#[macro_export]
macro_rules! log {
    ($($x:tt)*) => {
        #[cfg(feature = "logging")]
        {
            log::log!(log::Level::Trace, $($x)*)
        }
    };
    ($level:expr, $($x:tt)*) => {
        #[cfg(feature = "logging")]
        {
            log::log!($level, $($x)*)
        }
    };
}

#[cfg(test)]
mod test {

    #[cfg(feature = "logging")]
    #[test]
    fn test_logging_macros() {
        super::init_logger(log::LevelFilter::Trace, None)
            .expect("initializing the logger should succeed");

        error!("This is an error.");
        warn!("This is a warning.");
        info!("This is an info.");
        debug!("This is a debug.");
        info!("This is a trace.");
    }
}