slog_atomic/
lib.rs

1//! Slog runtime switchable drain
2//!
3//! `AtomicSwitch` allows swapping drain that it wraps atomically, race-free, in
4//! runtime. This can be useful eg. for turning on debug logging
5//! in production.
6//!
7//! See [`signal.rs` example](https://github.com/slog-rs/atomic/blob/master/examples/signal.rs).
8#![warn(missing_docs)]
9
10extern crate arc_swap;
11extern crate slog;
12
13use std::sync::Arc;
14
15use arc_swap::ArcSwap;
16use slog::*;
17
18type Inner<O, E> = Arc<ArcSwap<Box<dyn SendSyncRefUnwindSafeDrain<Ok=O,Err=E>>>>;
19
20/// Handle to `AtomicSwitch` that controls it.
21#[derive(Clone)]
22pub struct AtomicSwitchCtrl<O=(), E=slog::Never>(Inner<O, E>);
23
24/// Drain wrapping another drain, allowing atomic substitution in runtime.
25#[derive(Clone)]
26pub struct AtomicSwitch<O=(), E=slog::Never>(Inner<O, E>);
27
28impl Default for AtomicSwitch<(), slog::Never> {
29    fn default() -> Self {
30        Self::new(Discard)
31    }
32}
33
34impl<O, E> AtomicSwitch<O, E> {
35    /// Wrap `drain` in `AtomicSwitch` to allow swapping it later
36    ///
37    /// Use `AtomicSwitch::ctrl()` to get a handle to it
38    pub fn new<D: SendSyncRefUnwindSafeDrain<Ok = O, Err = E> + 'static>(drain: D) -> Self {
39        AtomicSwitch(Arc::new(ArcSwap::from_pointee(Box::new(drain))))
40
41    }
42
43    /// Get a `AtomicSwitchCtrl` handle to control this `AtomicSwitch` drain
44    pub fn ctrl(&self) -> AtomicSwitchCtrl<O, E> {
45        AtomicSwitchCtrl(self.0.clone())
46    }
47}
48
49impl<O, E> AtomicSwitchCtrl<O, E> {
50    /// Get Arc to the currently wrapped drain
51    pub fn get(&self) -> Arc<Box<dyn SendSyncRefUnwindSafeDrain<Ok = O, Err = E>>> {
52        self.0.load_full()
53    }
54
55    /// Set the current wrapped drain
56    pub fn set<D: SendSyncRefUnwindSafeDrain<Ok = O, Err = E> + 'static>(&self, drain: D) {
57        self.0.store(Arc::new(Box::new(drain)));
58    }
59
60    /// Swap the existing drain with a new one
61    pub fn swap(&self,
62                drain: Arc<Box<dyn SendSyncRefUnwindSafeDrain<Ok = O, Err = E>>>)
63                -> Arc<Box<dyn SendSyncRefUnwindSafeDrain<Ok = O, Err = E>>> {
64        self.0.swap(drain)
65    }
66
67    /// Get a `AtomicSwitch` drain controlled by this `AtomicSwitchCtrl`
68    pub fn drain(&self) -> AtomicSwitch<O, E> {
69        AtomicSwitch(self.0.clone())
70    }
71}
72
73impl<O, E> Drain for AtomicSwitch<O, E> {
74    type Ok = O;
75    type Err = E;
76    fn log(&self, info: &Record, kv: &OwnedKVList) -> std::result::Result<O, E> {
77        self.0.load().log(info, kv)
78    }
79
80    fn is_enabled(&self, level: Level) -> bool {
81        self.0.load().is_enabled(level)
82    }
83}