Skip to content

liblog: add the loglevel, file and linenumber to the log output #13708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/liblog/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<io::stdio::StdWriter>,
}

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(()) => {}
}
Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/liblog/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)+)
}
})
)
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/capturing-logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down