Преглед изворни кода

Merge pull request #37 from cehteh/main

Use a 'stderr' feature to target log output to stderr
Sam Clements пре 3 година
родитељ
комит
47643405f2
2 измењених фајлова са 13 додато и 0 уклоњено
  1. 1 0
      Cargo.toml
  2. 12 0
      src/lib.rs

+ 1 - 0
Cargo.toml

@@ -11,6 +11,7 @@ edition = "2018"
 default = ["colored", "chrono"]
 colors = ["colored"]
 timestamps = ["chrono"]
+stderr = []
 
 [dependencies]
 log = { version = "^0.4.5", features = ["std"] }

+ 12 - 0
src/lib.rs

@@ -305,6 +305,7 @@ impl Log for SimpleLogger {
 
             #[cfg(feature = "chrono")]
             if self.timestamps {
+                #[cfg(not(feature = "stderr"))]
                 println!(
                     "{} {:<5} [{}] {}",
                     Local::now().format("%Y-%m-%d %H:%M:%S,%3f"),
@@ -312,10 +313,21 @@ impl Log for SimpleLogger {
                     target,
                     record.args()
                 );
+                #[cfg(feature = "stderr")]
+                eprintln!(
+                    "{} {:<5} [{}] {}",
+                    Local::now().format("%Y-%m-%d %H:%M:%S,%3f"),
+                    level_string,
+                    target,
+                    record.args()
+                );
                 return;
             }
 
+            #[cfg(not(feature = "stderr"))]
             println!("{:<5} [{}] {}", level_string, target, record.args());
+            #[cfg(feature = "stderr")]
+            eprintln!("{:<5} [{}] {}", level_string, target, record.args());
         }
     }