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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
extern crate log;

use std::rc::Rc;
use std::sync::Arc;
use std::cell::RefCell;
use std::mem;
use log::Log;
use log::Metadata;
use log::Record;

pub struct Logger {
    underlying: Box<Log>,
}

impl Logger {
    pub fn new(underlying: Box<Log>) -> Logger {
        Logger {
            underlying,
        }
    }
}

impl Log for Logger {
    fn enabled(&self, metadata: &Metadata) -> bool {
        self.underlying.enabled(metadata)
    }

    fn log(&self, record: &Record) {
        get(|ndc| {
            if ndc.is_empty() {
                self.underlying.log(record)
            } else {
                self.underlying.log(
                    &Record::builder()
                        .metadata(record.metadata().clone())
                        .args(format_args!("[{}] {}", ndc, record.args()))
                        .module_path(record.module_path())
                        .file(record.file())
                        .line(record.line())
                        .build());
            }
        })
    }

    fn flush(&self) {
        self.underlying.flush()
    }
}

pub fn set_boxed_logger(logger: Box<Log>) -> Result<(), log::SetLoggerError> {
    log::set_boxed_logger(Box::new(Logger::new(logger)))
}

/// Diagnostic context
#[derive(Debug, Clone)]
pub enum Ndc {
    Str(&'static str),
    String(String),
    Rc(Rc<String>),
    Arc(Arc<String>),
}

impl Default for Ndc {
    fn default() -> Self {
        Ndc::Str("")
    }
}

impl Ndc {
    /// View as string
    fn to_str(&self) -> &str {
        match self {
            Ndc::Str(s) => s,
            Ndc::String(s) => &s,
            Ndc::Rc(s) => &**s,
            Ndc::Arc(s) => &**s,
        }

    }
}

impl From<&'static str> for Ndc {
    fn from(s: &'static str) -> Self {
        Ndc::Str(s)
    }
}

impl From<String> for Ndc {
    fn from(s: String) -> Self {
        Ndc::String(s)
    }
}

impl From<Rc<String>> for Ndc {
    fn from(s: Rc<String>) -> Self {
        Ndc::Rc(s)
    }
}

impl From<Arc<String>> for Ndc {
    fn from(s: Arc<String>) -> Self {
        Ndc::Arc(s)
    }
}

thread_local! {
    static NDC: RefCell<Ndc> = RefCell::new(Ndc::Str(""));
}

#[must_use]
pub struct SetGuard {
    ndc: Ndc,
}

impl Drop for SetGuard {
    fn drop(&mut self) {
        set(mem::replace(&mut self.ndc, Default::default()));
    }
}

/// Set thread-local context to a given string.
pub fn set<S: Into<Ndc>>(ndc: S) {
    replace(ndc);
}

/// Set thread-local context to a given string and return previously stored value.
pub fn replace<S: Into<Ndc>>(ndc: S) -> Ndc {
    NDC.with(|r| {
        mem::replace(&mut *r.borrow_mut(), ndc.into())
    })
}

/// Clear diagnostic context
pub fn clear() {
    set("");
}

/// Set thread-local context and return a guard which sets previous value on drop.
pub fn push<S: Into<Ndc>>(ndc: S) -> SetGuard {
    SetGuard { ndc: replace(ndc) }
}

/// Get a copy of the raw NDC object.
/// Return NDC with empty string if unset.
pub fn get_raw() -> Ndc {
    NDC.with(|r| r.borrow().clone())
}

/// Get a copy of the string in thread-local context.
/// Empty string is returned when there's no context
pub fn get_copy() -> String {
    get(|s| s.to_owned())
}

/// Read a thread-local context with provided callback.
pub fn get<R, F>(f: F) -> R where F: FnOnce(&str) -> R {
    NDC.with(|r| f(r.borrow().to_str()))
}

#[cfg(test)]
mod test {
    mod log_ndc {
        pub use super::super::*;
    }

    #[test]
    fn set_get() {
        let _g = log_ndc::push("");

        log_ndc::set("aa");
        assert_eq!("aa", log_ndc::get_copy());
    }

    #[test]
    fn push() {
        let _g = log_ndc::push("");

        let _g1 = log_ndc::push("aa");
        assert_eq!("aa", log_ndc::get_copy());

        let g2 = log_ndc::push("bb");
        assert_eq!("bb", log_ndc::get_copy());
        drop(g2);

        assert_eq!("aa", log_ndc::get_copy());
    }
}