瀏覽代碼

simple_logger: Clean up

Vladimir Petrigo 5 年之前
父節點
當前提交
121e9cefb0
共有 1 個文件被更改,包括 18 次插入14 次删除
  1. 18 14
      src/lib.rs

+ 18 - 14
src/lib.rs

@@ -59,6 +59,22 @@ impl Log for SimpleLogger {
     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::processenv::*;
+            use winapi::um::winbase::*;
+
+            let stdout = GetStdHandle(STD_OUTPUT_HANDLE);
+            SetConsoleMode(stdout, 0x0001 | 0x0002 | 0x0004);
+        }
+    }
+}
+
 /// Initializes the global logger with a SimpleLogger instance with
 /// `max_log_level` set to a specific log level.
 ///
@@ -74,20 +90,8 @@ 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);
-            }
-        }
-    }
+    #[cfg(all(windows, feature = "colored"))]
+    set_up_color_terminal();
 
     let logger = SimpleLogger { level };
     log::set_boxed_logger(Box::new(logger))?;