Skip to content

Commit 8e155d8

Browse files
committed
Log the current task name in the log! macros.
This requires doing an allocation on log! invocations if the current task is named, since there is no non-allocating way to access the name of the current task. Fixes rust-lang#2672
1 parent 09f04bf commit 8e155d8

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/liblog/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,10 @@ impl fmt::Show for LogLevel {
247247
impl Logger for DefaultLogger {
248248
fn log(&mut self, record: &LogRecord) {
249249
match writeln!(&mut self.handle,
250-
"{}:{}: {}",
250+
"{}:{}:{}: {}",
251251
record.level,
252252
record.module_path,
253+
record.task_name,
253254
record.args) {
254255
Err(e) => panic!("failed to log: {}", e),
255256
Ok(()) => {}
@@ -275,7 +276,7 @@ impl Drop for DefaultLogger {
275276
/// It is not recommended to call this function directly, rather it should be
276277
/// invoked through the logging family of macros.
277278
#[doc(hidden)]
278-
pub fn log(level: u32, loc: &'static LogLocation, args: &fmt::Arguments) {
279+
pub fn log(level: u32, loc: LogLocation, args: &fmt::Arguments) {
279280
// Test the literal string from args against the current filter, if there
280281
// is one.
281282
match unsafe { FILTER.as_ref() } {
@@ -297,6 +298,9 @@ pub fn log(level: u32, loc: &'static LogLocation, args: &fmt::Arguments) {
297298
file: loc.file,
298299
module_path: loc.module_path,
299300
line: loc.line,
301+
task_name: loc.task_name
302+
.as_ref().map(|s| &**s)
303+
.unwrap_or("<unnamed>"),
300304
});
301305
set_logger(logger);
302306
}
@@ -335,13 +339,17 @@ pub struct LogRecord<'a> {
335339

336340
/// The line number of where the LogRecord originated.
337341
pub line: uint,
342+
343+
/// The task name of the task where the LogRecord originated.
344+
pub task_name: &'a str
338345
}
339346

340347
#[doc(hidden)]
341348
pub struct LogLocation {
342349
pub module_path: &'static str,
343350
pub file: &'static str,
344351
pub line: uint,
352+
pub task_name: Option<String>
345353
}
346354

347355
/// Tests whether a given module's name is enabled for a particular level of

src/liblog/macros.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@
5353
#[macro_export]
5454
macro_rules! log(
5555
($lvl:expr, $($arg:tt)+) => ({
56-
static LOC: ::log::LogLocation = ::log::LogLocation {
57-
line: line!(),
58-
file: file!(),
59-
module_path: module_path!(),
60-
};
6156
let lvl = $lvl;
6257
if log_enabled!(lvl) {
63-
format_args!(|args| { ::log::log(lvl, &LOC, args) }, $($arg)+)
58+
format_args!(|args| { ::log::log(lvl, ::log::LogLocation {
59+
line: line!(),
60+
file: file!(),
61+
module_path: module_path!(),
62+
task_name: ::std::task::name()
63+
}, args)
64+
}, $($arg)+)
6465
}
6566
})
6667
)

0 commit comments

Comments
 (0)