Skip to content

Log the current task name in the log! macros. #19444

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
12 changes: 10 additions & 2 deletions src/liblog/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,10 @@ impl fmt::Show for LogLevel {
impl Logger for DefaultLogger {
fn log(&mut self, record: &LogRecord) {
match writeln!(&mut self.handle,
"{}:{}: {}",
"{}:{}:{}: {}",
record.level,
record.module_path,
record.task_name,
record.args) {
Err(e) => panic!("failed to log: {}", e),
Ok(()) => {}
Expand All @@ -275,7 +276,7 @@ impl Drop for DefaultLogger {
/// It is not recommended to call this function directly, rather it should be
/// invoked through the logging family of macros.
#[doc(hidden)]
pub fn log(level: u32, loc: &'static LogLocation, args: &fmt::Arguments) {
pub fn log(level: u32, loc: LogLocation, args: &fmt::Arguments) {
// Test the literal string from args against the current filter, if there
// is one.
match unsafe { FILTER.as_ref() } {
Expand All @@ -297,6 +298,9 @@ pub fn log(level: u32, loc: &'static LogLocation, args: &fmt::Arguments) {
file: loc.file,
module_path: loc.module_path,
line: loc.line,
task_name: loc.task_name
.as_ref().map(|s| &**s)
.unwrap_or("<unnamed>"),
});
set_logger(logger);
}
Expand Down Expand Up @@ -335,13 +339,17 @@ pub struct LogRecord<'a> {

/// The line number of where the LogRecord originated.
pub line: uint,

/// The task name of the task where the LogRecord originated.
pub task_name: &'a str
}

#[doc(hidden)]
pub struct LogLocation {
pub module_path: &'static str,
pub file: &'static str,
pub line: uint,
pub task_name: Option<String>
}

/// Tests whether a given module's name is enabled for a particular level of
Expand Down
13 changes: 7 additions & 6 deletions src/liblog/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@
#[macro_export]
macro_rules! log(
($lvl:expr, $($arg:tt)+) => ({
static LOC: ::log::LogLocation = ::log::LogLocation {
line: line!(),
file: file!(),
module_path: module_path!(),
};
let lvl = $lvl;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexcrichton had me specifically add in this static code, to reduce code generation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Urgh. Initially I tried to keep it, but you can't call ::std::task::name in a static.

if log_enabled!(lvl) {
format_args!(|args| { ::log::log(lvl, &LOC, args) }, $($arg)+)
format_args!(|args| { ::log::log(lvl, ::log::LogLocation {
line: line!(),
file: file!(),
module_path: module_path!(),
task_name: ::std::task::name()
}, args)
}, $($arg)+)
}
})
)
Expand Down