1111// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
1212
1313use std:: fmt:: { Debug , Write } ;
14+ use std:: sync:: OnceLock ;
1415
1516use tracing:: field:: { Field , Visit } ;
1617use tracing:: Subscriber ;
@@ -65,6 +66,16 @@ impl tracing::Level {
6566 }
6667 }
6768
69+ fn web_logger_1 ( & self ) -> fn ( & JsValue ) {
70+ match * self {
71+ tracing:: Level :: TRACE => web_sys:: console:: trace_1,
72+ tracing:: Level :: DEBUG => web_sys:: console:: debug_1,
73+ tracing:: Level :: INFO => web_sys:: console:: info_1,
74+ tracing:: Level :: WARN => web_sys:: console:: warn_1,
75+ tracing:: Level :: ERROR => web_sys:: console:: error_1,
76+ }
77+ }
78+
6879 /// Return a pretty color theme for a `tracing::Level`.
6980 fn web_log_color ( & self ) -> & ' static str {
7081 match * self {
@@ -77,6 +88,12 @@ impl tracing::Level {
7788 }
7889}
7990
91+ static IS_CHROME : OnceLock < bool > = OnceLock :: new ( ) ;
92+
93+ fn detect_chrome ( ) -> bool {
94+ web_sys:: window ( ) . unwrap ( ) . get ( "chrome" ) . is_some ( )
95+ }
96+
8097#[ extend:: ext]
8198impl < ' a > tracing:: Metadata < ' a > {
8299 /// Log a message in the style of `WasmLogger`.
@@ -87,12 +104,16 @@ impl<'a> tracing::Metadata<'a> {
87104 . and_then ( |file| self . line ( ) . map ( |ln| format ! ( "{}:{}" , & file[ 11 ..] , ln) ) )
88105 . unwrap_or_default ( ) ;
89106
90- level. web_logger_4 ( ) (
91- & format ! ( "%c {} %c {}%c {} " , level, origin, msg) . into ( ) ,
92- & level. web_log_color ( ) . into ( ) ,
93- & "color: gray; font-style: italic" . into ( ) ,
94- & "color: inherit" . into ( ) ,
95- ) ;
107+ if * IS_CHROME . get_or_init ( detect_chrome) {
108+ level. web_logger_4 ( ) (
109+ & format ! ( "%c {} %c {}%c {} " , level, origin, msg) . into ( ) ,
110+ & level. web_log_color ( ) . into ( ) ,
111+ & "color: gray; font-style: italic" . into ( ) ,
112+ & "color: inherit" . into ( ) ,
113+ ) ;
114+ } else {
115+ level. web_logger_1 ( ) ( & format ! ( "{} {}" , origin, msg) . into ( ) ) ;
116+ }
96117 }
97118}
98119
@@ -185,16 +206,7 @@ impl<S: Subscriber + for<'a> LookupSpan<'a>> Layer<S> for WasmLogger {
185206/// `tracing::Subscriber`, so it should not be called when `perspective` is used
186207/// as a library from a larger app; in this case the app itself should configure
187208/// `tracing` explicitly.
188- ///
189- /// Why is this stubbed out for `--release` builds, you may ask? While `tracing`
190- /// has support for [compile-time log ellision](https://docs.rs/tracing/latest/tracing/level_filters/index.html)
191- /// (which is enabled), even at `"max_level_off"` there is a 80k binary
192- /// payload generated from `set_global_logging()` in wasm. As this module does
193- /// not currently use even `"error"` level logging in `--release` builds,
194- /// we stub this library out entirely to save bytes.
195- #[ cfg( debug_assertions) ]
196209pub fn set_global_logging ( ) {
197- use std:: sync:: OnceLock ;
198210 static INIT_LOGGING : OnceLock < ( ) > = OnceLock :: new ( ) ;
199211 INIT_LOGGING . get_or_init ( || {
200212 use tracing_subscriber:: layer:: SubscriberExt ;
@@ -210,6 +222,3 @@ pub fn set_global_logging() {
210222 tracing:: subscriber:: set_global_default ( subscriber) . unwrap ( ) ;
211223 } ) ;
212224}
213-
214- #[ cfg( not( debug_assertions) ) ]
215- pub fn set_global_logging ( ) { }
0 commit comments