From 43b44b469eb99ad0d748094c40ff8ca1d9e92ee1 Mon Sep 17 00:00:00 2001 From: Stig Johan Berggren Date: Sat, 2 May 2020 19:38:05 +0200 Subject: [PATCH 1/4] Support web-sys via an optional feature --- Cargo.toml | 16 ++++++++++++- src/lib.rs | 63 ++++++++++++++++++-------------------------------- src/std_web.rs | 21 +++++++++++++++++ src/web_sys.rs | 23 ++++++++++++++++++ 4 files changed, 82 insertions(+), 41 deletions(-) create mode 100644 src/std_web.rs create mode 100644 src/web_sys.rs diff --git a/Cargo.toml b/Cargo.toml index e63d07e..a98cf40 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [package] name = "web_logger" +edition = "2018" version = "0.2.0" authors = ["Denis Kolodin "] repository = "https://github.com/DenisKolodin/web-logger" @@ -13,7 +14,20 @@ description = "A logger for logging in web-browsers" [dependencies] log = "0.4" -stdweb = "0.4" +cfg-if = "0.1.10" + +[dependencies.stdweb] +version = "0.4" +optional = true + +[dependencies.web-sys] +version = "0.3" +optional = true +features = ["console"] [target.'cfg(all(target_arch = "wasm32", not(cargo_web)))'.dependencies] wasm-bindgen = { version = "0.2" } + +[features] +std_web = ["stdweb"] +web_sys = ["web-sys"] diff --git a/src/lib.rs b/src/lib.rs index ac73ea1..fe2c4f0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,53 +1,33 @@ //! A logger that prints all messages in browser's console. extern crate log; -#[macro_use] -extern crate stdweb; -use log::{ - Log, - Level, - Metadata, - Record, - SetLoggerError, -}; - -mod console { - pub(super) fn trace(message: &str) { - js! { @(no_return) console.log(@{message}); } - } - - pub(super) fn debug(message: &str) { - js! { @(no_return) console.debug(@{message}); } - } - - pub(super) fn info(message: &str) { - js! { @(no_return) console.info(@{message}); } - } - - pub(super) fn warn(message: &str) { - js! { @(no_return) console.warn(@{message}); } - } - - pub(super) fn error(message: &str) { - js! { @(no_return) console.error(@{message}); } +use log::{Level, Log, Metadata, Record, SetLoggerError}; + +cfg_if::cfg_if! { + if #[cfg(feature = "std_web")] { + #[macro_use] + extern crate stdweb; + mod std_web; + use std_web::console; + } else if #[cfg(feature = "web_sys")] { + mod web_sys; + use crate::web_sys::console; } } - pub struct Config { - pub level: Level + pub level: Level, } impl Default for Config { fn default() -> Self { Config { - level: Level::Trace + level: Level::Trace, } } } - static LOGGER: WebLogger = WebLogger; struct WebLogger; @@ -61,10 +41,12 @@ impl Log for WebLogger { fn log(&self, record: &Record) { let metadata = record.metadata(); if self.enabled(metadata) { - let msg = format!("{}:{} -- {}", + let msg = format!( + "{}:{} -- {}", record.level(), record.target(), - record.args()); + record.args() + ); match metadata.level() { Level::Trace => console::trace(&msg), Level::Debug => console::debug(&msg), @@ -75,8 +57,7 @@ impl Log for WebLogger { } } - fn flush(&self) { - } + fn flush(&self) {} } pub fn try_init(config: Config) -> Result<(), SetLoggerError> { @@ -87,9 +68,11 @@ pub fn try_init(config: Config) -> Result<(), SetLoggerError> { } pub fn init() { - try_init(Config::default()).expect("web_logger::init should not be called after logger initialized"); + try_init(Config::default()) + .expect("web_logger::init should not be called after logger initialized"); } pub fn custom_init(config: Config) { - try_init(config).expect("web_logger::custom_init should not be called after logger initialized"); -} \ No newline at end of file + try_init(config) + .expect("web_logger::custom_init should not be called after logger initialized"); +} diff --git a/src/std_web.rs b/src/std_web.rs new file mode 100644 index 0000000..a3fa6c1 --- /dev/null +++ b/src/std_web.rs @@ -0,0 +1,21 @@ +pub mod console { + pub fn trace(message: &str) { + js! { @(no_return) console.log(@{message}); } + } + + pub fn debug(message: &str) { + js! { @(no_return) console.debug(@{message}); } + } + + pub fn info(message: &str) { + js! { @(no_return) console.info(@{message}); } + } + + pub fn warn(message: &str) { + js! { @(no_return) console.warn(@{message}); } + } + + pub fn error(message: &str) { + js! { @(no_return) console.error(@{message}); } + } +} diff --git a/src/web_sys.rs b/src/web_sys.rs new file mode 100644 index 0000000..412bc30 --- /dev/null +++ b/src/web_sys.rs @@ -0,0 +1,23 @@ +pub mod console { + use web_sys::console; + + pub fn trace(message: &str) { + console::log_1(&message.into()); + } + + pub fn debug(message: &str) { + console::debug_1(&message.into()); + } + + pub fn info(message: &str) { + console::info_1(&message.into()); + } + + pub fn warn(message: &str) { + console::warn_1(&message.into()); + } + + pub fn error(message: &str) { + console::error_1(&message.into()); + } +} From 471004c650b2518e9b80692b202f85ce5748c268 Mon Sep 17 00:00:00 2001 From: Stig Johan Berggren Date: Sat, 2 May 2020 19:47:20 +0200 Subject: [PATCH 2/4] std_web as default feature --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index a98cf40..c486d52 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,5 +29,6 @@ features = ["console"] wasm-bindgen = { version = "0.2" } [features] +default = ["std_web"] std_web = ["stdweb"] web_sys = ["web-sys"] From 19ea4414e5c11f4d04c9e8f7929c315a2f0653ca Mon Sep 17 00:00:00 2001 From: Stig Johan Berggren Date: Sat, 2 May 2020 20:05:02 +0200 Subject: [PATCH 3/4] 2018 edition and more documentation --- src/lib.rs | 10 ++++++---- src/std_web.rs | 4 +++- src/web_sys.rs | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fe2c4f0..eaf7399 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,13 +1,15 @@ //! A logger that prints all messages in browser's console. - -extern crate log; +//! +//! By default, `web_logger` will use the `std_web` feature, which depends on `stdweb`. If you want to use `web-sys`, +//! add this to your Cargo.toml under `[dependencies]`: +//! ```toml +//! web_logger = { version="0.2", default-features=false, features="web_sys" } +//! ``` use log::{Level, Log, Metadata, Record, SetLoggerError}; cfg_if::cfg_if! { if #[cfg(feature = "std_web")] { - #[macro_use] - extern crate stdweb; mod std_web; use std_web::console; } else if #[cfg(feature = "web_sys")] { diff --git a/src/std_web.rs b/src/std_web.rs index a3fa6c1..18d56be 100644 --- a/src/std_web.rs +++ b/src/std_web.rs @@ -1,4 +1,6 @@ -pub mod console { +pub(super) mod console { + use stdweb::js; + pub fn trace(message: &str) { js! { @(no_return) console.log(@{message}); } } diff --git a/src/web_sys.rs b/src/web_sys.rs index 412bc30..a975cee 100644 --- a/src/web_sys.rs +++ b/src/web_sys.rs @@ -1,4 +1,4 @@ -pub mod console { +pub(super) mod console { use web_sys::console; pub fn trace(message: &str) { From 23742dd6871fab688e855dca3dd860eeee3dc2fa Mon Sep 17 00:00:00 2001 From: Stig Johan Berggren Date: Sat, 2 May 2020 20:05:59 +0200 Subject: [PATCH 4/4] 2018 edition in Readme --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 857f506..ac3c389 100644 --- a/README.md +++ b/README.md @@ -22,9 +22,7 @@ web_logger = "0.2" After it's initialized, you can use the `log` macros to do actual logging. ```rust -#[macro_use] -extern crate log; -extern crate web_logger; +use log::info; fn main() { web_logger::init();