about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBrian Anderson <banderson@mozilla.com>2013-04-27 18:57:15 -0700
committerBrian Anderson <banderson@mozilla.com>2013-05-14 14:52:06 -0700
commit10355d7a7d1ba87d49149e5fcdc5d3955932afe2 (patch)
tree880aee8c5acbf1c75cf28b59de580836a9a3a717
parentad6719ee0b4977c949a34b7a2250ed7274cf442e (diff)
downloadrust-10355d7a7d1ba87d49149e5fcdc5d3955932afe2.tar.gz
rust-10355d7a7d1ba87d49149e5fcdc5d3955932afe2.zip
core::rt Wire up logging to newsched tasks
-rw-r--r--src/libcore/core.rc5
-rw-r--r--src/libcore/logging.rs68
-rw-r--r--src/libcore/macros.rs4
-rw-r--r--src/libcore/rt/local_services.rs24
-rw-r--r--src/libcore/rt/logging.rs38
-rw-r--r--src/libcore/rt/mod.rs3
6 files changed, 117 insertions, 25 deletions
diff --git a/src/libcore/core.rc b/src/libcore/core.rc
index d029fbc07f6..073a6dcf4d2 100644
--- a/src/libcore/core.rc
+++ b/src/libcore/core.rc
@@ -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
diff --git a/src/libcore/logging.rs b/src/libcore/logging.rs
index 69ecad56a8f..4ecd72ebd0c 100644
--- a/src/libcore/logging.rs
+++ b/src/libcore/logging.rs
@@ -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);
     };
+
+    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 {
-        let len = bytes.len() as libc::size_t;
-        rustrt::rust_log_str(level, transmute(vec::raw::to_ptr(bytes)), len);
+        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);
     }
 }
diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs
index b2e94f327c8..c4f0384f71e 100644
--- a/src/libcore/macros.rs
+++ b/src/libcore/macros.rs
@@ -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)*; )
 )
 
diff --git a/src/libcore/rt/local_services.rs b/src/libcore/rt/local_services.rs
index 94840d7b5d5..b8bf6e06780 100644
--- a/src/libcore/rt/local_services.rs
+++ b/src/libcore/rt/local_services.rs
@@ -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");
+        }
+    }
 }
+
diff --git a/src/libcore/rt/logging.rs b/src/libcore/rt/logging.rs
new file mode 100644
index 00000000000..4ed09fd829f
--- /dev/null
+++ b/src/libcore/rt/logging.rs
@@ -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();
+    }
+}
\ No newline at end of file
diff --git a/src/libcore/rt/mod.rs b/src/libcore/rt/mod.rs
index b5fba51ca7f..c7cdb277247 100644
--- a/src/libcore/rt/mod.rs
+++ b/src/libcore/rt/mod.rs
@@ -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;