Browse Source

Simplify formatting operations so that the format string is defined only once

Sam Clements 3 years ago
parent
commit
c49be5c183
2 changed files with 16 additions and 22 deletions
  1. 1 1
      Cargo.toml
  2. 15 21
      src/lib.rs

+ 1 - 1
Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 name = "simple_logger"
-version = "1.13.0"
+version = "1.13.1"
 license = "MIT"
 authors = ["Sam Clements <[email protected]>"]
 description = "A logger that prints all messages with a readable output format"

+ 15 - 21
src/lib.rs

@@ -303,31 +303,25 @@ impl Log for SimpleLogger {
                 record.module_path().unwrap_or_default()
             };
 
-            #[cfg(feature = "chrono")]
-            if self.timestamps {
-                #[cfg(not(feature = "stderr"))]
-                println!(
-                    "{} {:<5} [{}] {}",
-                    Local::now().format("%Y-%m-%d %H:%M:%S,%3f"),
-                    level_string,
-                    target,
-                    record.args()
-                );
-                #[cfg(feature = "stderr")]
-                eprintln!(
-                    "{} {:<5} [{}] {}",
-                    Local::now().format("%Y-%m-%d %H:%M:%S,%3f"),
-                    level_string,
-                    target,
-                    record.args()
-                );
-                return;
+            let timestamp = {
+                #[cfg(feature = "chrono")]
+                if self.timestamps {
+                    format!("{} ", Local::now().format("%Y-%m-%d %H:%M:%S,%3f"));
+                } else {
+                    "".to_string()
+                }
+
+                #[cfg(not(feature = "chrono"))]
+                "";
             }
 
+            let message = format!("{}{:<5} [{}] {}", timestamp, level_string, target, record.args());
+
             #[cfg(not(feature = "stderr"))]
-            println!("{:<5} [{}] {}", level_string, target, record.args());
+            println!("{}", message);
+
             #[cfg(feature = "stderr")]
-            eprintln!("{:<5} [{}] {}", level_string, target, record.args());
+            eprintln!("{}", message);
         }
     }