Skip to content

Commit

Permalink
Add logging API (v0.7.0) (#19)
Browse files Browse the repository at this point in the history
* Add logging functions, `info()`, `warn()`, and `note()`.

* Update README, changelog and docs for 0.7.0.
  • Loading branch information
AldaronLau authored Nov 4, 2019
1 parent f8a7fd0 commit 7f86db0
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 4 deletions.
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

[package]
name = "cala"
version = "0.6.0"
version = "0.7.0"
authors = ["Jeron Aldaron Lau <[email protected]>"]
edition = "2018"

Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions examples/log/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "logger"
version = "0.1.0"
authors = ["Jeron Aldaron Lau <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cala = { path = "../../" }
7 changes: 7 additions & 0 deletions examples/log/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use cala;

fn main() {
cala::info!("Info message");
cala::warn!("Warn message");
cala::note!("Note message");
}
63 changes: 63 additions & 0 deletions src/internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(|| {
Expand Down Expand Up @@ -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)
Expand Down
47 changes: 46 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
//! <li><b>Linux</b></li>
//! <li><b>MacOS</b> - missing <a href="https://github.com/libcala/cala/issues/5"><i>audio</i></a>, <a href="https://github.com/libcala/cala/issues/7"><i>controller</i></a>, <a href="https://github.com/libcala/cala/issues/9"><i>graphics</i></a></li>
//! <li><b>Windows</b> - missing <a href="https://github.com/libcala/cala/issues/4"><i>audio</i></a>, <a href="https://github.com/libcala/cala/issues/6"><i>controller</i></a>, <a href="https://github.com/libcala/cala/issues/8"><i>graphics</i></a></li>
//! <li><b>Web (WASM)</b> - missing audio, controller, graphics, files</li>
//! <li>Redox</li>
//! <li>Android</li>
//! <li>iOS</li>
//! <li>Web (WASM)</li>
//! <li>Nintendo Switch</li>
//! <li>XBox</li>
//! <li>PlayStation</li>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)*)))
};
}

0 comments on commit 7f86db0

Please sign in to comment.