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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//! # A specialized Logger for [`pt`](../libpt/index.html)
//!
//! This crate is part of [`pt`](../libpt/index.html), but can also be used as a standalone
//! module.
//!
//! For the library version, only the basic [`tracing`] is used, so that it is possible for
//! the end user to use the [`tracing`] frontend they desire.
//!
//! I did however decide to create a [`Logger`] struct. This struct is mainly intended to be used
//! with the python module of [`pt`](../libpt/index.html), but is still just as usable in other contexts.
//!
//! ## Technologies used for logging:
//! - [`tracing`]: base logging crate
//! - [`tracing_appender`]: Used to log to files
//! - [`tracing_subscriber`]: Used to do actual logging, formatting, to stdout

use std::{
    fmt,
    path::PathBuf,
    sync::atomic::{AtomicBool, Ordering},
};

pub mod error;
use error::*;

pub use tracing::{debug, error, info, trace, warn, Level};
use tracing_appender::{self, non_blocking::NonBlocking};
use tracing_subscriber::fmt::{format::FmtSpan, time};

use anyhow::{bail, Result};
/// The log level used when none is specified
pub const DEFAULT_LOG_LEVEL: Level = Level::INFO;
/// The path where logs are stored when no path is given.
///
/// Currently, this is `/dev/null`, meaning they will be written to the void = discarded.
pub const DEFAULT_LOG_DIR: &str = "/dev/null";

static INITIALIZED: AtomicBool = AtomicBool::new(false);

/// ## Logger for [`pt`](../libpt/index.html)
///
/// This struct exists mainly for the python module, so that we can use the same logger with both
/// python and rust.
pub struct Logger;

/// ## Main implementation
impl Logger {
    /// ## initializes the logger
    ///
    /// Will enable the logger to be used.
    ///
    /// Assumes some defaults, use [`init_customized`](Self::init_customized) for more control
    pub fn build(log_dir: Option<PathBuf>, max_level: Option<Level>, uptime: bool) -> Result<Self> {
        Self::build_customized(
            log_dir.is_some(),
            log_dir.unwrap_or(PathBuf::from(DEFAULT_LOG_DIR)),
            true,
            false,
            true,
            false,
            max_level.unwrap_or(DEFAULT_LOG_LEVEL),
            false,
            false,
            false,
            false,
            true,
            uptime,
        )
    }

    /// ## initializes the logger
    ///
    /// Will enable the logger to be used. This is a version that shows less information,
    /// useful in cases with only one sender to the logging framework.
    ///
    /// Assumes some defaults, use [`init_customized`](Self::init_customized) for more control
    pub fn build_mini(max_level: Option<Level>) -> Result<Self> {
        Self::build_customized(
            false,
            PathBuf::from(DEFAULT_LOG_DIR),
            true,
            false,
            true,
            false,
            max_level.unwrap_or(DEFAULT_LOG_LEVEL),
            false,
            false,
            false,
            false,
            false,
            false,
        )
    }

    // TODO: make the args a struct for easy access
    //
    /// ## initializes the logger
    ///
    /// Will enable the logger to be used.
    pub fn build_customized(
        log_to_file: bool,
        log_dir: PathBuf,
        ansi: bool,
        display_filename: bool,
        display_level: bool,
        display_target: bool,
        max_level: Level,
        display_thread_ids: bool,
        display_thread_names: bool,
        display_line_number: bool,
        pretty: bool,
        show_time: bool,
        uptime: bool, // uptime instead of system time
    ) -> Result<Self> {
        // only init if no init has been performed yet
        if INITIALIZED.load(Ordering::Relaxed) {
            warn!("trying to reinitialize the logger, ignoring");
            bail!(Error::Usage("logging is already initialized".to_string()));
        }
        let subscriber = tracing_subscriber::fmt::Subscriber::builder()
            .with_level(display_level)
            .with_max_level(max_level)
            .with_ansi(ansi)
            .with_target(display_target)
            .with_file(display_filename)
            .with_thread_ids(display_thread_ids)
            .with_line_number(display_line_number)
            .with_thread_names(display_thread_names)
            .with_span_events(FmtSpan::FULL);
        // I know this is hacky, but I couldn't get it any other way. I couldn't even find a
        // project that could do it any other way. You can't apply one after another, because the
        // type is changed every time. When using Box<dyn Whatever>, some methods complain about
        // not being in trait bounds.
        // TODO: somehow find a better solution for this
        match (log_to_file, show_time, pretty, uptime) {
            (true, true, true, true) => {
                let subscriber = subscriber
                    .with_writer(new_file_appender(log_dir))
                    .with_timer(time::uptime())
                    .pretty()
                    .finish();
                tracing::subscriber::set_global_default(subscriber)?;
            }
            (true, true, true, false) => {
                let subscriber = subscriber
                    .with_writer(new_file_appender(log_dir))
                    .pretty()
                    .finish();
                tracing::subscriber::set_global_default(subscriber)?;
            }
            (true, false, true, _) => {
                let subscriber = subscriber
                    .with_writer(new_file_appender(log_dir))
                    .without_time()
                    .pretty()
                    .finish();
                tracing::subscriber::set_global_default(subscriber)?;
            }
            (true, true, false, true) => {
                let subscriber = subscriber
                    .with_writer(new_file_appender(log_dir))
                    .with_timer(time::uptime())
                    .finish();
                tracing::subscriber::set_global_default(subscriber)?;
            }
            (true, true, false, false) => {
                let subscriber = subscriber.with_writer(new_file_appender(log_dir)).finish();
                tracing::subscriber::set_global_default(subscriber)?;
            }
            (true, false, false, _) => {
                let file_appender = tracing_appender::rolling::daily(log_dir.clone(), "log");
                let (file_writer, _guard) = tracing_appender::non_blocking(file_appender);
                let subscriber = subscriber.with_writer(file_writer).without_time().finish();
                tracing::subscriber::set_global_default(subscriber)?;
            }
            (false, true, true, true) => {
                let subscriber = subscriber.pretty().with_timer(time::uptime()).finish();
                tracing::subscriber::set_global_default(subscriber)?;
            }
            (false, true, true, false) => {
                let subscriber = subscriber.pretty().with_timer(time::uptime()).finish();
                tracing::subscriber::set_global_default(subscriber)?;
            }
            (false, false, true, _) => {
                let subscriber = subscriber.without_time().pretty().finish();
                tracing::subscriber::set_global_default(subscriber)?;
            }
            (false, true, false, true) => {
                let subscriber = subscriber.with_timer(time::uptime()).finish();
                tracing::subscriber::set_global_default(subscriber)?;
            }
            (false, true, false, false) => {
                let subscriber = subscriber.finish();
                tracing::subscriber::set_global_default(subscriber)?;
            }
            (false, false, false, _) => {
                let subscriber = subscriber.without_time().finish();
                tracing::subscriber::set_global_default(subscriber)?;
            }
        }
        INITIALIZED.store(true, Ordering::Relaxed);
        Ok(Logger {})
    }

    /// ## logging at [`Level::ERROR`]
    pub fn error<T>(&self, printable: T)
    where
        T: fmt::Display,
    {
        error!("{}", printable)
    }
    /// ## logging at [`Level::WARN`]
    pub fn warn<T>(&self, printable: T)
    where
        T: fmt::Display,
    {
        warn!("{}", printable)
    }
    /// ## logging at [`Level::INFO`]
    pub fn info<T>(&self, printable: T)
    where
        T: fmt::Display,
    {
        info!("{}", printable)
    }
    /// ## logging at [`Level::DEBUG`]
    pub fn debug<T>(&self, printable: T)
    where
        T: fmt::Display,
    {
        debug!("{}", printable)
    }
    /// ## logging at [`Level::TRACE`]
    pub fn trace<T>(&self, printable: T)
    where
        T: fmt::Display,
    {
        trace!("{}", printable)
    }
}

impl fmt::Debug for Logger {
    /// ## DEBUG representation for [`Logger`]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Logger: {{initialized: {}}} ",
            INITIALIZED.load(Ordering::Relaxed)
        )
    }
}

fn new_file_appender(log_dir: PathBuf) -> NonBlocking {
    let file_appender = tracing_appender::rolling::daily(log_dir.clone(), "log");
    tracing_appender::non_blocking(file_appender).0
}