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