1- #[ cfg( feature = "binary-logs" ) ]
2- mod str_buf;
3-
41#[ cfg( feature = "binary-logs" ) ]
52mod binary_logs {
6- use core:: { ffi:: VaListImpl , fmt:: Write } ;
7-
83 use log:: info;
94
10- use super :: str_buf:: StrBuf ;
5+ extern "C" {
6+ fn vsnprintf ( dst : * mut u8 , _n : u32 , format : * const u8 , ...) -> i32 ;
7+ }
118
129 #[ no_mangle]
1310 pub unsafe extern "C" fn phy_printf ( _format : * const u8 , _args: ...) {
@@ -29,109 +26,10 @@ mod binary_logs {
2926 pub unsafe extern "C" fn syslog ( format : * const u8 , args : VaListImpl ) {
3027 let mut buf = [ 0u8 ; 512 ] ;
3128 vsnprintf ( & mut buf as * mut u8 , 511 , format, args) ;
32- let res_str = StrBuf :: from ( & buf as * const u8 ) ;
33- info ! ( "{}" , res_str. as_str_ref( ) ) ;
34- }
35-
36- pub ( crate ) unsafe fn vsnprintf (
37- dst : * mut u8 ,
38- _n : u32 ,
39- format : * const u8 ,
40- mut args : VaListImpl ,
41- ) -> i32 {
42- let fmt_str_ptr = format;
43-
44- let mut res_str = StrBuf :: new ( ) ;
45-
46- let strbuf = StrBuf :: from ( fmt_str_ptr) ;
47- let s = strbuf. as_str_ref ( ) ;
48-
49- let mut format_char = ' ' ;
50- let mut is_long = false ;
51- let mut found = false ;
52- for c in s. chars ( ) {
53- if !found {
54- if c == '%' {
55- found = true ;
56- }
57-
58- if !found {
59- res_str. append_char ( c) ;
60- }
61- } else if c. is_numeric ( ) || c == '-' || c == 'l' {
62- if c == 'l' {
63- is_long = true ;
64- }
65- // ignore
66- } else {
67- // a format char
68- format_char = c;
69- }
70-
71- if found && format_char != ' ' {
72- // have to format an arg
73- match format_char {
74- 'd' => {
75- if is_long {
76- let v = args. arg :: < i64 > ( ) ;
77- write ! ( res_str, "{}" , v) . ok ( ) ;
78- } else {
79- let v = args. arg :: < i32 > ( ) ;
80- write ! ( res_str, "{}" , v) . ok ( ) ;
81- }
82- }
83-
84- 'u' => {
85- let v = args. arg :: < u32 > ( ) ;
86- write ! ( res_str, "{}" , v) . ok ( ) ;
87- }
88-
89- 'p' => {
90- let v = args. arg :: < u32 > ( ) ;
91- write ! ( res_str, "0x{:x}" , v) . ok ( ) ;
92- }
93-
94- 'X' => {
95- let v = args. arg :: < u32 > ( ) ;
96- write ! ( res_str, "{:02x}" , ( v & 0xff000000 ) >> 24 ) . ok ( ) ;
97- }
98-
99- 'x' => {
100- let v = args. arg :: < u32 > ( ) ;
101- write ! ( res_str, "{:02x}" , v) . ok ( ) ;
102- }
103-
104- 's' => {
105- let v = args. arg :: < u32 > ( ) as * const u8 ;
106- let vbuf = StrBuf :: from ( v) ;
107- write ! ( res_str, "{}" , vbuf. as_str_ref( ) ) . ok ( ) ;
108- }
109-
110- 'c' => {
111- let v = args. arg :: < u8 > ( ) ;
112- if v != 0 {
113- write ! ( res_str, "{}" , v as char ) . ok ( ) ;
114- }
115- }
116-
117- _ => {
118- write ! ( res_str, "<UNKNOWN{}>" , format_char) . ok ( ) ;
119- }
120- }
121-
122- format_char = ' ' ;
123- found = false ;
124- is_long = false ;
125- }
126- }
127- let mut idx = 0 ;
128- res_str. as_str_ref ( ) . chars ( ) . for_each ( |c| {
129- * ( dst. offset ( idx) ) = c as u8 ;
130- idx += 1 ;
131- } ) ;
132- * ( dst. offset ( idx) ) = 0 ;
133-
134- idx as i32
29+ let res_str = core:: ffi:: CStr :: from_ptr ( core:: ptr:: addr_of!( buf) . cast ( ) )
30+ . to_str ( )
31+ . unwrap ( ) ;
32+ info ! ( "{}" , res_str) ;
13533 }
13634}
13735
0 commit comments