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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
//! A log thread and thread-local log proxy combination.
//!
//! This module provides logging functionality to run a dedicated log thread in
//! combination with one or more thread-local log proxy instances. The log
//! thread provides the endpoint used by the log proxy instances to send their
//! log records. Log proxy instances run in different threads or child
//! processes.
//!
//! # Usage
//!
//! Start by spawning a [`LogThread`] from the main thread. Next, initialize a
//! [`LogProxy`] instance per thread or child process. A log [`LogRecord`] can be
//! generated using the provided [`macros`]. The thread-local [`LogProxy`]
//! forwards the records to the [`LogThread`] for logging.
//!
//! ## LogThread
//!
//! The [`LogThread`] is the sink for all log [`Records`]. It can output log
//! records to Standard Error output and invoke a [`LogCallback`] function.
//! Both these options can be enabled by setting the corresponding
//! [`LogLevelFilter`] above [`LogLevelFilter::Off`]. Incoming log [`Records`]
//! are forwarded to Standard Error output or to the [`LogCallback`] function
//! if their [`Loglevel`] is equal or below the configured [`LogLevelFilter`].
//!
//! ## LogCallback
//!
//! A [`LogThread`] can invoke a [`LogCallback`] function for incoming records.
//! This is enabled by passing a [`LogCallback`] (with a [`LoglevelFilter`]
//! bigger than [`LogLevelFilter::Off`]) to the [`callback`] argument of the
//! [`spawn`] method of [`LogThread`].
//!
//! ## LogProxy
//!
//! A [`LogProxy`] forwards log [`Records`] to a [`LogThread`]. It logs records
//! with it's logger name if the generated log [`LogRecord`] [`Loglevel`] is
//! smaller or equal than the configured [`LoglevelFilter`] of the
//! [`LogProxy`].
//!
//! ## TeeFile
//!
//! A [`TeeFile`] forwards log [`Records`] to a file. It logs records with it's
//! logger name if the generated log [`LogRecord`] [`Loglevel`] is smaller or
//! equal than the configured [`LoglevelFilter`] of the [`TeeFile`].
//!
//! # Basic Example
//!
//! ```rust
//! use dqcsim::{
//!     debug,
//!     common::log::{init, proxy::LogProxy, thread::LogThread, LoglevelFilter},
//!     note,
//! };
//!
//! // Spawn the log thread. This starts a thread-local log proxy in the main
//! // thread with a Note level filter and "main_thread" as name. This example
//! // enables Standard Error output at Debug level filter.
//! let log_thread = LogThread::spawn(
//!     "main_thread",
//!     LoglevelFilter::Note,
//!     LoglevelFilter::Debug,
//!     None,
//!     vec![]
//! )
//! .unwrap();
//!
//! // Grab a copy of the log thread sender to use in the log proxy.
//! let log_endpoint = log_thread.get_sender();
//!
//! // Spawn an other thread.
//! std::thread::spawn(move || {
//!     // Construct a log proxy instance which connects to the log thread endpoint.
//!     let log_proxy = LogProxy::boxed("other_thread", LoglevelFilter::Trace, log_endpoint);
//!
//!     // Initialize the thread-local logger to enable forwarding of log records to
//!     // the log thread.
//!     init(vec![log_proxy]);
//!
//!     // Generate a log record
//!     note!("Note from thread via proxy");
//! })
//! .join();
//!
//! // This log records is also forwarded to the log thread by the log proxy running
//! // in the main thread.
//! debug!("Note from main thread via proxy started by log_thread spawn function");
//!
//! ```
//!
//! # Inspired by
//! * [`log`]
//! * sfackler's [comment](https://github.com/rust-lang-nursery/log/issues/57#issuecomment-143383896)
//!
//! [`LogThread`]: ./thread/struct.LogThread.html
//! [`spawn`]: ./thread/struct.LogThread.html#method.spawn
//! [`LogProxy`]: ./proxy/struct.LogProxy.html
//! [`TeeFile`]: ./tee_file/struct.TeeFile.html
//! [`LogRecord`]: ./struct.LogRecord.html
//! [`Records`]: ./struct.LogRecord.html
//! [`Loglevel`]: ./enum.Loglevel.html
//! [`LoglevelFilter`]: ./enum.LoglevelFilter.html
//! [`LogLevelFilter::Off`]: ./enum.LogLevelFilter.html
//! [`LogCallback`]: ../configuration/struct.LogCallback.html
//! [`macros`]: ../index.html#macros
//! [`log`]: https://github.com/rust-lang-nursery/log

// This re-export is required as the trait needs to be in scope in the log
// macro.
#[doc(hidden)]
pub use ref_thread_local as _ref_thread_local;

pub mod callback;
pub mod proxy;
pub mod stdio;
pub mod tee_file;
pub mod thread;

use crate::common::{
    channel::Sender,
    error,
    error::{ErrorKind, ResultExt},
};
use lazy_static::lazy_static;
use ref_thread_local::ref_thread_local;
use serde::{Deserialize, Serialize};
use std::{cell::RefCell, fmt};
use strum_macros::{Display, EnumIter, EnumString};

/// The Log trait.
///
/// # Implementing the Log trait.
///
/// ```rust
/// use dqcsim::{
///     common::log::{Log, Loglevel, LogRecord}
/// };
///
/// struct SimpleLogger {}
///
/// impl Log for SimpleLogger {
///     fn name(&self) -> &str {
///         "simple_logger"
///     }
///
///     fn enabled(&self, level: Loglevel) -> bool {
///         // The SimpleLogger is always enabled.
///         true
///     }
///
///     fn log(&self, record: &LogRecord) {
///         // The SimpleLogger logs to Standard Output.
///         println!("{}", record);
///     }
/// }
/// ```
pub trait Log {
    /// Returns the name of this logger
    fn name(&self) -> &str;
    /// Returns true if the provided loglevel is enabled
    fn enabled(&self, level: Loglevel) -> bool;
    /// Log the incoming record
    fn log(&self, record: &LogRecord);
}

thread_local! {
    /// The thread-local loggers.
    pub static LOGGERS: RefCell<Option<Vec<Box<dyn Log>>>> = RefCell::new(None);
}

lazy_static! {
    // Cache the process id.
    #[doc(hidden)]
    pub static ref PID: u32 = std::process::id();
}

ref_thread_local! {
    // Cache the thread id.
    #[doc(hidden)]
    // Don't ask. (rust-lang/rust #52780)
    pub static managed TID: u64 = unsafe { std::mem::transmute(std::thread::current().id()) };
}

/// Loglevel for log records.
#[derive(
    Copy,
    Clone,
    Debug,
    Eq,
    PartialEq,
    PartialOrd,
    Serialize,
    Deserialize,
    EnumString,
    Display,
    EnumIter,
)]
pub enum Loglevel {
    /// This loglevel is to be used for reporting a fatal error, resulting from
    /// the owner of the logger getting into an illegal state from which it
    /// cannot recover. Such problems are also reported to the API caller via
    /// Result::Err if applicable.
    Fatal = 1,

    /// This loglevel is to be used for reporting or propagating a non-fatal
    /// error caused by the API caller doing something wrong. Such problems are
    /// also reported to the API caller via Result::Err if applicable.
    Error,

    /// This loglevel is to be used for reporting that a called API/function is
    /// telling us we did something wrong (that we weren't expecting), but we
    /// can recover. For instance, for a failed connection attempt to something
    /// that really should not be failing, we can still retry (and eventually
    /// report critical or error if a retry counter overflows). Since we're
    /// still trying to rectify things at this point, such problems are NOT
    /// reported to the API/function caller via Result::Err.
    Warn,

    /// This loglevel is to be used for reporting information specifically
    /// requested by the user/API caller, such as the result of an API function
    /// requested through the command line, or an explicitly captured
    /// stdout/stderr stream.
    Note,

    /// This loglevel is to be used for reporting information NOT specifically
    /// requested by the user/API caller, such as a plugin starting up or
    /// shutting down.
    Info,

    /// This loglevel is to be used for reporting debugging information useful
    /// for debugging the user of the API provided by the logged instance.
    Debug,

    /// This loglevel is to be used for reporting debugging information useful
    /// for debugging the internals of the logged instance. Such messages would
    /// normally only be generated by debug builds, to prevent them from
    /// impacting performance under normal circumstances.
    Trace,
}

impl Into<term::color::Color> for Loglevel {
    fn into(self) -> term::color::Color {
        match self {
            Loglevel::Fatal => term::color::BRIGHT_RED,
            Loglevel::Error => term::color::RED,
            Loglevel::Warn => term::color::YELLOW,
            Loglevel::Note => term::color::WHITE,
            Loglevel::Info => term::color::BLUE,
            Loglevel::Debug => term::color::CYAN,
            Loglevel::Trace => term::color::BRIGHT_BLACK,
        }
    }
}

/// LoglevelFilter for implementors of the Log trait.
#[derive(
    Copy,
    Clone,
    Debug,
    Eq,
    PartialEq,
    PartialOrd,
    Serialize,
    Deserialize,
    EnumString,
    Display,
    EnumIter,
)]
pub enum LoglevelFilter {
    /// A level lower than all log levels.
    #[strum(to_string = "Off", serialize = "off", serialize = "o")]
    Off = 0,
    /// Corresponds to the `Fatal` log level.
    #[strum(to_string = "Fatal", serialize = "fatal", serialize = "f")]
    Fatal,
    /// Corresponds to the `Error` log level.
    #[strum(to_string = "Error", serialize = "error", serialize = "e")]
    Error,
    /// Corresponds to the `Warn` log level.
    #[strum(to_string = "Warn", serialize = "warn", serialize = "w")]
    Warn,
    /// Corresponds to the `Note` log level.
    #[strum(to_string = "Note", serialize = "note", serialize = "n")]
    Note,
    /// Corresponds to the `Info` log level.
    #[strum(to_string = "Info", serialize = "info", serialize = "i")]
    Info,
    /// Corresponds to the `Debug` log level.
    #[strum(to_string = "Debug", serialize = "debug", serialize = "d")]
    Debug,
    /// Corresponds to the `Trace` log level.
    #[strum(to_string = "Trace", serialize = "trace", serialize = "t")]
    Trace,
}

#[derive(Debug)]
pub struct NoLoglevel;

impl fmt::Display for NoLoglevel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "No suitable Loglevel for LoglevelFilter::Off")
    }
}

impl std::error::Error for NoLoglevel {}

impl Loglevel {
    /// Attempt to convert a LoglevelFilter to a Loglevel.
    ///
    /// Until std::convert::TryFrom is stable. (rust-lang/rust #33417)
    pub fn try_from(levelfilter: LoglevelFilter) -> Result<Loglevel, NoLoglevel> {
        match levelfilter {
            LoglevelFilter::Fatal => Ok(Loglevel::Fatal),
            LoglevelFilter::Error => Ok(Loglevel::Error),
            LoglevelFilter::Warn => Ok(Loglevel::Warn),
            LoglevelFilter::Note => Ok(Loglevel::Note),
            LoglevelFilter::Info => Ok(Loglevel::Info),
            LoglevelFilter::Debug => Ok(Loglevel::Debug),
            LoglevelFilter::Trace => Ok(Loglevel::Trace),
            LoglevelFilter::Off => Err(NoLoglevel),
        }
    }
}

impl From<Loglevel> for LoglevelFilter {
    fn from(level: Loglevel) -> LoglevelFilter {
        match level {
            Loglevel::Fatal => LoglevelFilter::Fatal,
            Loglevel::Error => LoglevelFilter::Error,
            Loglevel::Warn => LoglevelFilter::Warn,
            Loglevel::Note => LoglevelFilter::Note,
            Loglevel::Info => LoglevelFilter::Info,
            Loglevel::Debug => LoglevelFilter::Debug,
            Loglevel::Trace => LoglevelFilter::Trace,
        }
    }
}

/// Log record metadata.
///
/// The log metadata attached to a [`LogRecord`].
///
/// [`LogRecord`]: ./struct.LogRecord.html
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Metadata {
    /// Loglevel of the log record.
    level: Loglevel,
    module_path: Option<String>,
    file: Option<String>,
    line: Option<u32>,
    timestamp: std::time::SystemTime,
    process: u32,
    thread: u64,
}

/// A log record.
///
/// A log record consists of some metadata and a payload.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct LogRecord {
    payload: String,
    metadata: Metadata,
    logger: String,
}

impl LogRecord {
    pub fn payload(&self) -> &str {
        &self.payload
    }
    pub fn level(&self) -> Loglevel {
        self.metadata.level
    }
    pub fn module_path(&self) -> Option<&str> {
        self.metadata.module_path.as_deref()
    }
    pub fn file(&self) -> Option<&str> {
        self.metadata.file.as_deref()
    }
    pub fn line(&self) -> Option<u32> {
        self.metadata.line
    }
    pub fn timestamp(&self) -> std::time::SystemTime {
        self.metadata.timestamp
    }
    pub fn process(&self) -> u32 {
        self.metadata.process
    }
    pub fn thread(&self) -> u64 {
        self.metadata.thread
    }
    pub fn logger(&self) -> &str {
        self.logger.as_str()
    }
}

impl LogRecord {
    #[cfg_attr(feature = "cargo-clippy", allow(clippy::too_many_arguments))]
    pub fn new(
        logger: impl Into<String>,
        payload: impl Into<String>,
        level: Loglevel,
        module_path: impl Into<String>,
        file: impl Into<String>,
        line: u32,
        process: u32,
        thread: u64,
    ) -> LogRecord {
        LogRecord {
            payload: payload.into(),
            metadata: Metadata {
                level,
                module_path: Some(module_path.into()),
                file: Some(file.into()),
                line: Some(line),
                timestamp: std::time::SystemTime::now(),
                process,
                thread,
            },
            logger: logger.into(),
        }
    }
}

impl fmt::Display for LogRecord {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            humantime::format_rfc3339_seconds(self.metadata.timestamp)
        )?;
        write!(
            f,
            "{:<6}",
            format!(
                "+{}ms",
                self.metadata.timestamp.elapsed().unwrap().as_millis()
            )
        )?;
        write!(f, "{:>5} ", format!("{}", self.metadata.level))?;
        write!(
            f,
            "{:<32} ",
            format!(
                "{:>5}:{:<2} {} ",
                self.metadata.process, self.metadata.thread, self.logger,
            )
        )?;
        write!(f, "{}", self.payload)
    }
}

/// Update the thread-local loggers.
fn update(loggers: Option<Vec<Box<dyn Log>>>) -> error::Result<()> {
    LOGGERS.with(|x| {
        let mut x = x.try_borrow_mut().context(ErrorKind::LogError(
            "Unable to update thread-local loggers".to_string(),
        ))?;
        *x = loggers;
        Ok(())
    })
}

/// Initialize the thread-local loggers.
pub fn init(loggers: Vec<Box<dyn Log>>) -> error::Result<()> {
    update(Some(loggers))
}

/// Deinitialize the thread-local loggers.
pub fn deinit() -> error::Result<()> {
    update(None)
}

#[macro_export]
macro_rules! log {
    (? target: $target:expr, location: ($file:expr, $line:expr), $lvl:expr, $($arg:tt)+) => ({
        use $crate::common::log::_ref_thread_local::RefThreadLocal;
        $crate::common::log::LOGGERS.try_with(|loggers| {
            if let Some(ref loggers) = *loggers.borrow() {
                loggers.iter().for_each(|logger| {
                    if logger.enabled($lvl) {
                        logger.log(&$crate::common::log::LogRecord::new(
                            logger.name(),
                            format!($($arg)+),
                            $lvl,
                            $target,
                            $file,
                            $line,
                            *$crate::common::log::PID,
                            *$crate::common::log::TID.borrow()
                        ));
                    }
                });
                true
            } else {
                false
            }
        }).unwrap_or(false)
    });
    (target: $target:expr, location: ($file:expr, $line:expr), $lvl:expr, $($arg:tt)+) => (
        {
            $crate::log!(?
                target: module_path!(),
                location: ($file, $line),
                $lvl, $($arg)+
            );
        }
    );
    (target: $target:expr, $lvl:expr, $($arg:tt)+) => (
        $crate::log!(
            target: module_path!(),
            location: (file!(), line!()),
            $lvl, $($arg)+
        )
    );
    ($lvl:expr, $($arg:tt)+) => (
        $crate::log!(
            target: module_path!(),
            $lvl, $($arg)+
        )
    )
}

#[macro_export]
macro_rules! fatal {
    (target: $target:expr, $($arg:tt)+) => (
        $crate::log!(target: $target, $crate::common::log::Loglevel::Fatal, $($arg)+);
    );
    ($($arg:tt)+) => (
        $crate::log!($crate::common::log::Loglevel::Fatal, $($arg)+);
    )
}

#[macro_export]
macro_rules! error {
    (target: $target:expr, $($arg:tt)+) => (
        $crate::log!(target: $target, $crate::common::log::Loglevel::Error, $($arg)+);
    );
    ($($arg:tt)+) => (
        $crate::log!($crate::common::log::Loglevel::Error, $($arg)+);
    )
}

#[macro_export]
macro_rules! warn {
    (target: $target:expr, $($arg:tt)+) => (
        $crate::log!(target: $target, $crate::common::log::Loglevel::Warn, $($arg)+);
    );
    ($($arg:tt)+) => (
        $crate::log!($crate::common::log::Loglevel::Warn, $($arg)+);
    )
}

#[macro_export]
macro_rules! note {
    (target: $target:expr, $($arg:tt)+) => (
        $crate::log!(target: $target, $crate::common::log::Loglevel::Note, $($arg)+);
    );
    ($($arg:tt)+) => (
        $crate::log!($crate::common::log::Loglevel::Note, $($arg)+);
    )
}

#[macro_export]
macro_rules! info {
    (target: $target:expr, $($arg:tt)+) => (
        $crate::log!(target: $target, $crate::common::log::Loglevel::Info, $($arg)+);
    );
    ($($arg:tt)+) => (
        $crate::log!($crate::common::log::Loglevel::Info, $($arg)+);
    )
}

#[macro_export]
macro_rules! debug {
    (target: $target:expr, $($arg:tt)+) => (
        $crate::log!(target: $target, $crate::common::log::Loglevel::Debug, $($arg)+);
    );
    ($($arg:tt)+) => (
        $crate::log!($crate::common::log::Loglevel::Debug, $($arg)+);
    )
}

#[macro_export]
macro_rules! trace {
    (target: $target:expr, $($arg:tt)+) => (
        $crate::log!(target: $target, $crate::common::log::Loglevel::Trace, $($arg)+);
    );
    ($($arg:tt)+) => (
        $crate::log!($crate::common::log::Loglevel::Trace, $($arg)+);
    )
}

#[cfg(test)]
mod tests {
    use super::{LogRecord, Loglevel, LoglevelFilter};

    #[test]
    fn level_order() {
        assert!(Loglevel::Debug < Loglevel::Trace);
        assert!(Loglevel::Info < Loglevel::Debug);
        assert!(Loglevel::Note < Loglevel::Info);
        assert!(Loglevel::Warn < Loglevel::Note);
        assert!(Loglevel::Error < Loglevel::Warn);
        assert!(Loglevel::Fatal < Loglevel::Error);
        assert!(LoglevelFilter::Off < LoglevelFilter::from(Loglevel::Fatal));
    }

    #[test]
    fn level_colors() {
        let color: term::color::Color = Loglevel::Error.into();
        assert_eq!(color, term::color::RED);

        let color: term::color::Color = Loglevel::Note.into();
        assert_eq!(color, term::color::WHITE);
    }

    #[test]
    fn filter_to_level() {
        assert!(Loglevel::try_from(LoglevelFilter::Fatal).is_ok());
        assert!(Loglevel::try_from(LoglevelFilter::Error).is_ok());
        assert!(Loglevel::try_from(LoglevelFilter::Warn).is_ok());
        assert!(Loglevel::try_from(LoglevelFilter::Note).is_ok());
        assert!(Loglevel::try_from(LoglevelFilter::Info).is_ok());
        assert!(Loglevel::try_from(LoglevelFilter::Debug).is_ok());
        assert!(Loglevel::try_from(LoglevelFilter::Trace).is_ok());
        assert!(Loglevel::try_from(LoglevelFilter::Off).is_err());
    }

    #[test]
    fn log_record_debug_getters() {
        let record = LogRecord::new("", "", Loglevel::Debug, "path", "file", 1234u32, 1u32, 1u64);
        assert_eq!(record.module_path(), Some("path"));
        assert_eq!(record.file(), Some("file"));
        assert_eq!(record.line(), Some(1234u32));
    }

    #[test]
    fn display_record() {
        let record = LogRecord::new(
            "logger",
            "message",
            Loglevel::Trace,
            "path",
            "file",
            1234u32,
            1u32,
            1u64,
        );
        assert_eq!(
            &format!("{}", record).as_str()[24..],
            "  Trace     1:1  logger                  message"
        );
    }
}