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

lib: Update color output support for Windows

Vladimir Petrigo пре 5 година
родитељ
комит
9ba41fc820
2 измењених фајлова са 36 додато и 10 уклоњено
  1. 4 0
      Cargo.toml
  2. 32 10
      src/lib.rs

+ 4 - 0
Cargo.toml

@@ -13,3 +13,7 @@ default = ["colored"]
 log = { version = "^0.4.5", features = ["std"] }
 chrono = "0.4.6"
 colored = { version = "^1.6", optional = true }
+
+[target.'cfg(windows)'.dependencies]
+atty = "0.2.13"
+winapi = { version = "0.3", features = ["winbase"]}

+ 32 - 10
src/lib.rs

@@ -1,13 +1,18 @@
 //! A logger that prints all messages with a readable output format.
 
-extern crate log;
+#[cfg(windows)]
+extern crate atty;
 extern crate chrono;
+#[cfg(feature = "colored")]
+extern crate colored;
+extern crate log;
+#[cfg(windows)]
+extern crate winapi;
 
-#[cfg(feature = "colored")] extern crate colored;
-#[cfg(feature = "colored")] use colored::*;
-
-use log::{Log,Level,Metadata,Record,SetLoggerError};
 use chrono::Local;
+#[cfg(feature = "colored")]
+use colored::*;
+use log::{Level, Log, Metadata, Record, SetLoggerError};
 
 struct SimpleLogger {
     level: Level,
@@ -21,7 +26,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 +36,8 @@ impl Log for SimpleLogger {
                         Level::Trace => record.level().to_string().normal(),
                     }
                 }
-                #[cfg(not(feature = "colored"))] {
+                #[cfg(not(feature = "colored"))]
+                {
                     record.level().to_string()
                 }
             };
@@ -44,12 +51,12 @@ impl Log for SimpleLogger {
                 Local::now().format("%Y-%m-%d %H:%M:%S,%3f"),
                 level_string,
                 target,
-                record.args());
+                record.args()
+            );
         }
     }
 
-    fn flush(&self) {
-    }
+    fn flush(&self) {}
 }
 
 /// Initializes the global logger with a SimpleLogger instance with
@@ -67,6 +74,21 @@ impl Log for SimpleLogger {
 /// # }
 /// ```
 pub fn init_with_level(level: Level) -> Result<(), SetLoggerError> {
+    if cfg!(windows) && cfg!(feature = "colored") {
+        use atty::Stream;
+
+        if atty::is(Stream::Stdout) {
+            unsafe {
+                use winapi::um::consoleapi::*;
+                use winapi::um::processenv::*;
+                use winapi::um::winbase::*;
+
+                let stdout = GetStdHandle(STD_OUTPUT_HANDLE);
+                SetConsoleMode(stdout, 0x0001 | 0x0002 | 0x0004);
+            }
+        }
+    }
+
     let logger = SimpleLogger { level };
     log::set_boxed_logger(Box::new(logger))?;
     log::set_max_level(level.to_level_filter());