Skip to content

Commit

Permalink
feat/log: use JS console log levels
Browse files Browse the repository at this point in the history
Fixes #6
  • Loading branch information
wehlutyk committed Jul 12, 2018
1 parent 43cb6e2 commit e48f2a0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
17 changes: 16 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ use lopdf::Document;
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[wasm_bindgen]
extern {
#[wasm_bindgen(js_namespace = console, js_name = error)]
fn log_error(s: &str);
#[wasm_bindgen(js_namespace = console, js_name = warn)]
fn log_warn(s: &str);
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
#[wasm_bindgen(js_namespace = console, js_name = info)]
fn log_info(s: &str);
#[wasm_bindgen(js_namespace = console, js_name = debug)]
fn log_debug(s: &str);
}

#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
Expand All @@ -30,7 +38,14 @@ impl log::Log for WasmConsoleLogger {

fn log(&self, record: &log::Record) {
if self.enabled(record.metadata()) {
log(&format!("{} - {}", record.level(), record.args()));
let log_type = match record.level() {
log::Level::Error => log_error,
log::Level::Warn => log_warn,
log::Level::Info => log_info,
log::Level::Debug => log_debug,
log::Level::Trace => log_debug,
};
log_type(&format!("{} - {}", record.level(), record.args()));
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ fn main() -> std::io::Result<()> {

// Process arguments
let arguments = env::args().skip(1);
info!("[args] {} file(s)", arguments.len());
debug!("[args] {} file(s)", arguments.len());
for argument in arguments {
info!("[processing] file name = {}", &argument);
debug!("[processing] file name = {}", &argument);
let file = File::open(&argument)?;
let mut buf_reader = BufReader::new(file);
let mut contents: Vec<u8> = vec![];
buf_reader.read_to_end(&mut contents)?;
info!("[processing] reading \"{}\" ended", &argument);
debug!("[processing] reading \"{}\" ended", &argument);

infuse::process_file_data(contents);
}
Expand Down

0 comments on commit e48f2a0

Please sign in to comment.