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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
//! # 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);

/// Builder for a well configured [Logger]
///
/// This struct helps configure a global logger that can be used with either macros or methods, see
/// [Logger].
///
/// ## Examples
///
/// ```
/// # use libpt_log::{Logger, info};
/// # fn main() {
/// Logger::builder()
///     .uptime(true)
///     .build();
/// info!("hello world");
/// # }
///
/// ```
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct LoggerBuilder {
    /// create and log to logfiles
    log_to_file: bool,
    /// logfiles would be created here
    log_dir: PathBuf,
    /// use ANSI control sequences
    ansi: bool,
    /// show which source file produces a log
    display_filename: bool,
    /// show the log level of the message
    display_level: bool,
    /// show target context
    display_target: bool,
    /// sets the maximum verbosity level.
    ///
    /// For example, if set to [Error](Level::ERROR), logs at [Info](Level::INFO) will not be
    /// printed. If set to [Debug](Level::DEBUG), logs at [Info](Level::INFO) will be printed.
    max_level: Level,
    /// show the id of the thread that created this message
    display_thread_ids: bool,
    /// show the name of the thread that created this message
    display_thread_names: bool,
    /// show which line in the source file produces a log
    display_line_number: bool,
    /// splits a log over multiple lines, looks like a python traceback
    pretty: bool,
    /// show when the log was created
    show_time: bool,
    /// show timestamps as uptime (duration since the logger was initialized)
    uptime: bool,
}

impl LoggerBuilder {
    /// use the configured settings to build and initialize a new global [Logger]
    ///
    /// This will build a functional [Logger]. You don't need to use the [Logger] struct, it's
    /// better to use the macros:
    ///
    /// * `error!`
    /// * `warn!`
    /// * `info!`
    /// * `debug!`
    /// * `trace!`
    ///
    /// instead of the methods of the [Logger] struct. You can however use the [Logger] struct in
    /// cases where usage of a macro is bad or you are somehow working with multiple loggers.
    ///
    /// ## Examples
    ///
    /// ```
    /// # use libpt_log::{Logger, info};
    /// # fn main() {
    /// Logger::builder()
    ///     .uptime(true)
    ///     .build();
    /// info!("hello world");
    /// # }
    ///
    /// ```
    /// # Errors
    ///
    /// This function will return an error if a global Logger was aready initialized. This module
    /// uses the [tracing] crate for logging, so if a [tracing] logger is initialized elsewhere,
    /// this method will error.
    pub fn build(self) -> Result<Logger> {
        // 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(self.display_level)
            .with_max_level(self.max_level)
            .with_ansi(self.ansi)
            .with_target(self.display_target)
            .with_file(self.display_filename)
            .with_thread_ids(self.display_thread_ids)
            .with_line_number(self.display_line_number)
            .with_thread_names(self.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 (self.log_to_file, self.show_time, self.pretty, self.uptime) {
            (true, true, true, true) => {
                let subscriber = subscriber
                    .with_writer(new_file_appender(self.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(self.log_dir))
                    .pretty()
                    .finish();
                tracing::subscriber::set_global_default(subscriber)?;
            }
            (true, false, true, _) => {
                let subscriber = subscriber
                    .with_writer(new_file_appender(self.log_dir))
                    .without_time()
                    .pretty()
                    .finish();
                tracing::subscriber::set_global_default(subscriber)?;
            }
            (true, true, false, true) => {
                let subscriber = subscriber
                    .with_writer(new_file_appender(self.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(self.log_dir))
                    .finish();
                tracing::subscriber::set_global_default(subscriber)?;
            }
            (true, false, false, _) => {
                let file_appender = tracing_appender::rolling::daily(self.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 {})
    }

    /// enable or disable logging to and creating of logfiles
    pub fn log_to_file(mut self, log_to_file: bool) -> Self {
        self.log_to_file = log_to_file;
        self
    }

    /// set a directory where logfiles would be created in
    ///
    /// Enable or disable creation and logging to logfiles with [log_to_file](Self::log_to_file).
    ///
    /// The default logdir is [DEFAULT_LOG_DIR].
    pub fn log_dir(mut self, log_dir: PathBuf) -> Self {
        self.log_dir = log_dir;
        self
    }

    /// enable or disable ANSI control sequences
    ///
    /// Disabling ANSI control sequences might improve compatibility and readability when the logs
    /// are displayed by a program that does not interpret them.
    ///
    /// Keeping ANSI control sequences enabled has the disadvantage of added colors for the logs.
    pub fn ansi(mut self, ansi: bool) -> Self {
        self.ansi = ansi;
        self
    }

    /// when making a log, display the source file in which a log was crated in
    pub fn display_filename(mut self, display_filename: bool) -> Self {
        self.display_filename = display_filename;
        self
    }

    /// when making a log, display the log level of the message
    pub fn display_level(mut self, display_level: bool) -> Self {
        self.display_level = display_level;
        self
    }

    /// show target context
    pub fn display_target(mut self, display_target: bool) -> Self {
        self.display_target = display_target;
        self
    }

    /// set the maximum verbosity level.
    pub fn max_level(mut self, max_level: Level) -> Self {
        self.max_level = max_level;
        self
    }

    /// show the id of the thread that created this message
    pub fn display_thread_ids(mut self, display_thread_ids: bool) -> Self {
        self.display_thread_ids = display_thread_ids;
        self
    }

    /// show the name of the thread that created this message
    pub fn display_thread_names(mut self, display_thread_names: bool) -> Self {
        self.display_thread_names = display_thread_names;
        self
    }

    /// show which line in the source file produces a log
    pub fn display_line_number(mut self, display_line_number: bool) -> Self {
        self.display_line_number = display_line_number;
        self
    }

    /// splits a log over multiple lines, looks like a python traceback
    pub fn pretty(mut self, pretty: bool) -> Self {
        self.pretty = pretty;
        self
    }

    /// show a timestamp describing when the log was created
    pub fn show_time(mut self, show_time: bool) -> Self {
        self.show_time = show_time;
        self
    }

    /// show timestamps as uptime (duration since the logger was initialized)
    pub fn uptime(mut self, uptime: bool) -> Self {
        self.uptime = uptime;
        self
    }
}

impl Default for LoggerBuilder {
    fn default() -> Self {
        Self {
            log_to_file: false,
            log_dir: PathBuf::from(DEFAULT_LOG_DIR),
            ansi: true,
            display_filename: false,
            display_level: true,
            display_target: false,
            max_level: DEFAULT_LOG_LEVEL,
            display_thread_ids: false,
            display_thread_names: false,
            display_line_number: false,
            pretty: false,
            show_time: true,
            uptime: false,
        }
    }
}

/// ## Logger for [`pt`](libpt)
///
/// A logger is generally a functionality that let's you write information from your library or
/// application in a more structured manner than if you just wrote all information to `stdout` or
/// `stderr` with the likes of `println!` or `eprintln!`.
///
/// It offers writing to multiple targets, such as both the terminal and a log file, and allows
/// users to choose the verbosity of the information that gets printed by selecting a
/// [Loglevel](Level).
///
/// ## Levels
///
/// TODO: add levels desc and ascii art
///
/// ## Usage
///
/// You don't need to use the [Logger] struct, it's better to use the macros instead:
///
/// * `error!`
/// * `warn!`
/// * `info!`
/// * `debug!`
/// * `trace!`
///
/// You can however use the [Logger] struct in cases where usage of a macro is bad or
/// you are somehow working with multiple loggers. The macros offer additional functionalities,
/// suck as full `format!` support and context, see [`tracing`], which we use as backend.
///
/// ## Examples
///
/// ```
/// # use libpt_log::{Logger, info};
/// # fn main() {
/// Logger::builder()
///     .uptime(true)
///     .build();
/// info!("hello world");
/// # }
///
/// ```
pub struct Logger;

/// ## Main implementation
impl Logger {
    /// Get a new [LoggerBuilder]
    pub fn builder() -> LoggerBuilder {
        LoggerBuilder::default()
    }

    /// ## initializes the logger
    ///
    /// Will enable the logger to be used.
    ///
    /// Assumes some defaults, use [`init_customized`](Self::init_customized) for more control
    #[deprecated(since = "0.4.1", note = "use Logger::builder() instead")]
    pub fn build(log_dir: Option<PathBuf>, max_level: Option<Level>, uptime: bool) -> Result<Self> {
        #[allow(deprecated)]
        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
    #[deprecated(since = "0.4.1", note = "use Logger::builder() instead")]
    pub fn build_mini(max_level: Option<Level>) -> Result<Self> {
        #[allow(deprecated)]
        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,
        )
    }

    /// ## initializes the logger
    ///
    /// Will enable the logger to be used.
    #[deprecated(since = "0.4.1", note = "use Logger::builder() instead")]
    #[allow(clippy::too_many_arguments)]
    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)
        )
    }
}

impl Default for Logger {
    fn default() -> Self {
        LoggerBuilder::default()
            .build()
            .expect("building a Logger failed")
    }
}

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
}