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
#![warn(missing_docs)]
#[macro_use(o, slog_log, slog_trace, slog_debug, slog_info, slog_warn, slog_error)]
extern crate slog;
#[macro_use]
extern crate lazy_static;
extern crate crossbeam;
use slog::*;
use std::sync::Arc;
use std::cell::RefCell;
use crossbeam::sync::ArcCell;
#[macro_export] macro_rules! crit( ($($args:tt)+) => { slog_crit![$crate::logger(), $($args)+]; };);
#[macro_export] macro_rules! error( ($($args:tt)+) => { slog_error![$crate::logger(), $($args)+]; };);
#[macro_export] macro_rules! warn( ($($args:tt)+) => { slog_warn![$crate::logger(), $($args)+]; };);
#[macro_export] macro_rules! info( ($($args:tt)+) => { slog_info![$crate::logger(), $($args)+]; };);
#[macro_export] macro_rules! debug( ($($args:tt)+) => { slog_debug![$crate::logger(), $($args)+]; };);
#[macro_export] macro_rules! trace( ($($args:tt)+) => { slog_trace![$crate::logger(), $($args)+]; };);
thread_local! {
static TL_SCOPES: RefCell<Vec<slog::Logger>> = RefCell::new(Vec::with_capacity(8))
}
lazy_static! {
static ref GLOBAL_LOGGER : ArcCell<slog::Logger> = ArcCell::new(
Arc::new(
slog::Logger::root(slog::Discard, o!())
)
);
}
pub fn set_global_logger(l: slog::Logger) {
let _ = GLOBAL_LOGGER.set(Arc::new(l));
}
struct ScopeGuard;
impl ScopeGuard {
fn new(logger: slog::Logger) -> Self {
TL_SCOPES.with(|s| {
s.borrow_mut().push(logger);
});
ScopeGuard
}
}
impl Drop for ScopeGuard {
fn drop(&mut self) {
TL_SCOPES.with(|s| {
s.borrow_mut().pop().expect("TL_SCOPES should contain a logger");
})
}
}
pub fn logger() -> Logger {
TL_SCOPES.with(|s| {
let s = s.borrow();
if s.is_empty() {
(*GLOBAL_LOGGER.get()).clone()
} else {
s[s.len() - 1].clone()
}
})
}
pub fn scope<SF, R>(logger: slog::Logger, f: SF) -> R
where SF: FnOnce() -> R
{
let _guard = ScopeGuard::new(logger);
f()
}