Prechádzať zdrojové kódy

Use an enum to control timestamp display

This will be a little easier to maintain than a potentially increasing
number of booleans; and avoids silly combinations like
`timestamps=false` at the same time as `timestamps_utc=true`.
Sam Clements 3 rokov pred
rodič
commit
bc4d25ab91
3 zmenil súbory, kde vykonal 44 pridanie a 46 odobranie
  1. 1 1
      examples/timestamps_local.rs
  2. 1 1
      examples/timestamps_utc.rs
  3. 42 44
      src/lib.rs

+ 1 - 1
examples/timestamps_local.rs

@@ -1,7 +1,7 @@
 use simple_logger::SimpleLogger;
 use simple_logger::SimpleLogger;
 
 
 fn main() {
 fn main() {
-    SimpleLogger::new().with_utc_timestamps(true).init().unwrap();
+    SimpleLogger::new().with_local_timestamps().init().unwrap();
 
 
     log::warn!("This is an example message.");
     log::warn!("This is an example message.");
 }
 }

+ 1 - 1
examples/timestamps_utc.rs

@@ -1,7 +1,7 @@
 use simple_logger::SimpleLogger;
 use simple_logger::SimpleLogger;
 
 
 fn main() {
 fn main() {
-    SimpleLogger::new().with_utc_timestamps(true).init().unwrap();
+    SimpleLogger::new().with_utc_timestamps().init().unwrap();
 
 
     log::warn!("This is an example message.");
     log::warn!("This is an example message.");
 }
 }

+ 42 - 44
src/lib.rs

@@ -42,6 +42,14 @@ const TIMESTAMP_FORMAT: &[FormatItem] = time::macros::format_description!(
     "[year]-[month]-[day] [hour]:[minute]:[second],[subsecond digits:3]"
     "[year]-[month]-[day] [hour]:[minute]:[second],[subsecond digits:3]"
 );
 );
 
 
+#[cfg(feature = "timestamps")]
+#[derive(PartialEq)]
+enum Timestamps {
+    None,
+    Local,
+    UTC
+}
+
 /// Implements [`Log`] and a set of simple builder methods for configuration.
 /// Implements [`Log`] and a set of simple builder methods for configuration.
 ///
 ///
 /// Use the various "builder" methods on this struct to configure the logger,
 /// Use the various "builder" methods on this struct to configure the logger,
@@ -63,17 +71,11 @@ pub struct SimpleLogger {
     #[cfg(feature = "threads")]
     #[cfg(feature = "threads")]
     threads: bool,
     threads: bool,
 
 
-    /// Whether to include timestamps or not
-    ///
-    /// This field is only available if the `timestamps` feature is enabled.
-    #[cfg(feature = "timestamps")]
-    timestamps: bool,
-
-    /// Whether to show timestamps in UTC time (true) or local time (false)
+    /// Control how timestamps are displayed.
     ///
     ///
     /// This field is only available if the `timestamps` feature is enabled.
     /// This field is only available if the `timestamps` feature is enabled.
     #[cfg(feature = "timestamps")]
     #[cfg(feature = "timestamps")]
-    timestamps_utc: bool,
+    timestamps: Timestamps,
 
 
     /// Whether to use color output or not.
     /// Whether to use color output or not.
     ///
     ///
@@ -103,10 +105,7 @@ impl SimpleLogger {
             threads: false,
             threads: false,
 
 
             #[cfg(feature = "timestamps")]
             #[cfg(feature = "timestamps")]
-            timestamps: true,
-
-            #[cfg(feature = "timestamps")]
-            timestamps_utc: false,
+            timestamps: Timestamps::Local,
 
 
             #[cfg(feature = "colored")]
             #[cfg(feature = "colored")]
             colors: true,
             colors: true,
@@ -255,34 +254,41 @@ impl SimpleLogger {
         note = "Use [`with_local_timestamps`] or [`with_utc_timestamps`] instead. Will be removed in version 2.0.0."
         note = "Use [`with_local_timestamps`] or [`with_utc_timestamps`] instead. Will be removed in version 2.0.0."
     )]
     )]
     pub fn with_timestamps(mut self, timestamps: bool) -> SimpleLogger {
     pub fn with_timestamps(mut self, timestamps: bool) -> SimpleLogger {
-        self.timestamps = timestamps;
-        self.timestamps_utc = false;
+        if timestamps {
+            self.timestamps = Timestamps::Local
+        } else {
+            self.timestamps = Timestamps::None
+        }
         self
         self
     }
     }
 
 
-    /// Control whether timestamps are printed or not.
-    ///
-    /// Timestamps will be displayed in the local timezone.
+    /// Don't display any timestamps.
     ///
     ///
     /// This method is only available if the `timestamps` feature is enabled.
     /// This method is only available if the `timestamps` feature is enabled.
     #[must_use = "You must call init() to begin logging"]
     #[must_use = "You must call init() to begin logging"]
     #[cfg(feature = "timestamps")]
     #[cfg(feature = "timestamps")]
-    pub fn with_local_timestamps(mut self, timestamps: bool) -> SimpleLogger {
-        self.timestamps = timestamps;
-        self.timestamps_utc = false;
+    pub fn without_timestamps(mut self) -> SimpleLogger {
+        self.timestamps = Timestamps::None;
         self
         self
     }
     }
 
 
-    /// Control whether timestamps are printed or not.
+    /// Display timestamps using the local timezone.
     ///
     ///
-    /// Timestamps will be displayed in UTC.
+    /// This method is only available if the `timestamps` feature is enabled.
+    #[must_use = "You must call init() to begin logging"]
+    #[cfg(feature = "timestamps")]
+    pub fn with_local_timestamps(mut self) -> SimpleLogger {
+        self.timestamps = Timestamps::Local;
+        self
+    }
+
+    /// Display timestamps using UTC.
     ///
     ///
     /// This method is only available if the `timestamps` feature is enabled.
     /// This method is only available if the `timestamps` feature is enabled.
     #[must_use = "You must call init() to begin logging"]
     #[must_use = "You must call init() to begin logging"]
     #[cfg(feature = "timestamps")]
     #[cfg(feature = "timestamps")]
-    pub fn with_utc_timestamps(mut self, timestamps: bool) -> SimpleLogger {
-        self.timestamps = timestamps;
-        self.timestamps_utc = true;
+    pub fn with_utc_timestamps(mut self) -> SimpleLogger {
+        self.timestamps = Timestamps::UTC;
         self
         self
     }
     }
 
 
@@ -398,21 +404,17 @@ impl Log for SimpleLogger {
 
 
             let timestamp = {
             let timestamp = {
                 #[cfg(feature = "timestamps")]
                 #[cfg(feature = "timestamps")]
-                if self.timestamps {
-                    if self.timestamps_utc {
-                        format!("{} ", OffsetDateTime::now_utc())
-                    } else {
-                        format!("{} ", OffsetDateTime::now_local().expect(concat!(
+                match self.timestamps {
+                    Timestamps::None => "".to_string(),
+                    Timestamps::Local => format!("{} ", OffsetDateTime::now_local().expect(concat!(
                             "Could not determine the UTC offset on this system. ",
                             "Could not determine the UTC offset on this system. ",
                             "Possible causes are that the time crate does not implement \"local_offset_at\" ",
                             "Possible causes are that the time crate does not implement \"local_offset_at\" ",
                             "on your system, or that you are running in a multi-threaded environment and ",
                             "on your system, or that you are running in a multi-threaded environment and ",
                             "the time crate is returning \"None\" from \"local_offset_at\" to avoid unsafe ",
                             "the time crate is returning \"None\" from \"local_offset_at\" to avoid unsafe ",
                             "behaviour. See the time crate's documentation for more information. ",
                             "behaviour. See the time crate's documentation for more information. ",
                             "(https://time-rs.github.io/internal-api/time/index.html#feature-flags)"
                             "(https://time-rs.github.io/internal-api/time/index.html#feature-flags)"
-                        )).format(&TIMESTAMP_FORMAT).unwrap())
-                    }
-                } else {
-                    "".to_string()
+                        )).format(&TIMESTAMP_FORMAT).unwrap()),
+                    Timestamps::UTC => format!("{} ", OffsetDateTime::now_utc()),
                 }
                 }
 
 
                 #[cfg(not(feature = "timestamps"))]
                 #[cfg(not(feature = "timestamps"))]
@@ -544,8 +546,7 @@ mod test {
     #[cfg(feature = "timestamps")]
     #[cfg(feature = "timestamps")]
     fn test_timestamps_defaults() {
     fn test_timestamps_defaults() {
         let builder = SimpleLogger::new();
         let builder = SimpleLogger::new();
-        assert!(builder.timestamps == true);
-        assert!(builder.timestamps_utc == false);
+        assert!(builder.timestamps == Timestamps::Local);
     }
     }
 
 
     #[test]
     #[test]
@@ -553,24 +554,21 @@ mod test {
     #[allow(deprecated)]
     #[allow(deprecated)]
     fn test_with_timestamps() {
     fn test_with_timestamps() {
         let builder = SimpleLogger::new().with_timestamps(false);
         let builder = SimpleLogger::new().with_timestamps(false);
-        assert!(builder.timestamps == false);
-        assert!(builder.timestamps_utc == false);
+        assert!(builder.timestamps == Timestamps::None);
     }
     }
 
 
     #[test]
     #[test]
     #[cfg(feature = "timestamps")]
     #[cfg(feature = "timestamps")]
     fn test_with_utc_timestamps() {
     fn test_with_utc_timestamps() {
-        let builder = SimpleLogger::new().with_utc_timestamps(true);
-        assert!(builder.timestamps == true);
-        assert!(builder.timestamps_utc == true);
+        let builder = SimpleLogger::new().with_utc_timestamps();
+        assert!(builder.timestamps == Timestamps::UTC);
     }
     }
 
 
     #[test]
     #[test]
     #[cfg(feature = "timestamps")]
     #[cfg(feature = "timestamps")]
     fn test_with_local_timestamps() {
     fn test_with_local_timestamps() {
-        let builder = SimpleLogger::new().with_local_timestamps(true);
-        assert!(builder.timestamps == true);
-        assert!(builder.timestamps_utc == false);
+        let builder = SimpleLogger::new().with_local_timestamps();
+        assert!(builder.timestamps == Timestamps::Local);
     }
     }
 
 
     #[test]
     #[test]