Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove log formatting #1204

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 9 additions & 74 deletions mutiny-wasm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ pub fn set_panic_hook() {

#[wasm_bindgen(start)]
pub async fn main_js() -> Result<(), JsValue> {
init(
Config::new(Level::Trace).message_on_new_line(),
Style::none(),
);
init(Config::new(Level::Trace).message_on_new_line());
debug!("Main function begins and ends");
Ok(())
}
Expand Down Expand Up @@ -75,48 +72,9 @@ impl Config {
}
}

/// The log styles
pub struct Style {
pub lvl_trace: String,
pub lvl_debug: String,
pub lvl_info: String,
pub lvl_warn: String,
pub lvl_error: String,
pub tgt: String,
pub args: String,
}

impl Style {
pub fn new() -> Style {
let base = String::from("color: white; padding: 0 3px; background:");
Style {
lvl_trace: format!("{} gray;", base),
lvl_debug: format!("{} blue;", base),
lvl_info: format!("{} green;", base),
lvl_warn: format!("{} orange;", base),
lvl_error: format!("{} darkred;", base),
tgt: String::from("font-weight: bold; color: inherit"),
args: String::from("background: inherit; color: inherit"),
}
}

pub fn none() -> Style {
Style {
lvl_trace: String::new(),
lvl_debug: String::new(),
lvl_info: String::new(),
lvl_warn: String::new(),
lvl_error: String::new(),
tgt: String::new(),
args: String::new(),
}
}
}

/// The logger
pub struct WasmLogger {
pub config: Config,
pub style: Style,
}

impl Log for WasmLogger {
Expand All @@ -130,37 +88,14 @@ impl Log for WasmLogger {

fn log(&self, record: &Record<'_>) {
if self.enabled(record.metadata()) {
let style = &self.style;
let s = format!("{}", record.args());
let s = JsValue::from_str(&s);
let tgt_style = JsValue::from_str(&style.tgt);
let args_style = JsValue::from_str(&style.args);
let s = JsValue::from_str(&format!("{}", record.args()));

match record.level() {
Level::Trace => console::debug_4(
&s,
&JsValue::from(&style.lvl_trace),
&tgt_style,
&args_style,
),
Level::Debug => console::log_4(
&s,
&JsValue::from(&style.lvl_debug),
&tgt_style,
&args_style,
),
Level::Info => {
console::info_4(&s, &JsValue::from(&style.lvl_info), &tgt_style, &args_style)
}
Level::Warn => {
console::warn_4(&s, &JsValue::from(&style.lvl_warn), &tgt_style, &args_style)
}
Level::Error => console::error_4(
&s,
&JsValue::from(&style.lvl_error),
&tgt_style,
&args_style,
),
Level::Trace => console::debug_1(&s),
Level::Debug => console::log_1(&s),
Level::Info => console::info_1(&s),
Level::Warn => console::warn_1(&s),
Level::Error => console::error_1(&s),
}
}
}
Expand All @@ -178,9 +113,9 @@ impl Log for WasmLogger {
/// ```rust
/// wasm_logger::init(wasm_logger::Config::with_prefix(log::Level::Debug, "some::module"));
/// ```
pub fn init(config: Config, style: Style) {
pub fn init(config: Config) {
let max_level = config.level;
let wl = WasmLogger { config, style };
let wl = WasmLogger { config };

match log::set_boxed_logger(Box::new(wl)) {
Ok(_) => log::set_max_level(max_level.to_level_filter()),
Expand Down
Loading