11use :: winapi:: um:: debugapi:: { OutputDebugStringA , OutputDebugStringW } ;
22
3- #[ expect( dead_code, reason = "test me!" ) ]
4- pub ( crate ) fn encode_unicode ( msg : & str ) {
3+ /// Output a Unicode debug string.
4+ ///
5+ /// OutputDebugStringW is... weird/standard Microsoft:
6+ /// > `OutputDebugStringW` converts the specified string based on the current
7+ /// > system locale information and passes it to `OutputDebugStringA` to be
8+ /// > displayed. As a result, some Unicode characters may not be displayed
9+ /// > correctly.
10+ ///
11+ /// See <https://learn.microsoft.com/en-us/windows/win32/api/debugapi/nf-debugapi-outputdebugstringw>.
12+ ///
13+ /// Although you shouldn't log a lot of stuff, if you need to, the ASCII
14+ /// version may be slightly faster.
15+ pub ( crate ) fn output_debug_string_w ( msg : & str ) {
516 let now = time:: OffsetDateTime :: now_utc ( ) ;
617 let s = format ! (
718 "[ZF {:04}-{:02}-{:02}T{:02}:{:02}:{:02}.{:03}Z] {msg}\0 " ,
@@ -16,19 +27,27 @@ pub(crate) fn encode_unicode(msg: &str) {
1627
1728 let v: Vec < u16 > = s. encode_utf16 ( ) . collect ( ) ;
1829 let p: * const u16 = v. as_ptr ( ) ;
19- // OutputDebugStringW is... weird/standard Microsoft:
20- // > `OutputDebugStringW` converts the specified string based on the current
21- // > system locale information and passes it to `OutputDebugStringA` to be
22- // > displayed. As a result, some Unicode characters may not be displayed
23- // > correctly.
24- // https://learn.microsoft.com/en-us/windows/win32/api/debugapi/nf-debugapi-outputdebugstringw
25- // Therefore, we may be better off just using OutputDebugStringA...
2630 unsafe { OutputDebugStringW ( p) } ;
2731 // paranoia: ensure `v` is valid until after `OutputDebugStringW`
2832 drop ( v) ;
2933}
3034
31- pub ( crate ) fn encode_ascii ( msg : & str ) {
35+ fn encode_ascii ( s : & str ) -> Vec < i8 > {
36+ s. chars ( )
37+ . map ( |c| {
38+ let b = if c. is_ascii ( ) { c as u8 } else { b'?' } ;
39+ b as i8
40+ } )
41+ . collect ( )
42+ }
43+
44+ /// Output an ASCII debug string.
45+ ///
46+ /// Non-ASCII characters are replaced by `?`. This version may be slightly
47+ /// faster than the Unicode version, as it avoids extra translation (due to
48+ /// Microsoft Unicode ineptness).
49+ #[ allow( dead_code, reason = "Use Unicode version by default" ) ]
50+ pub ( crate ) fn output_debug_string_a ( msg : & str ) {
3251 let now = time:: OffsetDateTime :: now_utc ( ) ;
3352 let s = format ! (
3453 "[ZF {:04}-{:02}-{:02}T{:02}:{:02}:{:02}.{:03}Z] {msg}\0 " ,
@@ -41,27 +60,21 @@ pub(crate) fn encode_ascii(msg: &str) {
4160 now. millisecond( ) ,
4261 ) ;
4362
44- let v: Vec < i8 > = s
45- . chars ( )
46- . map ( |c| {
47- let b = if c. is_ascii ( ) { c as u8 } else { b'?' } ;
48- b as i8
49- } )
50- . collect ( ) ;
63+ let v: Vec < i8 > = encode_ascii ( & s) ;
5164 let p: * const i8 = v. as_ptr ( ) ;
5265 unsafe { OutputDebugStringA ( p) } ;
5366 // paranoia: ensure `s` is valid until after `OutputDebugStringA`
5467 drop ( s) ;
5568}
5669
5770macro_rules! output {
58- ( $fmt: literal $( , $args: expr) * $( , ) ?) => { {
71+ ( a $fmt: literal $( , $args: expr) * $( , ) ?) => { {
5972 let msg: String = format!( $fmt $( , $args) * ) ;
60- $crate:: dbg:: encode_ascii ( & msg) ;
73+ $crate:: dbg:: output_debug_string_a ( & msg) ;
6174 } } ;
62- ( u $fmt: literal $( , $args: expr) * $( , ) ?) => { {
75+ ( $fmt: literal $( , $args: expr) * $( , ) ?) => { {
6376 let msg: String = format!( $fmt $( , $args) * ) ;
64- $crate:: dbg:: encode_unicode ( & msg) ;
77+ $crate:: dbg:: output_debug_string_w ( & msg) ;
6578 } } ;
6679}
6780pub ( crate ) use output;
0 commit comments