瀏覽代碼

Add print helpers

Gary Guo 3 年之前
父節點
當前提交
487ca98456
共有 3 個文件被更改,包括 59 次插入1 次删除
  1. 1 0
      Cargo.toml
  2. 7 1
      src/lib.rs
  3. 51 0
      src/print.rs

+ 1 - 0
Cargo.toml

@@ -20,6 +20,7 @@ dwarf-expr = []
 hide-trace = []
 personality = []
 personality-dummy = []
+print = ["libc"]
 system-alloc = []
 default = ["dwarf-expr", "hide-trace", "fde-phdr", "fde-registry"]
 

+ 7 - 1
src/lib.rs

@@ -1,7 +1,10 @@
 #![feature(c_unwind)]
 #![feature(naked_functions)]
 #![feature(asm)]
-#![cfg_attr(any(feature = "personality", feature = "personality-dummy"), feature(lang_items))]
+#![cfg_attr(
+    any(feature = "personality", feature = "personality-dummy"),
+    feature(lang_items)
+)]
 #![warn(rust_2018_idioms)]
 #![warn(unsafe_op_in_unsafe_fn)]
 #![no_std]
@@ -15,6 +18,9 @@ mod find_fde;
 mod frame;
 mod util;
 
+#[cfg(feature = "print")]
+pub mod print;
+
 #[cfg(feature = "personality")]
 mod personality;
 #[cfg(feature = "personality-dummy")]

+ 51 - 0
src/print.rs

@@ -0,0 +1,51 @@
+pub use crate::{eprint, eprintln, print, println};
+
+#[doc(hidden)]
+pub struct StdoutPrinter;
+impl core::fmt::Write for StdoutPrinter {
+    fn write_str(&mut self, s: &str) -> core::fmt::Result {
+        unsafe { libc::printf(b"%.*s\0".as_ptr() as _, s.len() as i32, s.as_ptr()) };
+        Ok(())
+    }
+}
+
+#[doc(hidden)]
+pub struct StderrPrinter;
+impl core::fmt::Write for StderrPrinter {
+    fn write_str(&mut self, s: &str) -> core::fmt::Result {
+        unsafe { libc::write(libc::STDERR_FILENO, s.as_ptr() as _, s.len() as _) };
+        Ok(())
+    }
+}
+
+#[macro_export]
+macro_rules! println {
+    ($($arg:tt)*) => ({
+        use core::fmt::Write;
+        let _ = core::writeln!($crate::print::StdoutPrinter, $($arg)*);
+    })
+}
+
+#[macro_export]
+macro_rules! print {
+    ($($arg:tt)*) => ({
+        use core::fmt::Write;
+        let _ = core::writeln!($crate::print::StdoutPrinter, $($arg)*);
+    })
+}
+
+#[macro_export]
+macro_rules! eprintln {
+    ($($arg:tt)*) => ({
+        use core::fmt::Write;
+        let _ = core::writeln!($crate::print::StderrPrinter, $($arg)*);
+    })
+}
+
+#[macro_export]
+macro_rules! eprint {
+    ($($arg:tt)*) => ({
+        use core::fmt::Write;
+        let _ = core::writeln!($crate::print::StderrPrinter, $($arg)*);
+    })
+}