2012-12-04 01:48:01 +01:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-07-04 23:53:12 +02:00
|
|
|
//! Logging
|
2012-01-13 22:20:11 +01:00
|
|
|
|
2013-04-28 03:57:15 +02:00
|
|
|
use option::*;
|
2013-06-22 01:52:07 +02:00
|
|
|
use os;
|
2013-04-28 03:57:15 +02:00
|
|
|
use either::*;
|
2013-06-22 01:52:07 +02:00
|
|
|
use rt;
|
2013-04-28 03:57:15 +02:00
|
|
|
use rt::logging::{Logger, StdErrLogger};
|
2012-01-13 22:20:11 +01:00
|
|
|
|
2012-07-04 23:53:12 +02:00
|
|
|
/// Turns on logging to stdout globally
|
2012-09-27 01:26:19 +02:00
|
|
|
pub fn console_on() {
|
2013-08-01 08:12:20 +02:00
|
|
|
rt::logging::console_on();
|
2012-01-13 22:20:11 +01:00
|
|
|
}
|
|
|
|
|
2012-07-04 23:53:12 +02:00
|
|
|
/**
|
|
|
|
* Turns off logging to stdout globally
|
|
|
|
*
|
|
|
|
* Turns off the console unless the user has overridden the
|
|
|
|
* runtime environment's logging spec, e.g. by setting
|
|
|
|
* the RUST_LOG environment variable
|
|
|
|
*/
|
2012-09-27 01:26:19 +02:00
|
|
|
pub fn console_off() {
|
2013-06-22 01:52:07 +02:00
|
|
|
// If RUST_LOG is set then the console can't be turned off
|
|
|
|
if os::getenv("RUST_LOG").is_some() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-01 08:12:20 +02:00
|
|
|
rt::logging::console_off();
|
2012-09-25 21:17:20 +02:00
|
|
|
}
|
|
|
|
|
2013-05-08 13:11:23 +02:00
|
|
|
#[cfg(not(test))]
|
2012-09-25 21:17:20 +02:00
|
|
|
#[lang="log_type"]
|
2013-05-28 23:35:52 +02:00
|
|
|
#[allow(missing_doc)]
|
2013-08-01 08:12:20 +02:00
|
|
|
pub fn log_type<T>(_level: u32, object: &T) {
|
2013-02-28 17:57:33 +01:00
|
|
|
use io;
|
|
|
|
use repr;
|
2013-05-28 01:04:00 +02:00
|
|
|
use str;
|
2013-02-28 17:57:33 +01:00
|
|
|
|
2012-10-18 18:14:11 +02:00
|
|
|
let bytes = do io::with_bytes_writer |writer| {
|
2012-09-25 21:17:20 +02:00
|
|
|
repr::write_repr(writer, object);
|
|
|
|
};
|
2013-04-28 03:57:15 +02:00
|
|
|
|
2013-08-01 08:12:20 +02:00
|
|
|
// XXX: Bad allocation
|
|
|
|
let msg = str::from_bytes(bytes);
|
|
|
|
newsched_log_str(msg);
|
2013-04-28 03:57:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn newsched_log_str(msg: ~str) {
|
2013-05-20 01:50:21 +02:00
|
|
|
use rt::task::Task;
|
|
|
|
use rt::local::Local;
|
|
|
|
|
2012-09-25 21:17:20 +02:00
|
|
|
unsafe {
|
2013-05-20 01:50:21 +02:00
|
|
|
match Local::try_unsafe_borrow::<Task>() {
|
2013-04-28 03:57:15 +02:00
|
|
|
Some(local) => {
|
|
|
|
// Use the available logger
|
|
|
|
(*local).logger.log(Left(msg));
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
// There is no logger anywhere, just write to stderr
|
|
|
|
let mut logger = StdErrLogger;
|
|
|
|
logger.log(Left(msg));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|