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

Add support for web_sys in addition to stdweb #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
17 changes: 16 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "web_logger"
edition = "2018"
version = "0.2.0"
authors = ["Denis Kolodin <[email protected]>"]
repository = "https://github.com/DenisKolodin/web-logger"
Expand All @@ -13,7 +14,21 @@ 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]
default = ["std_web"]
std_web = ["stdweb"]
web_sys = ["web-sys"]
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
71 changes: 28 additions & 43 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,35 @@
//! 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}); }
//!
//! 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")] {
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;
Expand All @@ -61,10 +43,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),
Expand All @@ -75,8 +59,7 @@ impl Log for WebLogger {
}
}

fn flush(&self) {
}
fn flush(&self) {}
}

pub fn try_init(config: Config) -> Result<(), SetLoggerError> {
Expand All @@ -87,9 +70,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");
}
try_init(config)
.expect("web_logger::custom_init should not be called after logger initialized");
}
23 changes: 23 additions & 0 deletions src/std_web.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pub(super) mod console {
use stdweb::js;

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}); }
}
}
23 changes: 23 additions & 0 deletions src/web_sys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pub(super) 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());
}
}