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
pub extern crate log;

use std::rc::Rc;
use std::sync::Arc;
use std::cell::RefCell;
use std::mem;

mod macros;

/// 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());
    }
}