Переглянути джерело

Merge branch 'master' into disable-chrono

Sam Clements 5 роки тому
батько
коміт
4a902a3191
3 змінених файлів з 54 додано та 11 видалено
  1. 5 1
      Cargo.toml
  2. 0 1
      examples/init_with_level.rs
  3. 49 9
      src/lib.rs

+ 5 - 1
Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 name = "simple_logger"
-version = "1.3.0"
+version = "1.4.0"
 license = "MIT"
 authors = ["Sam Clements <sam@borntyping.co.uk>"]
 description = "A logger that prints all messages with a readable output format"
@@ -13,3 +13,7 @@ default = ["colored", "chrono"]
 log = { version = "^0.4.5", features = ["std"] }
 chrono = { version = "0.4.6", optional = true }
 colored = { version = "^1.6", optional = true }
+
+[target.'cfg(windows)'.dependencies]
+atty = "0.2.13"
+winapi = { version = "0.3", features = ["handleapi", "winbase"]}

+ 0 - 1
examples/init_with_level.rs

@@ -10,4 +10,3 @@ fn main() {
     warn!("This will be logged.");
     info!("This will NOT be logged.");
 }
-

+ 49 - 9
src/lib.rs

@@ -1,13 +1,20 @@
 //! A logger that prints all messages with a readable output format.
 
+#[cfg(windows)]
+extern crate atty;
+#[cfg(feature = "chrono")]
+extern crate chrono;
+#[cfg(feature = "colored")]
+extern crate colored;
 extern crate log;
-use log::{Log,Level,Metadata,Record,SetLoggerError};
+#[cfg(windows)]
+extern crate winapi;
 
-#[cfg(feature = "colored")] extern crate colored;
-#[cfg(feature = "colored")] use colored::*;
-
-#[cfg(feature = "chrono")] extern crate chrono;
-#[cfg(feature = "chrono")] use chrono::Local;
+#[cfg(feature = "chrono")]
+use chrono::Local;
+#[cfg(feature = "colored")]
+use colored::*;
+use log::{Level, Log, Metadata, Record, SetLoggerError};
 
 struct SimpleLogger {
     level: Level,
@@ -21,7 +28,8 @@ impl Log for SimpleLogger {
     fn log(&self, record: &Record) {
         if self.enabled(record.metadata()) {
             let level_string = {
-                #[cfg(feature = "colored")] {
+                #[cfg(feature = "colored")]
+                {
                     match record.level() {
                         Level::Error => record.level().to_string().red(),
                         Level::Warn => record.level().to_string().yellow(),
@@ -30,7 +38,8 @@ impl Log for SimpleLogger {
                         Level::Trace => record.level().to_string().normal(),
                     }
                 }
-                #[cfg(not(feature = "colored"))] {
+                #[cfg(not(feature = "colored"))]
+                {
                     record.level().to_string()
                 }
             };
@@ -57,7 +66,35 @@ impl Log for SimpleLogger {
         }
     }
 
-    fn flush(&self) {
+    fn flush(&self) {}
+}
+
+#[cfg(windows)]
+fn set_up_color_terminal() {
+    use atty::Stream;
+
+    if atty::is(Stream::Stdout) {
+        unsafe {
+            use winapi::um::consoleapi::*;
+            use winapi::um::handleapi::*;
+            use winapi::um::processenv::*;
+            use winapi::um::winbase::*;
+            use winapi::um::wincon::*;
+
+            let stdout = GetStdHandle(STD_OUTPUT_HANDLE);
+
+            if stdout == INVALID_HANDLE_VALUE {
+                return;
+            }
+
+            let mut mode: winapi::shared::minwindef::DWORD = 0;
+
+            if GetConsoleMode(stdout, &mut mode) == 0 {
+                return;
+            }
+
+            SetConsoleMode(stdout, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
+        }
     }
 }
 
@@ -76,6 +113,9 @@ impl Log for SimpleLogger {
 /// # }
 /// ```
 pub fn init_with_level(level: Level) -> Result<(), SetLoggerError> {
+    #[cfg(all(windows, feature = "colored"))]
+    set_up_color_terminal();
+
     let logger = SimpleLogger { level };
     log::set_boxed_logger(Box::new(logger))?;
     log::set_max_level(level.to_level_filter());