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
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
Filename : lib.rs

Copyright (C) 2021 CJ McAllister
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA

Purpose:
    This library provides a multi-threaded, global logger.

    All logging actions occur in the logging thread, leaving the main thread
    free to do all the cool stuff it wants to do!

\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#![warn(missing_docs)]
#![cfg_attr(not(doctest), doc = include_str!("../README.md"))]

use std::error::Error;
use std::fmt;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::mpsc::{self, RecvError, SendError};
use std::sync::Arc;
use std::thread;

use chrono::DateTime;
use chrono::Local;
use once_cell::sync::OnceCell;


///////////////////////////////////////////////////////////////////////////////
//  Named Constants
///////////////////////////////////////////////////////////////////////////////

// Buffer size of the sync_channel for sending log messages
const CHANNEL_SIZE: usize = 512;


///////////////////////////////////////////////////////////////////////////////
//  Module Declarations
///////////////////////////////////////////////////////////////////////////////

#[doc(hidden)]
pub mod sender;
use self::sender::Sender;

#[doc(hidden)]
pub mod receiver;
use self::receiver::Receiver;


///////////////////////////////////////////////////////////////////////////////
//  Data Structures
///////////////////////////////////////////////////////////////////////////////

/// Denotes the level or severity of the log message.
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub enum Level {
    /// For tracing code paths. Mean to be very verbose.
    Trace = 0x01,
    /// For debugging and troubleshooting
    Debug = 0x02,
    /// For harmless but useful information
    Info = 0x04,
    /// For cosmetic/recoverable errors
    Warning = 0x08,
    /// For Major/unrecoverable errors
    Error = 0x10,
    /// For Very Bad News™
    Fatal = 0x20,
}

#[doc(hidden)]
/// Tuple struct containing log message and its log level
pub struct MsgTuple {
    pub timestamp: DateTime<Local>,
    pub level: Level,
    pub fn_name: String,
    pub line: u32,
    pub msg: String,
}

/// Specifies which stream(s) log messages should be written to.
#[derive(Debug, Copy, Clone)]
pub enum OutputStream {
    /// Don't write to either stream, i.e., disable logging
    Neither = 0x0,
    /// Write only to StdOut
    StdOut = 0x1,
    /// Write only to a file
    File = 0x2,
    /// Write to both StdOut and a File
    Both = 0x3,
}

#[doc(hidden)]
/// Enumeration of commands that the logging thread will handle
pub enum Command {
    LogMsg(MsgTuple),
    SetOutputLevel(Level),
    SetOutputStream(OutputStream),
    Flush(mpsc::Sender<()>),
}

#[doc(hidden)]
#[derive(Clone, Debug)]
pub struct MtLogger {
    enabled: bool,
    sender: Sender,
    msg_count: Arc<AtomicU64>,
}

/// Logging errors
#[derive(Debug)]
pub enum MtLoggerError {
    /// A logging command was attempted before the global logger instance was initialized with [`mt_new!`]
    LoggerNotInitialized,

    /* Wrappers */
    /// Wrapper for `SendError<>`
    SendError(SendError<Command>),
    /// Wrapper for `RecvError`
    RecvError(RecvError),
}

#[doc(hidden)]
pub static INSTANCE: OnceCell<MtLogger> = OnceCell::new();


///////////////////////////////////////////////////////////////////////////////
//  Object Implementation
///////////////////////////////////////////////////////////////////////////////

impl MtLogger {
    /// Fully-qualified constructor
    pub fn new(
        logfile_prefix: &'static str,
        output_level: Level,
        output_stream: OutputStream,
    ) -> Self {
        // Create the log messaging and control channel
        // Must be a sync channel in order to wrap OnceCell around an MtLogger
        let (logger_tx, logger_rx) = mpsc::sync_channel::<Command>(CHANNEL_SIZE);

        // Create the shared message count
        let msg_count = Arc::new(AtomicU64::new(0));

        // Initialize receiver struct, build and spawn thread
        let mut log_receiver = Receiver::new(
            logfile_prefix,
            logger_rx,
            output_level,
            output_stream,
            Arc::clone(&msg_count),
        );
        thread::Builder::new()
            .name("log_receiver".to_string())
            .spawn(move || log_receiver.main())
            .unwrap();

        // Initialize sender struct
        let log_sender = Sender::new(logger_tx);

        Self {
            enabled: true,
            sender: log_sender,
            msg_count,
        }
    }


    /*  *  *  *  *  *  *  *\
     *  Accessor Methods  *
    \*  *  *  *  *  *  *  */

    #[doc(hidden)]
    pub fn msg_count(&self) -> u64 {
        self.msg_count.load(Ordering::SeqCst)
    }


    /*  *  *  *  *  *  *  *\
     *   Utility Methods  *
    \*  *  *  *  *  *  *  */

    #[doc(hidden)]
    //FEAT: Bring filtering back to the sending-side
    pub fn log_msg(
        &self,
        timestamp: DateTime<Local>,
        level: Level,
        fn_name: String,
        line: u32,
        msg: String,
    ) -> Result<(), SendError<Command>> {
        // If logging is enabled, package log message into tuple and send
        if self.enabled {
            let log_tuple = MsgTuple {
                timestamp,
                level,
                fn_name,
                line,
                msg,
            };
            self.sender.send_log(Command::LogMsg(log_tuple))
        } else {
            Ok(())
        }
    }

    #[doc(hidden)]
    pub fn log_cmd(&self, cmd: Command) -> Result<(), SendError<Command>> {
        if self.enabled {
            self.sender.send_cmd(cmd)
        } else {
            Ok(())
        }
    }

    #[doc(hidden)]
    pub fn flush(&self) -> Result<(), MtLoggerError> {
        // Create a channel that will be used to notify completion of the flush
        let (flush_ack_tx, flush_ack_rx) = mpsc::channel::<()>();

        // Send a flush command to the receiver thread
        self.sender.send_cmd(Command::Flush(flush_ack_tx))?;

        // Block until the the flush ACK arrives
        flush_ack_rx.recv()?;

        Ok(())
    }
}


///////////////////////////////////////////////////////////////////////////////
//  Static Functions
///////////////////////////////////////////////////////////////////////////////

#[doc(hidden)]
// Wrapper around chrono::Local::now() to avoid dependency issues with using external crate functions in macros
pub fn mt_now() -> DateTime<Local> {
    Local::now()
}


///////////////////////////////////////////////////////////////////////////////
//  Trait Implementations
///////////////////////////////////////////////////////////////////////////////

/*  *  *  *  *  *  *  *\
 *       Level        *
\*  *  *  *  *  *  *  */

impl fmt::Display for Level {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Trace => write!(f, "TRACE"),
            Self::Debug => write!(f, "DEBUG"),
            Self::Info => write!(f, "INFO"),
            Self::Warning => write!(f, "WARNING"),
            Self::Error => write!(f, "ERROR"),
            Self::Fatal => write!(f, "FATAL"),
        }
    }
}


/*  *  *  *  *  *  *  *\
 *    MtLoggerError   *
\*  *  *  *  *  *  *  */

impl Error for MtLoggerError {}

impl fmt::Display for MtLoggerError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::LoggerNotInitialized => {
                write!(
                    f,
                    "Attempted a command before the logger instance was initialized"
                )
            }

            // Wrappers
            Self::SendError(send_err) => {
                write!(
                    f,
                    "Encountered SendError '{}' while performing a logger command",
                    send_err
                )
            }
            Self::RecvError(recv_err) => {
                write!(
                    f,
                    "Encountered RecvError '{}' while performing a logger command",
                    recv_err
                )
            }
        }
    }
}

impl From<SendError<Command>> for MtLoggerError {
    fn from(src: SendError<Command>) -> Self {
        Self::SendError(src)
    }
}
impl From<RecvError> for MtLoggerError {
    fn from(src: RecvError) -> Self {
        Self::RecvError(src)
    }
}


///////////////////////////////////////////////////////////////////////////////
//  Macro Definitions
///////////////////////////////////////////////////////////////////////////////

/// Initializes the `mt_logger` global instance.
///
/// # Examples
///
/// Initialize the logger instance to log `Info`-level messages and higher to _both_ StdOut and a file.
/// The filename will be given the default prefix, see module-level documentation for full format.
/// ```
/// # #[macro_use] extern crate mt_logger;
/// # use mt_logger::{Level, OutputStream};
/// # fn main() {
/// mt_new!(None, Level::Info, OutputStream::Both);
/// # }
/// ```
///
/// Initialize the logger instance to log `Trace`-level messages and higher to a file _only_.
/// The filename will be given the specified prefix.
/// ```
/// # #[macro_use] extern crate mt_logger;
/// # use mt_logger::{Level, OutputStream};
/// # fn main() {
/// mt_new!(Some("my_app_v2.3"), Level::Trace, OutputStream::File);
/// # }
/// ```
#[macro_export]
macro_rules! mt_new {
    ($logfile_prefix:expr, $output_level:expr, $output_stream:expr) => {{
        // Use prefix if specified, or default to parent package name
        let prefix = match $logfile_prefix {
            Some(specified_prefix) => specified_prefix,
            None => env!("CARGO_PKG_NAME"),
        };

        let logger = $crate::MtLogger::new(prefix, $output_level, $output_stream);

        $crate::INSTANCE
            .set(logger)
            .expect("MtLogger INSTANCE already initialized");
    }};
}

/// Sends a message to be logged at the specified logging level.
///
/// Arguments after `$log_level` follow the format of [`println!`] arguments.
///
/// # Note
/// A call to this macro will only _send_ the message to the logging thread.
/// It does NOT guarantee that the message will be delivered at any time.
///
/// If all messages must be logged at a given time, see [`mt_flush!`].
///
/// # Examples
///
/// Logs a `Debug`-level message with the content, "No response received after 500ms".
/// ```
/// # #[macro_use] extern crate mt_logger;
/// # use mt_logger::Level;
/// # fn main() {
/// let timeout = 500;
/// mt_log!(Level::Debug, "No response received after {}ms", timeout);
/// # }
/// ```
#[macro_export]
macro_rules! mt_log {
    ($log_level:expr, $( $fmt_args:expr ),*) => {{
        // Take the timestamp first for highest accuracy
        let timestamp = $crate::mt_now();

        // Capture fully-qualified function name
        let fn_name = {
            fn f() {}
            fn type_name_of<T>(_: T) -> &'static str {
                std::any::type_name::<T>()
            }
            let name = type_name_of(f);
            &name[..name.len() - 3]
        };

        let msg_content: String = format!($( $fmt_args ),*);

        $crate::INSTANCE
            .get()
            // If None is encountered, the logger has not been initialized, so do nothing
            .and_then(|instance| instance.log_msg(
                timestamp,
                $log_level,
                fn_name.to_string(),
                line!(),
                msg_content)
                .ok()
            );
    }};
}

/// Sets the active stream to the specified [`OutputStream`].
///
/// # Examples
///
/// Set active stream to `StdOut`.
/// ```
/// # #[macro_use] extern crate mt_logger;
/// # use mt_logger::OutputStream;
/// # fn main() {
/// mt_stream!(OutputStream::StdOut);
/// # }
/// ```
///
/// Set active stream to `Neither`, i.e., disable logging.
/// ```
/// # #[macro_use] extern crate mt_logger;
/// # use mt_logger::OutputStream;
/// # fn main() {
/// mt_stream!(OutputStream::Neither);
/// # }
/// ```
#[macro_export]
macro_rules! mt_stream {
    ($output_stream:expr) => {{
        // Get the global instance and send a command to set the output stream
        $crate::INSTANCE
            .get()
            // If None is encountered, the logger has not been initialized, so do nothing
            .and_then(|instance| {
                instance
                    .log_cmd($crate::Command::SetOutputStream($output_stream))
                    .ok()
            });
    }};
}

/// Sets the minimum logging level to the specified [`Level`].
///
/// # Examples
///
/// Log all messages at `Debug`-level or higher.
/// ```
/// # #[macro_use] extern crate mt_logger;
/// # use mt_logger::Level;
/// # fn main() {
/// mt_level!(Level::Debug);
/// # }
/// ```
///
/// Log all messages at `Fatal`-level or higher, i.e., only log `Fatal`-level messages.
/// ```
/// # #[macro_use] extern crate mt_logger;
/// # use mt_logger::Level;
/// # fn main() {
/// mt_level!(Level::Fatal);
/// # }
/// ```
#[macro_export]
macro_rules! mt_level {
    ($output_level:expr) => {{
        // Get the global instance and send a command to set the output level
        $crate::INSTANCE
            .get()
            // If None is encountered, the logger has not been initialized, so do nothing
            .and_then(|instance| {
                instance
                    .log_cmd($crate::Command::SetOutputLevel($output_level))
                    .ok()
            });
    }};
}

/// Returns a count of _recorded_ log messages.
///
/// NOTE: This may not (and likely _is_ not, at any given time), the same as the
/// number of times [`mt_log!`] has been called. The message count is only incremented
/// after the logging thread has successfully written a message to the active
/// stream(s). Due to the nature of multithreading, this may happen at any time or never.
///
/// If a count of all successfully sent and recorded messages is required, [`mt_flush!`]
/// must be called before [`mt_count!`].
///
/// # Examples
///
/// Get count of recorded messages.
/// ```
/// # #[macro_use] extern crate mt_logger;
/// # use mt_logger::{Level, OutputStream};
/// # fn main() {
/// # mt_new!(None, Level::Info, OutputStream::Both);
/// let msg_count = mt_count!();
/// # }
/// ```
#[macro_export]
macro_rules! mt_count {
    () => {{
        // Get the global instance and retrieve the message count
        $crate::INSTANCE
            .get()
            // If None is encountered, the logger has not been initialized, which is an error
            .and_then(|instance| Some(instance.msg_count()))
            .unwrap()
    }};
}

/// Blocks the calling thread until all messages have been received by the logging thread.
///
/// Returns [`Result<(), MtLoggerError>`]
///
/// # Examples
///
/// Flush all sent messages.
/// ```
/// # #[macro_use] extern crate mt_logger;
/// # use mt_logger::{Level, MtLoggerError, OutputStream};
/// # fn main() -> Result<(), MtLoggerError> {

/// # mt_new!(None, Level::Info, OutputStream::Both);/// mt_log!(Level::Debug, "These");
/// mt_log!(Level::Debug, "messages");
/// mt_log!(Level::Debug, "may");
/// mt_log!(Level::Debug, "not");
/// mt_log!(Level::Debug, "have");
/// mt_log!(Level::Debug, "been");
/// mt_log!(Level::Debug, "received");
/// mt_log!(Level::Debug, "yet.");
///
/// mt_flush!()?;
/// // Now they have!
///
/// Ok(())
/// # }
/// ```
///
/// # Errors
///
/// As this function is effectively a send and blocking receive, it is possible for
/// either of these calls to fail, and those errors will propagate back to the caller.
///
/// See [`MtLoggerError`] for an enumeration of errors that may be returned.
#[macro_export]
macro_rules! mt_flush {
    () => {
        $crate::INSTANCE.get().map_or(
            // If None is encountered, the logger has not been initialized, just return an error
            Err($crate::MtLoggerError::LoggerNotInitialized),
            // If instance is initialized, allow all messages to flush to output
            |instance| instance.flush(),
        )
    };
}


///////////////////////////////////////////////////////////////////////////////
//  Unit Tests
///////////////////////////////////////////////////////////////////////////////

#[cfg(test)]
mod tests {
    use std::error::Error;
    use std::fs;
    use std::io::Read;
    use std::path::PathBuf;
    use std::sync::Mutex;
    use std::time;

    use lazy_static::lazy_static;

    use regex::Regex;

    use crate::receiver::{FILE_OUT_FILENAME, STDOUT_FILENAME};
    use crate::{Level, OutputStream, INSTANCE};


    type TestResult = Result<(), Box<dyn Error>>;


    lazy_static! {
        static ref LOGGER_MUTEX: Mutex<()> = Mutex::new(());
    }


    #[derive(Debug, PartialEq)]
    enum VerfFile {
        StdOut,
        FileOut,
    }

    const LOGFILE_PREFIX: Option<&'static str> = Some("TEST");

    const STDOUT_HDR_REGEX_STR: &str = r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{9}: \x1b\[(\d{3};\d{3}m)\[(\s*(\w*)\s*)\]\x1b\[0m (.*)\(\) line (\d*):";
    const STDOUT_COLOR_IDX: usize = 1;
    const STDOUT_PADDED_LEVEL_IDX: usize = 2;
    const STDOUT_PADLESS_LEVEL_IDX: usize = 3;
    const STDOUT_FN_NAME_IDX: usize = 4;
    const STDOUT_LINE_NUM_IDX: usize = 5;

    const FILE_OUT_HDR_REGEX_STR: &str =
        r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{9}: \[(\s*(\w*)\s*)\] (.*)\(\) line (\d*):";
    const FILE_OUT_PADDED_LEVEL_IDX: usize = 1;
    const FILE_OUT_PADLESS_LEVEL_IDX: usize = 2;
    const FILE_OUT_FN_NAME_IDX: usize = 3;
    const FILE_OUT_LINE_NUM_IDX: usize = 4;


    // Reset verification files, if they exist
    fn reset_verf_files() -> TestResult {
        let path_buf = PathBuf::from(STDOUT_FILENAME);
        if path_buf.as_path().exists() {
            fs::write(STDOUT_FILENAME, "")?;
        }

        let path_buf = PathBuf::from(FILE_OUT_FILENAME);
        if path_buf.as_path().exists() {
            fs::write(FILE_OUT_FILENAME, "")?;
        }

        Ok(())
    }

    fn format_verf_helper(
        verf_type: VerfFile,
        verf_string: String,
        first_line_num: u32,
    ) -> TestResult {
        // Set up the verification items
        const FN_NAME: &str = "mt_logger::tests::format_verification";
        const VERF_MATRIX: [[&str; 3]; 6] = [
            ["TRACE", "030;105m", "  TRACE  "],
            ["DEBUG", "030;106m", "  DEBUG  "],
            ["INFO", "030;107m", "  INFO   "],
            ["WARNING", "030;103m", " WARNING "],
            ["ERROR", "030;101m", "  ERROR  "],
            ["FATAL", "031;040m", "  FATAL  "],
        ];
        const LEVEL_VERF_IDX: usize = 0;
        const COLOR_VERF_IDX: usize = 1;
        const PADDED_LEVEL_VERF_IDX: usize = 2;

        // Set up output-specific parameters
        let padded_level_hdr_capture_idx;
        let fn_name_hdr_capture_idx;
        let line_num_hdr_capture_idx;
        let header_regex;
        match verf_type {
            VerfFile::StdOut => {
                padded_level_hdr_capture_idx = STDOUT_PADDED_LEVEL_IDX;
                fn_name_hdr_capture_idx = STDOUT_FN_NAME_IDX;
                line_num_hdr_capture_idx = STDOUT_LINE_NUM_IDX;
                header_regex = Regex::new(STDOUT_HDR_REGEX_STR)?;
            }
            VerfFile::FileOut => {
                padded_level_hdr_capture_idx = FILE_OUT_PADDED_LEVEL_IDX;
                fn_name_hdr_capture_idx = FILE_OUT_FN_NAME_IDX;
                line_num_hdr_capture_idx = FILE_OUT_LINE_NUM_IDX;
                header_regex = Regex::new(FILE_OUT_HDR_REGEX_STR)?;
            }
        }
        let level_content_capture_idx = 1;

        // Create regex for message content
        let content_regex = Regex::new(r"^   This is an? (\w*) message.")?;

        // Read verf string into iterator
        let mut verf_lines: Vec<&str> = verf_string.split('\n').collect();
        let mut verf_line_iter = verf_lines.iter_mut();

        // Iterate over lines, verifying along the way
        let mut i = 0;
        while let Some(header_line) = verf_line_iter.next().filter(|v| !v.is_empty()) {
            // Match regex against header line, and capture groups
            let header_captures = header_regex.captures(header_line).unwrap_or_else(|| {
                panic!(
                    "{:?}: Header line {} '{}' did not match Regex:\n   {}",
                    verf_type,
                    i,
                    header_line,
                    header_regex.as_str()
                )
            });

            // Verify capture groups
            if verf_type == VerfFile::StdOut
                && &header_captures[STDOUT_COLOR_IDX] != VERF_MATRIX[i][COLOR_VERF_IDX]
            {
                panic!(
                    "{:?}: Wrong color '{}' on line '{}', should be '{}'",
                    verf_type,
                    &header_captures[STDOUT_COLOR_IDX],
                    header_line,
                    VERF_MATRIX[i][COLOR_VERF_IDX]
                );
            }
            if &header_captures[padded_level_hdr_capture_idx]
                != VERF_MATRIX[i][PADDED_LEVEL_VERF_IDX]
            {
                panic!(
                    "{:?}: Wrong padded level '{}' on line '{}', should be '{}'",
                    verf_type,
                    &header_captures[padded_level_hdr_capture_idx],
                    header_line,
                    VERF_MATRIX[i][PADDED_LEVEL_VERF_IDX]
                );
            }
            if &header_captures[fn_name_hdr_capture_idx] != FN_NAME {
                panic!(
                    "{:?}: Wrong function name '{}' on line '{}', should be '{}'",
                    verf_type, &header_captures[fn_name_hdr_capture_idx], header_line, FN_NAME
                );
            }
            if header_captures[line_num_hdr_capture_idx].parse::<u32>()?
                != first_line_num + i as u32
            {
                panic!(
                    "{:?}: Wrong line number '{}' on line '{}', should be '{}'",
                    verf_type,
                    &header_captures[line_num_hdr_capture_idx],
                    header_line,
                    first_line_num + i as u32
                );
            }

            // Verify content line
            let content_line = verf_line_iter
                .next()
                .unwrap_or_else(|| panic!("Missing content line after header '{}'", header_line));
            let content_captures = content_regex.captures(content_line).unwrap_or_else(|| {
                panic!(
                    "{:?}: Content line {} '{}' did not match content Regex:\n   {}",
                    verf_type,
                    i,
                    content_line,
                    content_regex.as_str()
                )
            });

            if &content_captures[level_content_capture_idx] != VERF_MATRIX[i][LEVEL_VERF_IDX] {
                panic!(
                    "{:?}: Wrong level '{}' in content line '{}', should be '{}'",
                    verf_type,
                    &content_captures[level_content_capture_idx],
                    content_line,
                    VERF_MATRIX[i][LEVEL_VERF_IDX]
                )
            }

            i += 1;
        }

        Ok(())
    }

    #[test]
    fn format_verification() -> TestResult {
        // Lock logger mutex and hold it until we're done processing messages
        let mutex = LOGGER_MUTEX.lock()?;

        // Clean verification files before test
        reset_verf_files()?;

        // Create or update logger instance such that all messages are logged to Both outputs
        if INSTANCE.get().is_none() {
            mt_new!(LOGFILE_PREFIX, Level::Trace, OutputStream::Both);
        } else {
            mt_level!(Level::Trace);
            mt_stream!(OutputStream::Both);
        }

        let first_line_num = line!() + 1;
        mt_log!(Level::Trace, "This is a TRACE message.");
        mt_log!(Level::Debug, "This is a DEBUG message.");
        mt_log!(Level::Info, "This is an INFO message.");
        mt_log!(Level::Warning, "This is a WARNING message.");
        mt_log!(Level::Error, "This is an ERROR message.");
        mt_log!(Level::Fatal, "This is a FATAL message.");

        // Flush the messages to their output
        println!("Flushing all messages to their output...");
        let start_time = time::Instant::now();
        mt_flush!()?;
        println!("Done flushing after {}ms", start_time.elapsed().as_millis());

        // Capture the files in memory before releasing the mutex
        let mut verf_file_stdout = fs::OpenOptions::new().read(true).open(STDOUT_FILENAME)?;
        let mut verf_string_stdout = String::new();
        verf_file_stdout.read_to_string(&mut verf_string_stdout)?;
        let mut verf_file_file_out = fs::OpenOptions::new().read(true).open(FILE_OUT_FILENAME)?;
        let mut verf_string_file_out = String::new();
        verf_file_file_out.read_to_string(&mut verf_string_file_out)?;

        // Unlock the mutex
        std::mem::drop(mutex);

        // Verify that the verification files contain well-formatted messages
        format_verf_helper(VerfFile::StdOut, verf_string_stdout, first_line_num)?;
        format_verf_helper(VerfFile::FileOut, verf_string_file_out, first_line_num)?;

        Ok(())
    }

    fn outputstream_verf_helper(verf_type: VerfFile, verf_string: String) -> TestResult {
        // Set up the verification items
        const VERF_MATRIX: [[[&str; 2]; 4]; 2] = [
            [
                ["TRACE", "BOTH"],
                ["FATAL", "BOTH"],
                ["TRACE", "STDOUT"],
                ["FATAL", "STDOUT"],
            ],
            [
                ["TRACE", "BOTH"],
                ["FATAL", "BOTH"],
                ["TRACE", "FILEOUT"],
                ["FATAL", "FILEOUT"],
            ],
        ];
        const STDOUT_TYPE_IDX: usize = 0;
        const FILE_OUT_TYPE_IDX: usize = 1;
        const LEVEL_VERF_IDX: usize = 0;
        const OUTPUT_TYPE_VERF_IDX: usize = 1;

        // Set up output-specific parameters
        let verf_type_idx;
        let padless_level_hdr_capture_idx;
        let header_regex;
        match verf_type {
            VerfFile::StdOut => {
                verf_type_idx = STDOUT_TYPE_IDX;
                padless_level_hdr_capture_idx = STDOUT_PADLESS_LEVEL_IDX;
                header_regex = Regex::new(STDOUT_HDR_REGEX_STR)?;
            }
            VerfFile::FileOut => {
                verf_type_idx = FILE_OUT_TYPE_IDX;
                padless_level_hdr_capture_idx = FILE_OUT_PADLESS_LEVEL_IDX;
                header_regex = Regex::new(FILE_OUT_HDR_REGEX_STR)?;
            }
        }
        let output_type_capture_idx = 1;

        // Create regex for message content
        let content_regex = Regex::new(r"^\s*This message appears in (\w*).")?;

        // Read verf string into iterator
        let mut verf_lines: Vec<&str> = verf_string.split('\n').collect();
        let mut verf_line_iter = verf_lines.iter_mut();

        // Verify that the verification files contain the correct filter level and content lines
        let mut i = 0;
        while let Some(header_line) = verf_line_iter.next().filter(|v| !v.is_empty()) {
            // Verify header contains the correct log level
            let header_captures = header_regex.captures(header_line).unwrap_or_else(|| {
                panic!(
                    "{:?}: Header line {} '{}' did not match Regex:\n   {}",
                    verf_type,
                    i,
                    header_line,
                    header_regex.as_str()
                )
            });
            if &header_captures[padless_level_hdr_capture_idx]
                != VERF_MATRIX[verf_type_idx][i][LEVEL_VERF_IDX]
            {
                panic!(
                    "{:?}: Wrong level '{}' on line '{}', should be '{}'",
                    verf_type,
                    &header_captures[padless_level_hdr_capture_idx],
                    header_line,
                    VERF_MATRIX[verf_type_idx][i][LEVEL_VERF_IDX]
                );
            }

            // Verify content contains the correct output type
            let content_line = verf_line_iter
                .next()
                .unwrap_or_else(|| panic!("Missing content line after header '{}'", header_line));
            let content_captures = content_regex.captures(content_line).unwrap_or_else(|| {
                panic!(
                    "{:?}: Content line {} '{}' did not match content Regex:\n   {}",
                    verf_type,
                    i,
                    content_line,
                    content_regex.as_str()
                )
            });

            if &content_captures[output_type_capture_idx]
                != VERF_MATRIX[verf_type_idx][i][OUTPUT_TYPE_VERF_IDX]
            {
                panic!(
                    "{:?}: Wrong output type '{}' on line '{}', should be '{}'",
                    verf_type,
                    &content_captures[output_type_capture_idx],
                    content_line,
                    VERF_MATRIX[verf_type_idx][i][OUTPUT_TYPE_VERF_IDX]
                )
            }

            i += 1;
        }

        Ok(())
    }

    #[test]
    fn outputstream_verification() -> TestResult {
        // Lock logger mutex and hold it until we're done processing messages
        let mutex = LOGGER_MUTEX.lock()?;

        // Clean verification files before test
        reset_verf_files()?;

        // Create or update logger instance such that all messages are logged to Both outputs
        if INSTANCE.get().is_none() {
            mt_new!(LOGFILE_PREFIX, Level::Trace, OutputStream::Both);
        } else {
            mt_level!(Level::Trace);
            mt_stream!(OutputStream::Both);
        }

        mt_log!(Level::Trace, "This message appears in BOTH.");
        mt_log!(Level::Fatal, "This message appears in BOTH.");

        // Log messages to STDOUT only
        mt_stream!(OutputStream::StdOut);
        mt_log!(Level::Trace, "This message appears in STDOUT.");
        mt_log!(Level::Fatal, "This message appears in STDOUT.");

        // Log messages to FILE only
        mt_stream!(OutputStream::File);
        mt_log!(Level::Trace, "This message appears in FILEOUT.");
        mt_log!(Level::Fatal, "This message appears in FILEOUT.");

        // Log messages to NEITHER output
        mt_stream!(OutputStream::Neither);
        mt_log!(Level::Trace, "This message appears in NEITHER.");
        mt_log!(Level::Fatal, "This message appears in NEITHER.");

        // Flush the messages to their output
        println!("Flushing all messages to their output...");
        let start_time = time::Instant::now();
        mt_flush!()?;
        println!("Done flushing after {}ms", start_time.elapsed().as_millis());

        // Capture the files in memory before releasing the mutex
        let mut verf_file_stdout = fs::OpenOptions::new().read(true).open(STDOUT_FILENAME)?;
        let mut verf_string_stdout = String::new();
        verf_file_stdout.read_to_string(&mut verf_string_stdout)?;
        let mut verf_file_file_out = fs::OpenOptions::new().read(true).open(FILE_OUT_FILENAME)?;
        let mut verf_string_file_out = String::new();
        verf_file_file_out.read_to_string(&mut verf_string_file_out)?;

        // Unlock the mutex
        std::mem::drop(mutex);

        // Verify that the verification files contain only the correct messages
        outputstream_verf_helper(VerfFile::StdOut, verf_string_stdout)?;
        outputstream_verf_helper(VerfFile::FileOut, verf_string_file_out)?;

        Ok(())
    }

    #[test]
    fn flush_test() -> TestResult {
        // Lock logger mutex and hold it for the remainder of this test
        let _mutex = LOGGER_MUTEX.lock()?;

        // Clean verification files before test
        reset_verf_files()?;

        // Set up the logger instance
        if INSTANCE.get().is_none() {
            mt_new!(LOGFILE_PREFIX, Level::Info, OutputStream::StdOut);
        } else {
            mt_level!(Level::Info);
            mt_stream!(OutputStream::StdOut);
        }

        // Capture the initial count
        let initial_msg_count = mt_count!();
        eprintln!("Initial message count: {}", initial_msg_count);

        // Send some messages
        eprintln!("Sending messages...");
        let sent_msg_count = 5;
        for i in 0..sent_msg_count {
            mt_log!(Level::Info, "Message #{}", i);
        }

        // Send a flush command
        mt_flush!()?;

        // Verify that all sent messages were processed after flushing
        eprintln!("Messages processed: {}", mt_count!());
        assert_eq!(initial_msg_count + sent_msg_count, mt_count!());

        Ok(())
    }
}