]> git.lizzy.rs Git - rust.git/commitdiff
std: Handle prints with literally no context
authorAlex Crichton <alex@alexcrichton.com>
Fri, 13 Dec 2013 01:32:35 +0000 (17:32 -0800)
committerAlex Crichton <alex@alexcrichton.com>
Tue, 24 Dec 2013 22:42:00 +0000 (14:42 -0800)
Printing is an incredibly useful debugging utility, and it's not much help if
your debugging prints just trigger an obscure abort when you need them most. In
order to handle this case, forcibly fall back to a libc::write implementation of
printing whenever a local task is not available.

Note that this is *not* a 1:1 fallback. All 1:1 rust tasks will still have a
local Task that it can go through (and stdio will be created through the local
IO factory), this is only a fallback for "no context" rust code (such as that
setting up the context).

src/libstd/io/stdio.rs
src/libstd/rt/util.rs

index 0adb83d2015354ce84f62c8699051f9fc8539a6c..88047aecda261d88be2cb5fe9d8775b8d62ca385 100644 (file)
@@ -132,7 +132,17 @@ fn with_task_stdout(f: |&mut Writer|) {
             }
 
             None => {
-                let mut io = stdout();
+                struct Stdout;
+                impl Writer for Stdout {
+                    fn write(&mut self, data: &[u8]) {
+                        unsafe {
+                            libc::write(libc::STDOUT_FILENO,
+                                        vec::raw::to_ptr(data) as *libc::c_void,
+                                        data.len() as libc::size_t);
+                        }
+                    }
+                }
+                let mut io = Stdout;
                 f(&mut io as &mut Writer);
             }
         }
index 93721986f3c259d88d89aea9f442165a38a4ffb8..2f3e5be39e68b333bf349ea4c7893cf2a14e722f 100644 (file)
@@ -68,11 +68,22 @@ pub fn default_sched_threads() -> uint {
 }
 
 pub fn dumb_println(args: &fmt::Arguments) {
-    use io::native::file::FileDesc;
     use io;
     use libc;
-    let mut out = FileDesc::new(libc::STDERR_FILENO, false);
-    fmt::writeln(&mut out as &mut io::Writer, args);
+    use vec;
+
+    struct Stderr;
+    impl io::Writer for Stderr {
+        fn write(&mut self, data: &[u8]) {
+            unsafe {
+                libc::write(libc::STDERR_FILENO,
+                            vec::raw::to_ptr(data) as *libc::c_void,
+                            data.len() as libc::size_t);
+            }
+        }
+    }
+    let mut w = Stderr;
+    fmt::writeln(&mut w as &mut io::Writer, args);
 }
 
 pub fn abort(msg: &str) -> ! {