Skip to content

Commit 65aca96

Browse files
committed
Adds colors for log output behind feature flag.
1 parent aa0e1f7 commit 65aca96

File tree

3 files changed

+104
-10
lines changed

3 files changed

+104
-10
lines changed

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,11 @@ version = "0.4"
1717
[dependencies.web-sys]
1818
version = "0.3"
1919
features = ["console"]
20+
21+
[dependencies.wasm-bindgen]
22+
version = "0.2"
23+
optional = true
24+
25+
[features]
26+
default = []
27+
color = ["wasm-bindgen"]

src/lib.rs

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@
8787
use log::{Level, Log, Metadata, Record, SetLoggerError};
8888
use web_sys::console;
8989

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+
9099
static LOGGER: WebConsoleLogger = WebConsoleLogger {};
91100

92101
struct WebConsoleLogger {}
@@ -119,16 +128,60 @@ impl Log for WebConsoleLogger {
119128
/// .apply()?;
120129
/// ```
121130
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+
}
132185
}
133186

134187
/// Initializes the global logger setting `max_log_level` to the given value.

src/style.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/// Log message styling.
2+
///
3+
/// Adapted from <https://gitlab.com/limira-rs/wasm-logger/-/blob/0c16227/src/lib.rs#L72-85>
4+
pub struct Style<'s> {
5+
pub trace: &'s str,
6+
pub debug: &'s str,
7+
pub info: &'s str,
8+
pub warn: &'s str,
9+
pub error: &'s str,
10+
pub file_line: &'s str,
11+
pub text: &'s str,
12+
}
13+
14+
impl Style<'static> {
15+
/// Returns default style values.
16+
pub const fn default() -> Self {
17+
macro_rules! bg_color {
18+
($color:expr) => {
19+
concat!("color: white; padding: 0 3px; background: ", $color, ";");
20+
};
21+
};
22+
23+
Style {
24+
trace: bg_color!("gray"),
25+
debug: bg_color!("blue"),
26+
info: bg_color!("green"),
27+
warn: bg_color!("orange"),
28+
error: bg_color!("darkred"),
29+
file_line: "font-weight: bold; color: inherit",
30+
text: "background: inherit; color: inherit",
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)