Skip to content

Commit

Permalink
feat: add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
meskill committed Jul 3, 2022
1 parent fea4613 commit fe92118
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 23,7 @@ rustdoc-args = ["--cfg", "docsrs"]
async-graphql = { version="4.0.1", optional=true }
custom_error = "1.9.2"
libloading = "0.7.3"
log = "0.4.17"
oaidl = "0.2.1"
serde = { version="1.0.137", optional=true, features=["derive"] }
widestring = "0.4.3" # version compatible with `oaidl`
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 51,13 @@
//!
//! That all means you can safely use rust wrapper both in single-threaded and multi-threaded environments, but actual sdk calls will be executed in sequence anyway.
//!
//! # Usage
//!
//! ## logging
//!
//! 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`
//!
//! # Features
//!
//! ## serde
Expand Down
8 changes: 8 additions & 0 deletions src/sdk/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 95,12 @@ impl Device {
}

pub(crate) fn new(library: Arc<Mutex<Library>>, name: String, led_count: u32) -> Self {
log::debug!(
"fn:new call with args: name={}, led_count={}",
name,
led_count
);

Self {
library,
name,
Expand All @@ -111,6 117,8 @@ impl Device {
where
F: for<'a> Filter<&'a DeviceLed>,
{
log::debug!("fn:leds_with_filter call");

let leds = (0..self.led_count)
.into_iter()
.map(|led_index| DeviceLed::new(Arc::clone(&self.library), &self.name, led_index))
Expand Down
54 changes: 52 additions & 2 deletions src/sdk/led.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 2,6 @@ use std::collections::HashSet;
use std::fmt::Debug;
use std::ptr::null_mut;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

use libloading::{Library, Symbol};

Expand Down Expand Up @@ -184,6 182,12 @@ impl DeviceLed {
device_name: &str,
led_index: u32,
) -> Result<Self> {
log::debug!(
"fn:new call with args: device_name={}, led_index={}",
device_name,
led_index
);

let get_led_info: Symbol<
unsafe extern "C" fn(
device_name: DeviceName,
Expand Down Expand Up @@ -248,6 252,14 @@ impl DeviceLed {

drop(library_instance);

log::debug!(
"fn:new got led with: name={}, supported_style={:?}, max_bright={}, max_speed={}",
name,
supported_styles,
max_bright,
max_speed
);

Ok(Self {
library,
device_name,
Expand All @@ -261,6 273,8 @@ impl DeviceLed {

/// Return state of the led
pub fn get_state(&self) -> Result<DeviceLedState> {
log::debug!("fn:get_state call for led with name={}", &self.name);

let get_led_style: Symbol<
unsafe extern "C" fn(
device_name: DeviceName,
Expand Down Expand Up @@ -338,6 352,13 @@ impl DeviceLed {

let color = Color { red, green, blue };

log::debug!(
"fn:get_state got state with: color={:?}, bright={}, speed={}",
color,
bright,
speed
);

Ok(DeviceLedState {
style: Bstr::from(style).to_string(),
color,
Expand All @@ -348,6 369,11 @@ impl DeviceLed {

/// Set led style
pub fn set_style(&self, style: &str) -> Result<()> {
log::debug!(
"fn:set_style call for led with name={} with args: style={}",
&self.name,
style
);
let set_led_style: Symbol<
unsafe extern "C" fn(
device_name: DeviceName,
Expand Down Expand Up @@ -391,6 417,12 @@ impl DeviceLed {
/// Some of the styles do not support setting color for the led.
/// In this case this method will return `Err(CommonError::MysticLightSDKError(Timeout))` as this error is returned by the underlying dll
pub fn set_color(&self, color: &Color) -> Result<()> {
log::debug!(
"fn:set_color call with name={} with args: color={:?}",
&self.name,
color
);

let set_led_color: Symbol<
unsafe extern "C" fn(
device_name: DeviceName,
Expand Down Expand Up @@ -427,6 459,12 @@ impl DeviceLed {
/// Some of the styles do not support setting color for the led.
/// In this case this method will return `Err(CommonError::MysticLightSDKError(Timeout))` as this error is returned by the underlying dll
pub fn set_bright(&self, bright: BrightLevel) -> Result<()> {
log::debug!(
"fn:set_bright call with name={} with args: bright={}",
&self.name,
bright
);

let set_led_bright: Symbol<
unsafe extern "C" fn(
device_name: DeviceName,
Expand Down Expand Up @@ -457,6 495,12 @@ impl DeviceLed {
/// Some of the styles do not support setting color for the led.
/// In this case this method will return `Err(CommonError::MysticLightSDKError(Timeout))` as this error is returned by the underlying dll
pub fn set_speed(&self, speed: SpeedLevel) -> Result<()> {
log::debug!(
"fn:set_speed call with name={} with args: speed={}",
&self.name,
speed
);

let set_led_speed: Symbol<
unsafe extern "C" fn(
device_name: DeviceName,
Expand Down Expand Up @@ -487,6 531,12 @@ impl DeviceLed {
/// Some of the styles do not support setting color for the led.
/// In this case this method will return `Err(CommonError::MysticLightSDKError(Timeout))` as this error is returned by the underlying dll
pub fn set_state(&mut self, state: &DeviceLedState) -> Result<()> {
log::debug!(
"fn:set_state call with name={} with args: state={:?}",
&self.name,
state
);

self.set_style(&state.style)?;
match self.set_color(&state.color) {
Ok(_) => (),
Expand Down
10 changes: 10 additions & 0 deletions src/sdk/mystic_light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 95,7 @@ impl MysticLightSDK {
///
/// **You must pass valid dll based on the os architecture**
pub fn new(lib_path: &str) -> Result<Self> {
log::debug!("fn:new call");
let library;

unsafe {
Expand All @@ -112,6 113,7 @@ impl MysticLightSDK {
}

pub fn get_devices(&self) -> Result<Vec<Device>> {
log::debug!("fn:get_devices call");
self.get_devices_with_filter(DeviceFilter::default())
}

Expand All @@ -120,6 122,8 @@ impl MysticLightSDK {
where
F: for<'a> Filter<&'a str>,
{
log::debug!("fn:get_devices_with_filter call");

let mut dev_type: DeviceTypes = null_mut();
let mut led_count: LedCounts = null_mut();

Expand All @@ -142,6 146,12 @@ impl MysticLightSDK {
let devices: Vec<String> = Vec::from_safearray(dev_type);
let leds: Vec<String> = Vec::from_safearray(led_count);

log::debug!(
"fn:get_devices_with_filter got raw: devices={:?}, led={:?}",
devices,
leds
);

Ok(devices
.into_iter()
.zip(leds)
Expand Down

0 comments on commit fe92118

Please sign in to comment.