2012-12-03 18:48:01 -06: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 16:53:12 -05:00
|
|
|
//! Logging
|
2012-01-13 15:20:11 -06:00
|
|
|
|
2013-04-27 20:57:15 -05:00
|
|
|
use option::*;
|
|
|
|
use either::*;
|
|
|
|
use rt;
|
|
|
|
use rt::logging::{Logger, StdErrLogger};
|
|
|
|
use cast;
|
2013-05-04 19:30:31 -05:00
|
|
|
use str;
|
2012-01-13 15:20:11 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Turns on logging to stdout globally
|
2012-09-26 18:26:19 -05:00
|
|
|
pub fn console_on() {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
rustrt::rust_log_console_on();
|
|
|
|
}
|
2012-01-13 15:20:11 -06:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05: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-26 18:26:19 -05:00
|
|
|
pub fn console_off() {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
rustrt::rust_log_console_off();
|
|
|
|
}
|
2012-09-25 14:17:20 -05:00
|
|
|
}
|
|
|
|
|
2013-05-08 06:11:23 -05:00
|
|
|
#[cfg(not(test))]
|
2012-09-25 14:17:20 -05:00
|
|
|
#[lang="log_type"]
|
|
|
|
pub fn log_type<T>(level: u32, object: &T) {
|
2013-05-12 19:34:15 -05:00
|
|
|
use container::Container;
|
2013-02-28 10:57:33 -06:00
|
|
|
use io;
|
2013-03-22 21:26:32 -05:00
|
|
|
use libc;
|
2013-02-28 10:57:33 -06:00
|
|
|
use repr;
|
|
|
|
use vec;
|
|
|
|
|
2012-10-18 11:14:11 -05:00
|
|
|
let bytes = do io::with_bytes_writer |writer| {
|
2012-09-25 14:17:20 -05:00
|
|
|
repr::write_repr(writer, object);
|
|
|
|
};
|
2013-04-27 20:57:15 -05:00
|
|
|
|
|
|
|
match rt::context() {
|
|
|
|
rt::OldTaskContext => {
|
|
|
|
unsafe {
|
|
|
|
let len = bytes.len() as libc::size_t;
|
|
|
|
rustrt::rust_log_str(level, cast::transmute(vec::raw::to_ptr(bytes)), len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// XXX: Bad allocation
|
2013-05-04 19:30:31 -05:00
|
|
|
let msg = str::from_bytes(bytes);
|
2013-04-27 20:57:15 -05:00
|
|
|
newsched_log_str(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn newsched_log_str(msg: ~str) {
|
2013-05-19 18:50:21 -05:00
|
|
|
use rt::task::Task;
|
|
|
|
use rt::local::Local;
|
|
|
|
|
2012-09-25 14:17:20 -05:00
|
|
|
unsafe {
|
2013-05-19 18:50:21 -05:00
|
|
|
match Local::try_unsafe_borrow::<Task>() {
|
2013-04-27 20:57:15 -05: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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod rustrt {
|
|
|
|
use libc;
|
|
|
|
|
|
|
|
pub extern {
|
|
|
|
unsafe fn rust_log_console_on();
|
|
|
|
unsafe fn rust_log_console_off();
|
|
|
|
unsafe fn rust_log_str(level: u32,
|
|
|
|
string: *libc::c_char,
|
|
|
|
size: libc::size_t);
|
2012-09-25 14:17:20 -05:00
|
|
|
}
|
|
|
|
}
|