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
#![warn(missing_docs)]
extern crate arc_swap;
extern crate slog;
use std::sync::Arc;
use arc_swap::ArcSwap;
use slog::*;
type Inner<O, E> = Arc<ArcSwap<Box<dyn SendSyncRefUnwindSafeDrain<Ok=O,Err=E>>>>;
#[derive(Clone)]
pub struct AtomicSwitchCtrl<O=(), E=slog::Never>(Inner<O, E>);
#[derive(Clone)]
pub struct AtomicSwitch<O=(), E=slog::Never>(Inner<O, E>);
impl Default for AtomicSwitch<(), slog::Never> {
fn default() -> Self {
Self::new(Discard)
}
}
impl<O, E> AtomicSwitch<O, E> {
pub fn new<D: SendSyncRefUnwindSafeDrain<Ok = O, Err = E> + 'static>(drain: D) -> Self {
AtomicSwitch(Arc::new(ArcSwap::from_pointee(Box::new(drain))))
}
pub fn ctrl(&self) -> AtomicSwitchCtrl<O, E> {
AtomicSwitchCtrl(self.0.clone())
}
}
impl<O, E> AtomicSwitchCtrl<O, E> {
pub fn get(&self) -> Arc<Box<dyn SendSyncRefUnwindSafeDrain<Ok = O, Err = E>>> {
self.0.load_full()
}
pub fn set<D: SendSyncRefUnwindSafeDrain<Ok = O, Err = E> + 'static>(&self, drain: D) {
self.0.store(Arc::new(Box::new(drain)));
}
pub fn swap(&self,
drain: Arc<Box<dyn SendSyncRefUnwindSafeDrain<Ok = O, Err = E>>>)
-> Arc<Box<dyn SendSyncRefUnwindSafeDrain<Ok = O, Err = E>>> {
self.0.swap(drain)
}
pub fn drain(&self) -> AtomicSwitch<O, E> {
AtomicSwitch(self.0.clone())
}
}
impl<O, E> Drain for AtomicSwitch<O, E> {
type Ok = O;
type Err = E;
fn log(&self, info: &Record, kv: &OwnedKVList) -> std::result::Result<O, E> {
self.0.load().log(info, kv)
}
fn is_enabled(&self, level: Level) -> bool {
self.0.load().is_enabled(level)
}
}