about summary refs log tree commit diff
path: root/src/libstd/logging.rs
blob: 7de55f48317a556d8c66067fc0b010e5552779ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// 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.

//! Logging

use option::*;
use os;
use either::*;
use rt;
use rt::logging::{Logger, StdErrLogger};

/// Turns on logging to stdout globally
pub fn console_on() {
    rt::logging::console_on();
}

/**
 * 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
 */
pub fn console_off() {
    // If RUST_LOG is set then the console can't be turned off
    if os::getenv("RUST_LOG").is_some() {
        return;
    }

    rt::logging::console_off();
}

#[cfg(not(test))]
#[lang="log_type"]
#[allow(missing_doc)]
pub fn log_type<T>(_level: u32, object: &T) {
    use io;
    use repr;
    use str;

    let bytes = do io::with_bytes_writer |writer| {
        repr::write_repr(writer, object);
    };

    // XXX: Bad allocation
    let msg = str::from_bytes(bytes);
    newsched_log_str(msg);
}

fn newsched_log_str(msg: ~str) {
    use rt::task::Task;
    use rt::local::Local;

    unsafe {
        match Local::try_unsafe_borrow::<Task>() {
            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));
            }
        }
    }
}