1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use anyhow::{format_err, Result};
use log::{set_logger, set_max_level, Level, LevelFilter, Log, Metadata, Record};
use stdweb::js;
static LOGGER: LogService = LogService;
pub fn init_logger() -> Result<()> {
set_logger(&LOGGER)
.map(|()| set_max_level(LevelFilter::Trace))
.map_err(|_| format_err!("Logger init failed"))
}
struct LogService;
impl Log for LogService {
fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= Level::Trace
}
fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
let mut log_entry = format!("%c{}: %c", record.level());
if let (Some(file), Some(line)) = (record.file(), record.line()) {
log_entry += &format!("{}:{}: ", file, line);
}
log_entry += &format!("{}", record.args());
const BOLD: &str = "font-weight: bold";
const NORMAL: &str = "font-weight: normal";
match record.level() {
Level::Error => {
js! { console.error(@{log_entry}, @{BOLD}, @{NORMAL}) }
}
Level::Warn => {
js! { console.warn(@{log_entry}, @{BOLD}, @{NORMAL}) }
}
Level::Info => {
js! { console.log(@{log_entry}, @{BOLD}, @{NORMAL}) }
}
Level::Debug => {
js! { console.debug(@{log_entry}, @{BOLD}, @{NORMAL}) }
}
Level::Trace => {
js! { console.debug(@{log_entry}, @{BOLD}, @{NORMAL}) }
}
}
}
}
fn flush(&self) {}
}