Преглед на файлове

Merge pull request #11 from vpetrigo/feature/windows_color

Enable Windows console colors
Sam Clements преди 5 години
родител
ревизия
50dd2db418
променени са 3 файла, в които са добавени 52 реда и са изтрити 10 реда
  1. 4 0
      Cargo.toml
  2. 0 1
      examples/init_with_level.rs
  3. 48 9
      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 = ["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.");
 }
-

+ 48 - 9
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,11 +51,40 @@ 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) {}
+}
+
+#[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);
+        }
     }
 }
 
@@ -67,6 +103,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());