|
@@ -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());
|