From 76270816d527bfceef64bf6cbdc64f985ca73eba Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 12 Dec 2013 17:34:29 -0800 Subject: std: Make logging safely implemented This commit fixes the logging function to be safely implemented, as well as forcibly requiring a task to be present to use logging macros. This is safely implemented by transferring ownership of the logger from the task to the local stack frame in order to perform the print. This means that if a logger does more logging while logging a new one will be initialized and then will get overwritten once the initial logging function returns. Without a scheme such as this, it is possible to unsafely alias two loggers by logging twice (unsafely borrows from the task twice). --- src/libstd/logging.rs | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/logging.rs b/src/libstd/logging.rs index dbe8b3247c0..fb83cfdd6ea 100644 --- a/src/libstd/logging.rs +++ b/src/libstd/logging.rs @@ -118,26 +118,16 @@ pub static ERROR: u32 = 1; /// 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) { - unsafe { - let optional_task: Option<*mut Task> = Local::try_unsafe_borrow(); - match optional_task { - Some(local) => { - // Lazily initialize the local task's logger - match (*local).logger { - // Use the available logger if we have one - Some(ref mut logger) => { logger.log(args); } - None => { - let mut logger = StdErrLogger::new(); - logger.log(args); - (*local).logger = Some(logger); - } - } - } - // If there's no local task, then always log to stderr - None => { - let mut logger = StdErrLogger::new(); - logger.log(args); - } - } + let mut logger = { + let mut task = Local::borrow(None::); + task.get().logger.take() + }; + + if logger.is_none() { + logger = Some(StdErrLogger::new()); } + logger.get_mut_ref().log(args); + + let mut task = Local::borrow(None::); + task.get().logger = logger; } -- cgit 1.4.1-3-g733a5