core::rt Wire up logging to newsched tasks

This commit is contained in:
Brian Anderson 2013-04-27 18:57:15 -07:00
parent ad6719ee0b
commit 10355d7a7d
6 changed files with 118 additions and 26 deletions

View File

@ -246,8 +246,11 @@ mod unicode;
#[path = "num/cmath.rs"]
mod cmath;
mod stackwalk;
// XXX: This shouldn't be pub, and it should be reexported under 'unstable'
// but name resolution doesn't work without it being pub.
#[path = "rt/mod.rs"]
mod rt;
pub mod rt;
// A curious inner-module that's not exported that contains the binding
// 'core' so that macro-expanded references to core::error and such

View File

@ -10,17 +10,15 @@
//! Logging
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);
}
}
use option::*;
use either::*;
use rt;
use rt::logging::{Logger, StdErrLogger};
use io;
use libc;
use repr;
use vec;
use cast;
/// Turns on logging to stdout globally
pub fn console_on() {
@ -45,17 +43,51 @@ pub fn console_off() {
#[cfg(not(test))]
#[lang="log_type"]
pub fn log_type<T>(level: u32, object: &T) {
use cast::transmute;
use io;
use libc;
use repr;
use vec;
let bytes = do io::with_bytes_writer |writer| {
repr::write_repr(writer, object);
};
unsafe {
let len = bytes.len() as libc::size_t;
rustrt::rust_log_str(level, transmute(vec::raw::to_ptr(bytes)), len);
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
let msg = bytes.to_str();
newsched_log_str(msg);
}
}
}
fn newsched_log_str(msg: ~str) {
unsafe {
match rt::local_services::unsafe_try_borrow_local_services() {
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);
}
}

View File

@ -11,7 +11,7 @@
#[macro_escape];
// Some basic logging
macro_rules! rtdebug_ (
macro_rules! rtdebug (
($( $arg:expr),+) => ( {
dumb_println(fmt!( $($arg),+ ));
@ -26,7 +26,7 @@ macro_rules! rtdebug_ (
)
// An alternate version with no output, for turning off logging
macro_rules! rtdebug (
macro_rules! rtdebug_ (
($( $arg:expr),+) => ( $(let _ = $arg)*; )
)

View File

@ -23,19 +23,19 @@ use libc::{c_void, uintptr_t};
use cast::transmute;
use super::sched::local_sched;
use super::local_heap::LocalHeap;
use rt::logging::{Logger, StdErrLogger};
pub struct LocalServices {
heap: LocalHeap,
gc: GarbageCollector,
storage: LocalStorage,
logger: Logger,
logger: StdErrLogger,
unwinder: Option<Unwinder>,
destroyed: bool
}
pub struct GarbageCollector;
pub struct LocalStorage(*c_void, Option<~fn(*c_void)>);
pub struct Logger;
pub struct Unwinder {
unwinding: bool,
@ -47,7 +47,7 @@ impl LocalServices {
heap: LocalHeap::new(),
gc: GarbageCollector,
storage: LocalStorage(ptr::null(), None),
logger: Logger,
logger: StdErrLogger,
unwinder: Some(Unwinder { unwinding: false }),
destroyed: false
}
@ -58,7 +58,7 @@ impl LocalServices {
heap: LocalHeap::new(),
gc: GarbageCollector,
storage: LocalStorage(ptr::null(), None),
logger: Logger,
logger: StdErrLogger,
unwinder: None,
destroyed: false
}
@ -184,6 +184,14 @@ pub unsafe fn unsafe_borrow_local_services() -> *mut LocalServices {
}
}
pub unsafe fn unsafe_try_borrow_local_services() -> Option<*mut LocalServices> {
if local_sched::exists() {
Some(unsafe_borrow_local_services())
} else {
None
}
}
#[cfg(test)]
mod test {
use rt::test::*;
@ -231,4 +239,12 @@ mod test {
let _ = r.next();
}
}
#[test]
fn logging() {
do run_in_newsched_task() {
info!("here i am. logging in a newsched task");
}
}
}

38
src/libcore/rt/logging.rs Normal file
View File

@ -0,0 +1,38 @@
// Copyright 2013 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.
use either::*;
pub trait Logger {
fn log(&mut self, msg: Either<~str, &'static str>);
}
pub struct StdErrLogger;
impl Logger for StdErrLogger {
fn log(&mut self, msg: Either<~str, &'static str>) {
use io::{Writer, WriterUtil};
let s: &str = match msg {
Left(ref s) => {
let s: &str = *s;
s
}
Right(ref s) => {
let s: &str = *s;
s
}
};
let dbg = ::libc::STDERR_FILENO as ::io::fd_t;
dbg.write_str(s);
dbg.write_str("\n");
dbg.flush();
}
}

View File

@ -58,6 +58,9 @@ pub mod env;
/// The local, managed heap
mod local_heap;
/// The Logger trait and implementations
pub mod logging;
/// Tools for testing the runtime
#[cfg(test)]
pub mod test;