#![warn(missing_docs)]
#[macro_use(o)]
extern crate slog;
#[macro_use]
extern crate lazy_static;
extern crate arc_swap;
use slog::{Logger, Record, OwnedKVList};
use std::sync::Arc;
use std::cell::RefCell;
use arc_swap::ArcSwap;
use std::result;
pub use slog::{slog_crit, slog_debug, slog_error, slog_info, slog_trace, slog_warn};
#[macro_export]
macro_rules! crit( ($($args:tt)+) => {
$crate::with_logger(|logger| $crate::slog_crit![logger, $($args)+])
};);
#[macro_export]
macro_rules! error( ($($args:tt)+) => {
$crate::with_logger(|logger| $crate::slog_error![logger, $($args)+])
};);
#[macro_export]
macro_rules! warn( ($($args:tt)+) => {
$crate::with_logger(|logger| $crate::slog_warn![logger, $($args)+])
};);
#[macro_export]
macro_rules! info( ($($args:tt)+) => {
$crate::with_logger(|logger| $crate::slog_info![logger, $($args)+])
};);
#[macro_export]
macro_rules! debug( ($($args:tt)+) => {
$crate::with_logger(|logger| $crate::slog_debug![logger, $($args)+])
};);
#[macro_export]
macro_rules! trace( ($($args:tt)+) => {
$crate::with_logger(|logger| $crate::slog_trace![logger, $($args)+])
};);
thread_local! {
static TL_SCOPES: RefCell<Vec<*const slog::Logger>> = RefCell::new(Vec::with_capacity(8))
}
lazy_static! {
static ref GLOBAL_LOGGER : ArcSwap<slog::Logger> = ArcSwap::from(
Arc::new(
slog::Logger::root(slog::Discard, o!())
)
);
}
struct NoGlobalLoggerSet;
impl slog::Drain for NoGlobalLoggerSet {
type Ok = ();
type Err = slog::Never;
fn log(&self,
_record: &Record,
_values: &OwnedKVList)
-> result::Result<Self::Ok, Self::Err> {
panic!(
"slog-scope: No logger set. Use `slog_scope::set_global_logger` or `slog_scope::scope`."
)
}
}
#[must_use]
pub struct GlobalLoggerGuard {
canceled : bool,
}
impl GlobalLoggerGuard {
pub fn is_canceled(&self) -> bool {
self.canceled
}
fn new() -> Self {
GlobalLoggerGuard {
canceled: false,
}
}
pub fn cancel_reset(mut self) {
self.canceled = true;
}
}
impl Drop for GlobalLoggerGuard {
fn drop(&mut self) {
if !self.canceled {
GLOBAL_LOGGER.store(
Arc::new(
slog::Logger::root(NoGlobalLoggerSet, o!())
)
);
}
}
}
pub fn set_global_logger(l: slog::Logger) -> GlobalLoggerGuard {
GLOBAL_LOGGER.store(Arc::new(l));
GlobalLoggerGuard::new()
}
struct ScopeGuard;
impl ScopeGuard {
fn new(logger: &slog::Logger) -> Self {
TL_SCOPES.with(|s| { s.borrow_mut().push(logger as *const 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) => (unsafe {&**logger}).clone(),
None => Logger::clone(&GLOBAL_LOGGER.load())
}
})
}
pub fn with_logger<F, R>(f : F) -> R
where F : FnOnce(&Logger) -> R {
TL_SCOPES.with(|s| {
let s = s.borrow();
match s.last() {
Some(logger) => f(unsafe {&**logger}),
None => f(&GLOBAL_LOGGER.load()),
}
})
}
pub fn scope<SF, R>(logger: &slog::Logger, f: SF) -> R
where SF: FnOnce() -> R
{
let _guard = ScopeGuard::new(&logger);
f()
}