Browse Source

Replace the chrono cate with the time crate

This avoids a potential segfault when getting the local UTC offset in
order to display a local timestamp.

https://rustsec.org/advisories/RUSTSEC-2020-0159
https://github.com/time-rs/time/issues/293

Resolves #41
Sam Clements 3 years ago
parent
commit
20d93f64ad
2 changed files with 27 additions and 19 deletions
  1. 6 6
      Cargo.toml
  2. 21 13
      src/lib.rs

+ 6 - 6
Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 name = "simple_logger"
-version = "1.13.1"
+version = "1.14.0"
 license = "MIT"
 authors = ["Sam Clements <[email protected]>"]
 description = "A logger that prints all messages with a readable output format"
@@ -8,14 +8,14 @@ repository = "https://github.com/borntyping/rust-simple_logger"
 edition = "2018"
 
 [features]
-default = ["colored", "chrono"]
+default = ["colored", "time"]
 colors = ["colored"]
-timestamps = ["chrono"]
+timestamps = ["time"]
 stderr = []
 
 [dependencies]
 log = { version = "^0.4.5", features = ["std"] }
-chrono = { version = "0.4.6", optional = true }
+time = { version = "0.3.5", features = ["formatting", "local-offset", "macros"], optional = true }
 colored = { version = "^1.6", optional = true }
 
 [target.'cfg(windows)'.dependencies]
@@ -24,8 +24,8 @@ winapi = { version = "0.3", features = ["handleapi", "winbase"]}
 
 [[example]]
 name = "colors"
-required-features = ["colored"]
+required-features = ["colors"]
 
 [[example]]
 name = "timestamps"
-required-features = ["chrono"]
+required-features = ["timestamps"]

+ 21 - 13
src/lib.rs

@@ -28,13 +28,16 @@
 //! simple_logger::init_with_level(log::Level::Warn).unwrap();
 //! ```
 
-#[cfg(feature = "chrono")]
-use chrono::Local;
+#[cfg(feature = "timestamps")]
+use time::{format_description::FormatItem, OffsetDateTime};
 #[cfg(feature = "colored")]
 use colored::*;
 use log::{Level, LevelFilter, Log, Metadata, Record, SetLoggerError};
 use std::collections::HashMap;
 
+#[cfg(feature = "timestamps")]
+const TIMESTAMP_FORMAT: &[FormatItem] = time::macros::format_description!("[year]-[month]-[day] [hour]:[minute]:[second],[subsecond digits:3]");
+
 /// Implements [`Log`] and a set of simple builder methods for configuration.
 ///
 /// Use the various "builder" methods on this struct to configure the logger,
@@ -52,8 +55,8 @@ pub struct SimpleLogger {
 
     /// Whether to include timestamps or not
     ///
-    /// This field is only available if the `chrono` feature is enabled.
-    #[cfg(feature = "chrono")]
+    /// This field is only available if the `timestamps` feature is enabled.
+    #[cfg(feature = "timestamps")]
     timestamps: bool,
 
     /// Whether to use color output or not.
@@ -80,7 +83,7 @@ impl SimpleLogger {
             default_level: LevelFilter::Trace,
             module_levels: Vec::new(),
 
-            #[cfg(feature = "chrono")]
+            #[cfg(feature = "timestamps")]
             timestamps: true,
 
             #[cfg(feature = "colored")]
@@ -209,9 +212,9 @@ impl SimpleLogger {
 
     /// Control whether timestamps are printed or not.
     ///
-    /// This method is only available if the `chrono` feature is enabled.
+    /// This method is only available if the `timestamps` feature is enabled.
     #[must_use = "You must call init() to begin logging"]
-    #[cfg(feature = "chrono")]
+    #[cfg(feature = "timestamps")]
     pub fn with_timestamps(mut self, timestamps: bool) -> SimpleLogger {
         self.timestamps = timestamps;
         self
@@ -304,16 +307,21 @@ impl Log for SimpleLogger {
             };
 
             let timestamp = {
-                #[cfg(feature = "chrono")]
+                #[cfg(feature = "timestamps")]
                 if self.timestamps {
-                    format!("{} ", Local::now().format("%Y-%m-%d %H:%M:%S,%3f"));
+                    format!("{} ", OffsetDateTime::now_local().expect(concat!(
+                        "Could not determine the UTC offset on this system. ",
+                        "The time crate disables this feature by default in multi-threaded environments. ",
+                        "Carefully consider using it's \"unsound_local_offset\" feature flag. ",
+                        "https://time-rs.github.io/internal-api/time/index.html#feature-flags"
+                    )).format(&TIMESTAMP_FORMAT).unwrap())
                 } else {
                     "".to_string()
                 }
 
-                #[cfg(not(feature = "chrono"))]
-                "";
-            }
+                #[cfg(not(feature = "timestamps"))]
+                ""
+            };
 
             let message = format!("{}{:<5} [{}] {}", timestamp, level_string, target, record.args());
 
@@ -430,7 +438,7 @@ mod test {
     }
 
     #[test]
-    #[cfg(feature = "chrono")]
+    #[cfg(feature = "timestamps")]
     fn test_with_timestamps() {
         let mut builder = SimpleLogger::new();
         assert!(builder.timestamps == true);