cute_log/io/
regular.rs

1extern crate std;
2
3use std::io::Write;
4
5#[cfg(feature="timestamp")]
6#[inline(always)]
7fn get_date() -> impl core::fmt::Display {
8    struct TimeDate(time::OffsetDateTime);
9
10    impl core::fmt::Display for TimeDate {
11        #[inline(always)]
12        fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
13            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())
14        }
15    }
16
17    #[cfg(feature = "local_timestamp")]
18    {
19        return TimeDate(time::OffsetDateTime::now_local().unwrap_or_else(|_| time::OffsetDateTime::now_utc()))
20    }
21    #[cfg(not(feature = "local_timestamp"))]
22    {
23        return TimeDate(std::time::SystemTime::now().into())
24    }
25}
26
27impl crate::Logger {
28    #[inline(always)]
29    ///Prints to `stdout` as it is
30    pub fn print_fmt(args: core::fmt::Arguments<'_>) {
31        let stdout = std::io::stdout();
32        let mut stdout = stdout.lock();
33        let _ = stdout.write_fmt(format_args!("{}\n", args));
34    }
35
36    #[inline]
37    ///Logger printer.
38    pub fn print(record: &log::Record) {
39        let stdout = std::io::stdout();
40        let mut stdout = stdout.lock();
41
42        #[cfg(feature="timestamp")]
43        let _ = {
44            stdout.write_fmt(format_args!("{:<5} [{}] {{{}:{}}} - {}\n", record.level(),
45                                                                         get_date(),
46                                                                         record.file().unwrap_or("UNKNOWN"),
47                                                                         record.line().unwrap_or(0),
48                                                                         record.args()))
49        };
50
51        #[cfg(not(feature="timestamp"))]
52        let _ = {
53            stdout.write_fmt(format_args!("{:<5} {{{}:{}}} - {}\n", record.level(),
54                                                                    record.file().unwrap_or("UNKNOWN"),
55                                                                    record.line().unwrap_or(0),
56                                                                    record.args()))
57        };
58    }
59}