Skip to content

Commit

Permalink
feat: migrate to tracing library for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
meskill committed Aug 20, 2022
1 parent c0bba65 commit fd3051c
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 95 deletions.
2 changes: 2 additions & 0 deletions .releaserc.yml
Expand Up @@ -10,6 +10,8 @@ plugins:
- { type: ci, release: false }
- { subject: "*\\[skip release\\]*", release: false }
- { breaking: true, release: minor } # change to major after reaching 1.0.0 version
# remove rules below after reaching 1.0.0
- { type: feat, release: patch }
- - '@semantic-release/release-notes-generator'
- preset: 'conventionalcommits'
presetConfig:
Expand Down
104 changes: 101 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Expand Up @@ -24,14 +24,15 @@ async-graphql = { version="4.0.1", optional=true }
custom_error = "1.9.2"
either = "1.7.0"
libloading = "0.7.3"
log = "0.4.17"
oaidl = "0.2.1"
serde = { version="1.0.137", optional=true, features=["derive"] }
tracing = "0.1.36"
widestring = "0.4.3" # version compatible with `oaidl`
winapi = { version = "0.3.9" }

[dev-dependencies]
serde_json = { version="1.0.81" }
tracing-subscriber = "0.3.15"

[build-dependencies]
copy_dir = "0.1.2"
Expand Down
23 changes: 16 additions & 7 deletions examples/disable_light_for_5_sec.rs
Expand Up @@ -2,26 +2,35 @@ use mystic_light_sdk::{Color, CommonError, DeviceLedState, MysticLightSDK};
use std::thread;
use std::time::Duration;

use tracing::{info, warn, Level};
use tracing_subscriber::{fmt, fmt::format::FmtSpan};

const LIB_PATH: &str = if cfg!(target_arch = "x86_64") {
"../sdk/MysticLight_SDK_x64.dll"
} else {
"../sdk/MysticLight_SDK.dll"
};

fn main() -> Result<(), CommonError> {
fmt()
.pretty()
.with_max_level(Level::DEBUG)
.with_span_events(FmtSpan::ACTIVE)
.init();

let sdk = MysticLightSDK::new(LIB_PATH)?;

let devices: Vec<_> = sdk.devices_iter().collect();

println!("{:#?}", devices);
info!(?devices);

println!("Second Device name is {}", devices[2].name());
info!(second_device_name = devices[2].name());

let keyboard_leds: Vec<_> = devices[2].leds_iter().collect();

println!("{:#?}", keyboard_leds);
info!(?keyboard_leds);

println!(
info!(
"First led has name: {} with max_bright: {} and max_speed: {}",
keyboard_leds[0].name(),
keyboard_leds[0].max_bright(),
Expand All @@ -30,9 +39,9 @@ fn main() -> Result<(), CommonError> {

let state = keyboard_leds[0].get_state()?;

println!("Current device state: {:#?}", state);
info!("Current device state: {:#?}", state);

println!("Disable lightning!");
warn!("Disable lightning!");

let new_state = DeviceLedState {
color: Color {
Expand All @@ -48,7 +57,7 @@ fn main() -> Result<(), CommonError> {

thread::sleep(Duration::from_secs(5));

println!("Enable lightning");
warn!("Enable lightning");

keyboard_leds[0].set_state(&state)?;

Expand Down
27 changes: 9 additions & 18 deletions examples/get_all_states.rs
@@ -1,5 +1,6 @@
use mystic_light_sdk::{CommonError, MysticLightSDK};
use std::time::Instant;
use tracing::{info, Level};
use tracing_subscriber::{fmt, fmt::format::FmtSpan};

const LIB_PATH: &str = if cfg!(target_arch = "x86_64") {
"../sdk/MysticLight_SDK_x64.dll"
Expand All @@ -8,22 +9,17 @@ const LIB_PATH: &str = if cfg!(target_arch = "x86_64") {
};

fn main() -> Result<(), CommonError> {
let timer = Instant::now();
let sdk = MysticLightSDK::new(LIB_PATH)?;
fmt()
.pretty()
.with_max_level(Level::DEBUG)
.with_span_events(FmtSpan::FULL)
.init();

println!("Init in {:?} secs", timer.elapsed().as_secs_f32());
let sdk = MysticLightSDK::new(LIB_PATH)?;

let devices = sdk.devices_iter();

println!(
"Getting devices for {:#?} secs",
timer.elapsed().as_secs_f32()
);

let leds = devices.map(|device| device.leds_iter());

println!("Getting leds for {:#?} secs", timer.elapsed().as_secs_f32());

let states: Vec<Vec<_>> = leds
.map(|led| {
led.into_iter()
Expand All @@ -32,12 +28,7 @@ fn main() -> Result<(), CommonError> {
})
.collect();

println!(
"Getting states for {:#?} secs",
timer.elapsed().as_secs_f32()
);

println!("States: {:#?}", states);
info!(?states);

Ok(())
}
5 changes: 2 additions & 3 deletions src/lib.rs
Expand Up @@ -53,10 +53,9 @@
//!
//! # Usage
//!
//! ## logging
//! ## tracing
//!
//! Logging is implemented with library [`log`](https://docs.rs/log/0.4.17/log/index.html) - to enable actual logging just pick one of the logger
//! implementation from the [list](https://docs.rs/log/0.4.17/log/index.html#available-logging-implementations) and activate log for the module `mystic_light` e.g. for `env_logger` pass `RUST_LOG=mystic_light_sdk`
//! Tracing is implemented with library [`tracing`](https://docs.rs/tracing/0.1.36/tracing/index.html) - to see tracing logs follow the [instructions of tracing crate](https://docs.rs/tracing/0.1.36/tracing/index.html#in-executables).
//!
//! # Features
//!
Expand Down
10 changes: 2 additions & 8 deletions src/sdk/device.rs
Expand Up @@ -104,13 +104,8 @@ impl Device {
&self.name
}

#[tracing::instrument(level = "debug", skip(library))]
pub(crate) fn new(library: Arc<Mutex<Library>>, name: String, led_count: u32) -> Result<Self> {
log::debug!(
"fn:new call with args: name={}, led_count={}",
name,
led_count
);

let leds = Self::resolve_leds(&library, &name, led_count)?;

Ok(Self {
Expand All @@ -127,9 +122,8 @@ impl Device {
}

/// reload cached leds info
#[tracing::instrument(level = "debug", skip_all, fields(self.name = self.name))]
pub fn reload(&mut self) -> Result<()> {
log::debug!("fn:reload call");

self.leds = Self::resolve_leds(&self.library, &self.name, self.led_count)?;

Ok(())
Expand Down

0 comments on commit fd3051c

Please sign in to comment.