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
extern crate std;

use std::io::Write;

#[cfg(feature="timestamp")]
#[inline(always)]
fn get_date() -> impl core::fmt::Display {
    struct TimeDate(time::OffsetDateTime);

    impl core::fmt::Display for TimeDate {
        #[inline(always)]
        fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
            write!(f, "{}-{:02}-{:02} {:02}:{:02}:{:02}.{:03}", self.0.year(), self.0.month() as u8, self.0.day(), self.0.hour(), self.0.minute(), self.0.second(), self.0.millisecond())
        }
    }

    #[cfg(feature = "local_timestamp")]
    {
        return TimeDate(time::OffsetDateTime::now_local().unwrap_or_else(|_| time::OffsetDateTime::now_utc()))
    }
    #[cfg(not(feature = "local_timestamp"))]
    {
        return TimeDate(std::time::SystemTime::now().into())
    }
}

impl crate::Logger {
    #[inline(always)]
    ///Prints to `stdout` as it is
    pub fn print_fmt(args: core::fmt::Arguments<'_>) {
        let stdout = std::io::stdout();
        let mut stdout = stdout.lock();
        let _ = stdout.write_fmt(format_args!("{}\n", args));
    }

    #[inline]
    ///Logger printer.
    pub fn print(record: &log::Record) {
        let stdout = std::io::stdout();
        let mut stdout = stdout.lock();

        #[cfg(feature="timestamp")]
        let _ = {
            stdout.write_fmt(format_args!("{:<5} [{}] {{{}:{}}} - {}\n", record.level(),
                                                                         get_date(),
                                                                         record.file().unwrap_or("UNKNOWN"),
                                                                         record.line().unwrap_or(0),
                                                                         record.args()))
        };

        #[cfg(not(feature="timestamp"))]
        let _ = {
            stdout.write_fmt(format_args!("{:<5} {{{}:{}}} - {}\n", record.level(),
                                                                    record.file().unwrap_or("UNKNOWN"),
                                                                    record.line().unwrap_or(0),
                                                                    record.args()))
        };
    }
}