|
87 | 87 | use log::{Level, Log, Metadata, Record, SetLoggerError};
|
88 | 88 | use web_sys::console;
|
89 | 89 |
|
| 90 | +#[cfg(feature = "color")] |
| 91 | +use wasm_bindgen::JsValue; |
| 92 | + |
| 93 | +#[cfg(feature = "color")] |
| 94 | +const STYLE: style::Style<'static> = style::Style::default(); |
| 95 | + |
| 96 | +#[cfg(feature = "color")] |
| 97 | +mod style; |
| 98 | + |
90 | 99 | static LOGGER: WebConsoleLogger = WebConsoleLogger {};
|
91 | 100 |
|
92 | 101 | struct WebConsoleLogger {}
|
@@ -119,16 +128,60 @@ impl Log for WebConsoleLogger {
|
119 | 128 | /// .apply()?;
|
120 | 129 | /// ```
|
121 | 130 | pub fn log(record: &Record) {
|
122 |
| - // pick the console.log() variant for the appropriate logging level |
123 |
| - let console_log = match record.level() { |
124 |
| - Level::Error => console::error_1, |
125 |
| - Level::Warn => console::warn_1, |
126 |
| - Level::Info => console::info_1, |
127 |
| - Level::Debug => console::log_1, |
128 |
| - Level::Trace => console::debug_1, |
129 |
| - }; |
130 |
| - |
131 |
| - console_log(&format!("{}", record.args()).into()); |
| 131 | + #[cfg(not(feature = "color"))] |
| 132 | + { |
| 133 | + // pick the console.log() variant for the appropriate logging level |
| 134 | + let console_log = match record.level() { |
| 135 | + Level::Error => console::error_1, |
| 136 | + Level::Warn => console::warn_1, |
| 137 | + Level::Info => console::info_1, |
| 138 | + Level::Debug => console::log_1, |
| 139 | + Level::Trace => console::debug_1, |
| 140 | + }; |
| 141 | + |
| 142 | + console_log(&format!("{}", record.args()).into()); |
| 143 | + } |
| 144 | + |
| 145 | + #[cfg(feature = "color")] |
| 146 | + { |
| 147 | + // pick the console.log() variant for the appropriate logging level |
| 148 | + let console_log = match record.level() { |
| 149 | + Level::Error => console::error_4, |
| 150 | + Level::Warn => console::warn_4, |
| 151 | + Level::Info => console::info_4, |
| 152 | + Level::Debug => console::log_4, |
| 153 | + Level::Trace => console::debug_4, |
| 154 | + }; |
| 155 | + |
| 156 | + let message = { |
| 157 | + let message = format!( |
| 158 | + "%c{level}%c {file}:{line} %c\n{text}", |
| 159 | + level = record.level(), |
| 160 | + file = record.file().unwrap_or_else(|| record.target()), |
| 161 | + line = record |
| 162 | + .line() |
| 163 | + .map_or_else(|| "[Unknown]".to_string(), |line| line.to_string()), |
| 164 | + text = record.args(), |
| 165 | + ); |
| 166 | + JsValue::from(&message) |
| 167 | + }; |
| 168 | + |
| 169 | + let level_style = { |
| 170 | + let style_str = match record.level() { |
| 171 | + Level::Trace => STYLE.trace, |
| 172 | + Level::Debug => STYLE.debug, |
| 173 | + Level::Info => STYLE.info, |
| 174 | + Level::Warn => STYLE.warn, |
| 175 | + Level::Error => STYLE.error, |
| 176 | + }; |
| 177 | + |
| 178 | + JsValue::from(style_str) |
| 179 | + }; |
| 180 | + |
| 181 | + let file_line_style = JsValue::from_str(STYLE.file_line); |
| 182 | + let text_style = JsValue::from_str(STYLE.text); |
| 183 | + console_log(&message, &level_style, &file_line_style, &text_style); |
| 184 | + } |
132 | 185 | }
|
133 | 186 |
|
134 | 187 | /// Initializes the global logger setting `max_log_level` to the given value.
|
|
0 commit comments