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
/// How the timestamp should be displayed
///
/// Several helper methods for constructing this type are provided
/// * [`Relative`](enum.TimeConfig.html#variant.Relative) timestamp style
///     * use [`TimeConfig::relative_now`](enum.TimeConfig.html#method.relative_now) to start the _clock_ from `now`
/// * [`Timing`](enum.TimeConfig.html#variant.Timing) timestamp style
///     * use [`TimeConfig::relative_local`](enum.TimeConfig.html#method.relative_local).
///
/// ***Note*** Defaults to the `None` timestamp
#[derive(Debug)]
#[non_exhaustive]
pub enum TimeConfig {
    /// No timestamp
    None,
    ///
    /// Timestamp since the UNIX epoch
    Unix,
    /// Relative timestamp from the start of the program
    ///
    /// This prints out a fractional number of seconds from when the logger was initialized.
    Relative(std::time::Instant),
    /// Relative timestamp from the previous log statement
    ///
    /// This prints out a fractional number of seconds since the last statement was logged        
    Timing(std::sync::Mutex<Option<std::time::Instant>>),
    #[cfg(feature = "time")]
    /// Timestamp formatted with from UTC 'now'. See [`formatting`](https://docs.rs/time/0.2.9/time/index.html#formatting)
    ///
    /// This allows you to provide a 'fixed' date time. (e.g. UTC offset or unix timestamp or whatever you want)    
    DateTime(String),
}

impl Clone for TimeConfig {
    fn clone(&self) -> Self {
        match self {
            Self::None => Self::None,
            Self::Unix => Self::Unix,
            Self::Relative(inner) => Self::Relative(*inner),
            Self::Timing(_) => Self::Timing(Default::default()),
            #[cfg(feature = "time")]
            Self::DateTime(inner) => Self::DateTime(inner.clone()),
        }
    }
}

impl TimeConfig {
    /// Create a Relative timestamp starting at 'now'
    pub fn relative_now() -> Self {
        Self::Relative(std::time::Instant::now())
    }

    /// Create a Relative timestamp based on the previous logging statement
    pub fn relative_local() -> Self {
        Self::Timing(Default::default())
    }

    /// Create a timestamp based on the UNIX epoch (number of seconds since Jan. 1 1970)
    pub fn unix_timestamp() -> Self {
        Self::Unix
    }

    #[cfg(feature = "time")]
    /// Create a DateTime format
    pub fn date_time_format(s: impl ToString) -> Self {
        Self::DateTime(s.to_string())
    }
}

impl Default for TimeConfig {
    fn default() -> Self {
        Self::None
    }
}