From 262d2bc4cc2a84ad9641d72ec68c847731669398 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Br=C3=BCschweiler?= Date: Mon, 21 Apr 2014 21:31:28 +0200 Subject: [PATCH] liblog: add the loglevel, file and linenumber to the log output --- src/liblog/lib.rs | 24 +++++++++++++++++++----- src/liblog/macros.rs | 2 +- src/test/run-pass/capturing-logging.rs | 2 +- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/liblog/lib.rs b/src/liblog/lib.rs index 87c82e1dd7d42..3235ae8bc8bf8 100644 --- a/src/liblog/lib.rs +++ b/src/liblog/lib.rs @@ -164,17 +164,31 @@ local_data_key!(local_logger: ~Logger:Send) pub trait Logger { /// Logs a single message described by the `args` structure. The level is /// provided in case you want to do things like color the message, etc. - fn log(&mut self, level: u32, args: &fmt::Arguments); + /// `file` and `line` are the filename and line number respectively of + /// the originating log statement. + fn log(&mut self, level: u32, file: &str, line: uint, args: &fmt::Arguments); } struct DefaultLogger { handle: LineBufferedWriter, } +fn level_to_string(level: u32) -> ~str { + match level { + DEBUG => ~"D", + INFO => ~"I", + WARN => ~"W", + ERROR => ~"E", + _ => level.to_str(), + } +} + impl Logger for DefaultLogger { // by default, just ignore the level - fn log(&mut self, _level: u32, args: &fmt::Arguments) { - match fmt::writeln(&mut self.handle, args) { + fn log(&mut self, level: u32, file: &str, line: uint, args: &fmt::Arguments) { + match write!(&mut self.handle, "{} {}:{} ", + level_to_string(level), file, line).and_then( + |()| fmt::writeln(&mut self.handle, args)) { Err(e) => fail!("failed to log: {}", e), Ok(()) => {} } @@ -198,14 +212,14 @@ impl Drop for DefaultLogger { /// /// It is not recommended to call this function directly, rather it should be /// invoked through the logging family of macros. -pub fn log(level: u32, args: &fmt::Arguments) { +pub fn log(level: u32, file: &str, line:uint, args: &fmt::Arguments) { // Completely remove the local logger from TLS in case anyone attempts to // frob the slot while we're doing the logging. This will destroy any logger // set during logging. let mut logger = local_data::pop(local_logger).unwrap_or_else(|| { ~DefaultLogger { handle: io::stderr() } as ~Logger:Send }); - logger.log(level, args); + logger.log(level, file, line, args); local_data::set(local_logger, logger); } diff --git a/src/liblog/macros.rs b/src/liblog/macros.rs index f1d4a318bf080..f5486cdd22a4e 100644 --- a/src/liblog/macros.rs +++ b/src/liblog/macros.rs @@ -35,7 +35,7 @@ macro_rules! log( ($lvl:expr, $($arg:tt)+) => ({ let lvl = $lvl; if log_enabled!(lvl) { - format_args!(|args| { ::log::log(lvl, args) }, $($arg)+) + format_args!(|args| { ::log::log(lvl, file!(), line!(), args) }, $($arg)+) } }) ) diff --git a/src/test/run-pass/capturing-logging.rs b/src/test/run-pass/capturing-logging.rs index a6443dc5a1a9a..b1d81651c0bba 100644 --- a/src/test/run-pass/capturing-logging.rs +++ b/src/test/run-pass/capturing-logging.rs @@ -24,7 +24,7 @@ use log::{set_logger, Logger}; struct MyWriter(ChanWriter); impl Logger for MyWriter { - fn log(&mut self, _level: u32, args: &fmt::Arguments) { + fn log(&mut self, _level: u32, _file: &str, _line: uint, args: &fmt::Arguments) { let MyWriter(ref mut inner) = *self; fmt::writeln(inner as &mut Writer, args); }