std::logging: Use a more specific enum than Either
This commit is contained in:
parent
b4eff79f38
commit
830ac37ca2
@ -12,9 +12,8 @@
|
||||
|
||||
use option::*;
|
||||
use os;
|
||||
use either::*;
|
||||
use rt;
|
||||
use rt::logging::{Logger, StdErrLogger};
|
||||
use rt::logging::{Logger, StdErrLogger, OwnedString};
|
||||
|
||||
/// Turns on logging to stdout globally
|
||||
pub fn console_on() {
|
||||
@ -57,12 +56,12 @@ fn newsched_log_str(msg: ~str) {
|
||||
match optional_task {
|
||||
Some(local) => {
|
||||
// Use the available logger
|
||||
(*local).logger.log(Left(msg));
|
||||
(*local).logger.log(OwnedString(msg));
|
||||
}
|
||||
None => {
|
||||
// There is no logger anywhere, just write to stderr
|
||||
let mut logger = StdErrLogger;
|
||||
logger.log(Left(msg));
|
||||
logger.log(OwnedString(msg));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,6 @@
|
||||
// <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.
|
||||
use either::*;
|
||||
use libc::{uintptr_t, exit, STDERR_FILENO};
|
||||
use option::{Some, None, Option};
|
||||
use rt::util::dumb_println;
|
||||
@ -168,14 +167,20 @@ fn update_log_settings(crate_map: *u8, settings: ~str) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Represent a string with `Send` bound.
|
||||
pub enum SendableString {
|
||||
OwnedString(~str),
|
||||
StaticString(&'static str)
|
||||
}
|
||||
|
||||
pub trait Logger {
|
||||
fn log(&mut self, msg: Either<~str, &'static str>);
|
||||
fn log(&mut self, msg: SendableString);
|
||||
}
|
||||
|
||||
pub struct StdErrLogger;
|
||||
|
||||
impl Logger for StdErrLogger {
|
||||
fn log(&mut self, msg: Either<~str, &'static str>) {
|
||||
fn log(&mut self, msg: SendableString) {
|
||||
use io::{Writer, WriterUtil};
|
||||
|
||||
if !should_log_console() {
|
||||
@ -183,14 +188,11 @@ fn log(&mut self, msg: Either<~str, &'static str>) {
|
||||
}
|
||||
|
||||
let s: &str = match msg {
|
||||
Left(ref s) => {
|
||||
let s: &str = *s;
|
||||
s
|
||||
}
|
||||
Right(ref s) => {
|
||||
let s: &str = *s;
|
||||
s
|
||||
}
|
||||
OwnedString(ref s) => {
|
||||
let slc: &str = *s;
|
||||
slc
|
||||
},
|
||||
StaticString(s) => s,
|
||||
};
|
||||
|
||||
// Truncate the string
|
||||
|
@ -136,12 +136,11 @@ fn fail_with(cause: &'static str, file: &'static str, line: uint) -> ! {
|
||||
|
||||
// FIXME #4427: Temporary until rt::rt_fail_ goes away
|
||||
pub fn begin_unwind_(msg: *c_char, file: *c_char, line: size_t) -> ! {
|
||||
use either::Left;
|
||||
use option::{Some, None};
|
||||
use rt::in_green_task_context;
|
||||
use rt::task::Task;
|
||||
use rt::local::Local;
|
||||
use rt::logging::Logger;
|
||||
use rt::logging::{Logger, OwnedString};
|
||||
use str::Str;
|
||||
|
||||
unsafe {
|
||||
@ -164,7 +163,7 @@ pub fn begin_unwind_(msg: *c_char, file: *c_char, line: size_t) -> ! {
|
||||
msg, file, line as int)
|
||||
};
|
||||
|
||||
task.logger.log(Left(msg));
|
||||
task.logger.log(OwnedString(msg));
|
||||
}
|
||||
} else {
|
||||
rterrln!("failed in non-task context at '%s', %s:%i",
|
||||
|
Loading…
Reference in New Issue
Block a user