@@ -4,12 +4,16 @@ use core::fmt;
4
4
use crate :: bindings;
5
5
use crate :: c_types:: c_int;
6
6
7
+ pub use crate :: bindings:: {
8
+ KERN_ALERT , KERN_CRIT , KERN_DEBUG , KERN_EMERG , KERN_ERR , KERN_INFO , KERN_NOTICE , KERN_WARNING ,
9
+ } ;
10
+
7
11
#[ doc( hidden) ]
8
- pub fn printk ( s : & [ u8 ] ) {
12
+ pub fn printk ( s : & [ u8 ] , level : & ' static [ u8 ; 3usize ] ) {
9
13
// Don't copy the trailing NUL from `KERN_INFO`.
10
14
let mut fmt_str = [ 0 ; bindings:: KERN_INFO . len ( ) - 1 + b"%.*s\0 " . len ( ) ] ;
11
15
fmt_str[ ..bindings:: KERN_INFO . len ( ) - 1 ]
12
- . copy_from_slice ( & bindings :: KERN_INFO [ ..bindings:: KERN_INFO . len ( ) - 1 ] ) ;
16
+ . copy_from_slice ( & level [ ..bindings:: KERN_INFO . len ( ) - 1 ] ) ;
13
17
fmt_str[ bindings:: KERN_INFO . len ( ) - 1 ..] . copy_from_slice ( b"%.*s\0 " ) ;
14
18
15
19
// TODO: I believe printk never fails
@@ -56,15 +60,36 @@ impl fmt::Write for LogLineWriter {
56
60
#[ macro_export]
57
61
macro_rules! println {
58
62
( ) => ( {
59
- $crate:: printk:: printk( "\n " . as_bytes( ) ) ;
63
+ $crate:: printk:: printk( "\n " . as_bytes( ) , $crate:: printk:: KERN_INFO ) ;
64
+ } ) ;
65
+ ( $fmt: expr) => ( {
66
+ $crate:: printk:: printk( concat!( $fmt, "\n " ) . as_bytes( ) , $crate:: printk:: KERN_INFO ) ;
67
+ } ) ;
68
+ ( $fmt: expr, $( $arg: tt) * ) => ( {
69
+ use :: core:: fmt;
70
+ let mut writer = $crate:: printk:: LogLineWriter :: new( ) ;
71
+ let _ = fmt:: write( & mut writer, format_args!( concat!( $fmt, "\n " ) , $( $arg) * ) ) . unwrap( ) ;
72
+ $crate:: printk:: printk( writer. as_bytes( ) , $crate:: printk:: KERN_INFO ) ;
73
+ } ) ;
74
+ }
75
+
76
+ /// [`eprintln!`] functions the same as it does in `std`, except instead of
77
+ /// printing to `stdout`, it writes to the kernel console at the `KERN_ERR`
78
+ /// level.
79
+ ///
80
+ /// [`eprintln!`]: https://doc.rust-lang.org/stable/std/macro.eprintln.html
81
+ #[ macro_export]
82
+ macro_rules! eprintln {
83
+ ( ) => ( {
84
+ $crate:: printk:: printk( "\n " . as_bytes( ) , $crate:: printk:: KERN_ERR ) ;
60
85
} ) ;
61
86
( $fmt: expr) => ( {
62
- $crate:: printk:: printk( concat!( $fmt, "\n " ) . as_bytes( ) ) ;
87
+ $crate:: printk:: printk( concat!( $fmt, "\n " ) . as_bytes( ) , $crate :: printk :: KERN_ERR ) ;
63
88
} ) ;
64
89
( $fmt: expr, $( $arg: tt) * ) => ( {
65
90
use :: core:: fmt;
66
91
let mut writer = $crate:: printk:: LogLineWriter :: new( ) ;
67
92
let _ = fmt:: write( & mut writer, format_args!( concat!( $fmt, "\n " ) , $( $arg) * ) ) . unwrap( ) ;
68
- $crate:: printk:: printk( writer. as_bytes( ) ) ;
93
+ $crate:: printk:: printk( writer. as_bytes( ) , $crate :: printk :: KERN_ERR ) ;
69
94
} ) ;
70
95
}
0 commit comments