#![warn(missing_docs)]
#[macro_use(o)]
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!()).into_erased()
)
);
}
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();
match s.last() {
Some(logger) => logger.clone(),
None => (*GLOBAL_LOGGER.get()).clone(),
}
})
}
pub fn scope<SF, R>(logger: slog::Logger, f: SF) -> R
where SF: FnOnce() -> R
{
let _guard = ScopeGuard::new(logger);
f()
}