Skip to content

Commit 10355d7

Browse files
committed
core::rt Wire up logging to newsched tasks
1 parent ad6719e commit 10355d7

File tree

6 files changed

+117
-25
lines changed

6 files changed

+117
-25
lines changed

src/libcore/core.rc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,11 @@ mod unicode;
246246
#[path = "num/cmath.rs"]
247247
mod cmath;
248248
mod stackwalk;
249+
250+
// XXX: This shouldn't be pub, and it should be reexported under 'unstable'
251+
// but name resolution doesn't work without it being pub.
249252
#[path = "rt/mod.rs"]
250-
mod rt;
253+
pub mod rt;
251254

252255
// A curious inner-module that's not exported that contains the binding
253256
// 'core' so that macro-expanded references to core::error and such

src/libcore/logging.rs

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@
1010

1111
//! Logging
1212
13-
pub mod rustrt {
14-
use libc;
15-
16-
pub extern {
17-
unsafe fn rust_log_console_on();
18-
unsafe fn rust_log_console_off();
19-
unsafe fn rust_log_str(level: u32,
20-
string: *libc::c_char,
21-
size: libc::size_t);
22-
}
23-
}
13+
use option::*;
14+
use either::*;
15+
use rt;
16+
use rt::logging::{Logger, StdErrLogger};
17+
use io;
18+
use libc;
19+
use repr;
20+
use vec;
21+
use cast;
2422

2523
/// Turns on logging to stdout globally
2624
pub fn console_on() {
@@ -45,17 +43,51 @@ pub fn console_off() {
4543
#[cfg(not(test))]
4644
#[lang="log_type"]
4745
pub fn log_type<T>(level: u32, object: &T) {
48-
use cast::transmute;
49-
use io;
50-
use libc;
51-
use repr;
52-
use vec;
5346

5447
let bytes = do io::with_bytes_writer |writer| {
5548
repr::write_repr(writer, object);
5649
};
50+
51+
match rt::context() {
52+
rt::OldTaskContext => {
53+
unsafe {
54+
let len = bytes.len() as libc::size_t;
55+
rustrt::rust_log_str(level, cast::transmute(vec::raw::to_ptr(bytes)), len);
56+
}
57+
}
58+
_ => {
59+
// XXX: Bad allocation
60+
let msg = bytes.to_str();
61+
newsched_log_str(msg);
62+
}
63+
}
64+
}
65+
66+
fn newsched_log_str(msg: ~str) {
67+
5768
unsafe {
58-
let len = bytes.len() as libc::size_t;
59-
rustrt::rust_log_str(level, transmute(vec::raw::to_ptr(bytes)), len);
69+
match rt::local_services::unsafe_try_borrow_local_services() {
70+
Some(local) => {
71+
// Use the available logger
72+
(*local).logger.log(Left(msg));
73+
}
74+
None => {
75+
// There is no logger anywhere, just write to stderr
76+
let mut logger = StdErrLogger;
77+
logger.log(Left(msg));
78+
}
79+
}
80+
}
81+
}
82+
83+
pub mod rustrt {
84+
use libc;
85+
86+
pub extern {
87+
unsafe fn rust_log_console_on();
88+
unsafe fn rust_log_console_off();
89+
unsafe fn rust_log_str(level: u32,
90+
string: *libc::c_char,
91+
size: libc::size_t);
6092
}
6193
}

src/libcore/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#[macro_escape];
1212

1313
// Some basic logging
14-
macro_rules! rtdebug_ (
14+
macro_rules! rtdebug (
1515
($( $arg:expr),+) => ( {
1616
dumb_println(fmt!( $($arg),+ ));
1717

@@ -26,7 +26,7 @@ macro_rules! rtdebug_ (
2626
)
2727

2828
// An alternate version with no output, for turning off logging
29-
macro_rules! rtdebug (
29+
macro_rules! rtdebug_ (
3030
($( $arg:expr),+) => ( $(let _ = $arg)*; )
3131
)
3232

src/libcore/rt/local_services.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ use libc::{c_void, uintptr_t};
2323
use cast::transmute;
2424
use super::sched::local_sched;
2525
use super::local_heap::LocalHeap;
26+
use rt::logging::{Logger, StdErrLogger};
2627

2728
pub struct LocalServices {
2829
heap: LocalHeap,
2930
gc: GarbageCollector,
3031
storage: LocalStorage,
31-
logger: Logger,
32+
logger: StdErrLogger,
3233
unwinder: Option<Unwinder>,
3334
destroyed: bool
3435
}
3536

3637
pub struct GarbageCollector;
3738
pub struct LocalStorage(*c_void, Option<~fn(*c_void)>);
38-
pub struct Logger;
3939

4040
pub struct Unwinder {
4141
unwinding: bool,
@@ -47,7 +47,7 @@ impl LocalServices {
4747
heap: LocalHeap::new(),
4848
gc: GarbageCollector,
4949
storage: LocalStorage(ptr::null(), None),
50-
logger: Logger,
50+
logger: StdErrLogger,
5151
unwinder: Some(Unwinder { unwinding: false }),
5252
destroyed: false
5353
}
@@ -58,7 +58,7 @@ impl LocalServices {
5858
heap: LocalHeap::new(),
5959
gc: GarbageCollector,
6060
storage: LocalStorage(ptr::null(), None),
61-
logger: Logger,
61+
logger: StdErrLogger,
6262
unwinder: None,
6363
destroyed: false
6464
}
@@ -184,6 +184,14 @@ pub unsafe fn unsafe_borrow_local_services() -> *mut LocalServices {
184184
}
185185
}
186186
187+
pub unsafe fn unsafe_try_borrow_local_services() -> Option<*mut LocalServices> {
188+
if local_sched::exists() {
189+
Some(unsafe_borrow_local_services())
190+
} else {
191+
None
192+
}
193+
}
194+
187195
#[cfg(test)]
188196
mod test {
189197
use rt::test::*;
@@ -231,4 +239,12 @@ mod test {
231239
let _ = r.next();
232240
}
233241
}
242+
243+
#[test]
244+
fn logging() {
245+
do run_in_newsched_task() {
246+
info!("here i am. logging in a newsched task");
247+
}
248+
}
234249
}
250+

src/libcore/rt/logging.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use either::*;
12+
13+
pub trait Logger {
14+
fn log(&mut self, msg: Either<~str, &'static str>);
15+
}
16+
17+
pub struct StdErrLogger;
18+
19+
impl Logger for StdErrLogger {
20+
fn log(&mut self, msg: Either<~str, &'static str>) {
21+
use io::{Writer, WriterUtil};
22+
23+
let s: &str = match msg {
24+
Left(ref s) => {
25+
let s: &str = *s;
26+
s
27+
}
28+
Right(ref s) => {
29+
let s: &str = *s;
30+
s
31+
}
32+
};
33+
let dbg = ::libc::STDERR_FILENO as ::io::fd_t;
34+
dbg.write_str(s);
35+
dbg.write_str("\n");
36+
dbg.flush();
37+
}
38+
}

src/libcore/rt/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ pub mod env;
5858
/// The local, managed heap
5959
mod local_heap;
6060

61+
/// The Logger trait and implementations
62+
pub mod logging;
63+
6164
/// Tools for testing the runtime
6265
#[cfg(test)]
6366
pub mod test;

0 commit comments

Comments
 (0)