From 7f86db0b00132caf597f04cd0db31f7dac8afc96 Mon Sep 17 00:00:00 2001 From: Jeron Aldaron Lau Date: Sun, 3 Nov 2019 22:15:38 -0800 Subject: [PATCH] Add logging API (v0.7.0) (#19) * Add logging functions, `info()`, `warn()`, and `note()`. * Update README, changelog and docs for 0.7.0. --- Cargo.toml | 7 +++-- README.md | 2 +- changelog.md | 8 +++++ examples/log/Cargo.toml | 10 +++++++ examples/log/src/main.rs | 7 +++++ src/internal/mod.rs | 63 ++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 47 +++++++++++++++++++++++++++++- 7 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 examples/log/Cargo.toml create mode 100644 examples/log/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index eec3d9b..277f281 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ [package] name = "cala" -version = "0.6.0" +version = "0.7.0" authors = ["Jeron Aldaron Lau "] edition = "2018" @@ -36,9 +36,12 @@ stick = {version = "0.7", optional = true} # joystick / controller # barg = {version = "0.2", optional = true} # video window = {version = "0.3", optional = true} fonterator = {version = "0.6", optional = true, default-features = false, features = ["normal-font"]} -rvg = {version = "0.0.2", optional = true, features = ["footile"]} +rvg = {version = "0.0.3", optional = true, features = ["footile"]} chrono = {version = "0.4", optional = true} # clock +[target.'cfg(target_arch = "wasm32")'.dependencies] +stdweb = "0.4" + [build-dependencies] res = {version = "0.5", optional = true} # video diff --git a/README.md b/README.md index 0a45b8e..a649278 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,10 @@ Cala is a platform-agnostic system interface for hardware IO. This means that e - **Linux** - **MacOS** - missing [*audio*](https://github.com/libcala/cala/issues/5), [*controller*](https://github.com/libcala/cala/issues/7), [*graphics*](https://github.com/libcala/cala/issues/9) - **Windows** - missing [*audio*](https://github.com/libcala/cala/issues/4), [*controller*](https://github.com/libcala/cala/issues/6), [*graphics*](https://github.com/libcala/cala/issues/8) +- **Web (WASM)** - missing audio, controller, graphics, files - Redox - Android - iOS -- Web (WASM) - Nintendo Switch - XBox - PlayStation diff --git a/changelog.md b/changelog.md index 20ba9c5..99f229d 100644 --- a/changelog.md +++ b/changelog.md @@ -8,6 +8,14 @@ and this project adheres to [Semantic Versioning](https://code.plopgrizzly.com/s ### TODO - Possibly redesign the controller API to be event based using a message queue. +## [0.7.0] - 2019-11-04 +### Added +- Logging macros `info!()`, `warn!()` and `note!()` +- Web Assembly support for `user` feature, and logging. + +### Changed +- Updated `whoami` + ## [0.6.0] - 2019-10-25 ### Added - `aspect()` for getting aspect ratio diff --git a/examples/log/Cargo.toml b/examples/log/Cargo.toml new file mode 100644 index 0000000..1a6fe23 --- /dev/null +++ b/examples/log/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "logger" +version = "0.1.0" +authors = ["Jeron Aldaron Lau "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +cala = { path = "../../" } diff --git a/examples/log/src/main.rs b/examples/log/src/main.rs new file mode 100644 index 0000000..41c0da2 --- /dev/null +++ b/examples/log/src/main.rs @@ -0,0 +1,7 @@ +use cala; + +fn main() { + cala::info!("Info message"); + cala::warn!("Warn message"); + cala::note!("Note message"); +} diff --git a/src/internal/mod.rs b/src/internal/mod.rs index dcc1753..6151e60 100644 --- a/src/internal/mod.rs +++ b/src/internal/mod.rs @@ -4,6 +4,9 @@ static NANOS: std::sync::atomic::AtomicU64 = // // // // // // +#[cfg(target_arch = "wasm32")] +use stdweb::js; + /// Initialize Cala. fn init(_name: &str, _run: fn(nanos: u64)) { START.call_once(|| { @@ -42,6 +45,66 @@ fn r#loop(_run: fn(nanos: u64)) -> bool { } } +/// Log an informative (stdout) message in both release and debug mode. +/// +/// Do not overuse this function. Release builds should have few logs. +#[inline(always)] +pub fn info(string: &str) { + #[cfg(target_arch = "wasm32")] + { + js! { + console.info(@{string}) + } + } + #[cfg(not(target_arch = "wasm32"))] + { + println!("{}", string); + } +} + +/// Log a warning (stderr) message in both release and debug mode. +/// +/// Do not overuse this function. Release builds should have few logs. +#[inline(always)] +pub fn warn(string: &str) { + #[cfg(target_arch = "wasm32")] + { + js! { + console.warn(@{string}) + } + } + #[cfg(not(target_arch = "wasm32"))] + { + eprintln!("{}", string); + } +} + +/// Log an informative (stdout) message in only debug mode. +/// +/// This is designed to be used for debugging. +#[cfg(not(debug_assertions))] +#[inline(always)] +pub fn note(string: &str) { +} + +/// Log an informative (stdout) message in only debug mode. +/// +/// This is designed to be used for debugging. +#[cfg(debug_assertions)] +#[inline(always)] +pub fn note(string: &str) { + #[cfg(target_arch = "wasm32")] + { + js! { + console.debug(@{string}) + } + } + #[cfg(not(target_arch = "wasm32"))] + { + println!("{}", string); + } +} + /// Get nanoseconds passed since previous frame. pub fn delta() -> u64 { NANOS.load(std::sync::atomic::Ordering::Relaxed) diff --git a/src/lib.rs b/src/lib.rs index d57c109..84728eb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,10 +23,10 @@ //!
  • Linux
  • //!
  • MacOS - missing audio, controller, graphics
  • //!
  • Windows - missing audio, controller, graphics
  • +//!
  • Web (WASM) - missing audio, controller, graphics, files
  • //!
  • Redox
  • //!
  • Android
  • //!
  • iOS
  • -//!
  • Web (WASM)
  • //!
  • Nintendo Switch
  • //!
  • XBox
  • //!
  • PlayStation
  • @@ -238,6 +238,12 @@ pub use clock::*; #[doc(hidden)] pub use internal::start; #[doc(hidden)] +pub use internal::info as __cala_internal_info__; +#[doc(hidden)] +pub use internal::warn as __cala_internal_warn__; +#[doc(hidden)] +pub use internal::note as __cala_internal_note__; +#[doc(hidden)] pub use run::Loop::*; pub use internal::delta; @@ -304,3 +310,42 @@ macro_rules! init { } }; } + +/// Log an informative (stdout) message only in debug mode. +/// +/// This is designed to be used for debugging. +#[macro_export] +macro_rules! note { + () => { + $crate::__cala_internal_note__("") + }; + ($($arg:tt)*) => { + $crate::__cala_internal_note__(&format!("{}", format_args!($($arg)*))) + }; +} + +/// Log an informative (stdout) message in both release and debug mode. +/// +/// Do not overuse this function. Release builds should have few logs. +#[macro_export] +macro_rules! info { + () => { + $crate::__cala_internal_info__("") + }; + ($($arg:tt)*) => { + $crate::__cala_internal_info__(&format!("{}", format_args!($($arg)*))) + }; +} + +/// Log a warning (stderr) message in both release and debug mode. +/// +/// Do not overuse this function. Release builds should have few logs. +#[macro_export] +macro_rules! warn { + () => { + $crate::__cala_internal_warn__("") + }; + ($($arg:tt)*) => { + $crate::__cala_internal_warn__(&format!("{}", format_args!($($arg)*))) + }; +}