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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use std::{
    borrow::Borrow,
    convert::TryFrom,
};

use tracing_subscriber::reload;

use crate::{
    config::Config,
    error::{
        Error,
        Result,
    },
};

mod layers;
mod loggers;
mod span_broker;
mod trace_logger;

use layers::Layers;
pub use layers::PolyLayer;
use span_broker::SpanBroker;
pub use trace_logger::TraceLogger;

/// The reloadable handle for a `TraceLogger`, with this we can modify the
/// logging configuration at runtime.
#[derive(Clone)]
pub struct Handle {
    reload_handle: reload::Handle<Layers<SpanBroker>, SpanBroker>,
    trace_logger:  TraceLogger,
    broker:        SpanBroker,
}

/// Initializes the default `trace4rs` handle as the `tracing` global default.
///
/// # Errors
/// We could fail to set the global default subscriber for `tracing`.
pub fn init_console_logger() -> Result<Handle> {
    let h = Handle::default();
    tracing::subscriber::set_global_default(h.subscriber())?;
    Ok(h)
}

impl Handle {
    /// Get the subscriber that backs this handle.
    #[must_use]
    pub fn subscriber(&self) -> TraceLogger {
        self.trace_logger.clone()
    }

    /// Disable the subscriber.
    ///
    /// # Errors
    /// - An io error occurred in flushing output.
    /// - We were unable to update the subscriber.
    pub fn disable(&self) -> Result<()> {
        self.reload_handle
            .modify(Layers::disable)
            .map_err(Into::into)
    }

    /// Enable the subscriber.
    ///
    /// # Errors
    /// - An io error occurred in flushing output.
    /// - We were unable to update the subscriber.
    pub fn enable(&self) -> Result<()> {
        self.reload_handle
            .modify(Layers::enable)
            .map_err(Into::into)
    }

    /// Flush buffered output for all appenders.
    ///
    /// # Errors
    /// - An io error occurred in flushing output.
    /// - We were unable to update the subscriber.
    pub fn flush(&self) -> Result<()> {
        let h = self.reload_handle.borrow();
        h.with_current(|ls| ls.appenders().flush())??;
        Ok(())
    }

    /// Correct the output path of log files if they have been moved.
    ///
    /// # Errors
    /// - We were unable to update the subscriber.
    /// - Re-mounting a file has failed.
    pub fn correct_appender_paths(&self) -> Result<()> {
        self.reload_handle
            .borrow()
            .with_current(Layers::correct_appender_paths)??;
        Ok(())
    }

    /// Update with the given config.
    ///
    /// # Errors
    /// - We were unable to update the subscriber.
    /// - Building the appenders in the config, for example
    /// opening a file for write.
    pub fn update(&mut self, config: &Config) -> Result<()> {
        let ls = Layers::from_config(self.broker.clone(), config)?;
        Ok(self.reload_handle.reload(ls)?)
    }

    /// Using the given `SpanBroker` we configure and initialize our `Self`.
    ///
    /// # Errors
    /// This could fail building the appenders in the config, for example
    /// opening a file for write.
    pub fn from_config(broker: SpanBroker, config: &Config) -> Result<Handle> {
        let layers = Layers::from_config(broker.clone(), config)?;
        Ok(Self::from_layers(broker, layers))
    }

    /// Builds `Self` from `Layers` and the backing `SpanBroker`.
    fn from_layers(broker: SpanBroker, layers: Layers) -> Self {
        let (reloadable, reload_handle) = reload::Layer::new(layers);
        let trace_logger = TraceLogger::new(broker.clone(), reloadable);

        Self {
            reload_handle,
            trace_logger,
            broker,
        }
    }
}

impl Default for Handle {
    fn default() -> Self {
        let broker = SpanBroker::new();
        let layers = Layers::default(broker.clone());

        Self::from_layers(broker, layers)
    }
}

impl TryFrom<Config> for Handle {
    type Error = Error;

    fn try_from(c: Config) -> Result<Handle> {
        Self::try_from(&c)
    }
}

impl TryFrom<&Config> for Handle {
    type Error = Error;

    fn try_from(c: &Config) -> Result<Handle> {
        let broker = SpanBroker::new();
        Self::from_config(broker, c)
    }
}