rust/src/libstd/logging.rs
Marvin Löbel 76c3e8a38c Add an SendStr type
A SendStr is a string that can hold either a ~str or a &'static str.
This can be useful as an optimization when an allocation is sometimes needed but the common case is statically known.

Possible use cases include Maps with both static and owned keys, or propagating error messages across task boundaries.

SendStr implements most basic traits in a way that hides the fact that it is an enum; in particular things like order and equality are only determined by the content of the wrapped strings.

Replaced std::rt:logging::SendableString with SendStr
Added tests for using an SendStr as key in Hash- and Treemaps
2013-09-16 16:57:50 +02:00

77 lines
2.0 KiB
Rust

// 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 rt;
use rt::logging::{Logger, StdErrLogger};
use send_str::SendStrOwned;
/// 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), stage0)]
#[lang="log_type"]
#[allow(missing_doc)]
pub fn log_type<T>(_level: u32, object: &T) {
use sys;
// XXX: Bad allocation
let msg = sys::log_str(object);
newsched_log_str(msg);
}
fn newsched_log_str(msg: ~str) {
use rt::task::Task;
use rt::local::Local;
unsafe {
let optional_task: Option<*mut Task> = Local::try_unsafe_borrow();
match optional_task {
Some(local) => {
// Use the available logger
(*local).logger.log(SendStrOwned(msg));
}
None => {
// There is no logger anywhere, just write to stderr
let mut logger = StdErrLogger;
logger.log(SendStrOwned(msg));
}
}
}
}
// XXX: This will change soon to not require an allocation. This is an unstable
// api which should not be used outside of the macros in ext/expand.
#[doc(hidden)]
pub fn log(_level: u32, msg: ~str) {
newsched_log_str(msg);
}