Skip to content

Commit

Permalink
Improve Tracing Management (#200)
Browse files Browse the repository at this point in the history
* improve tracing and setups args
* display common usefull paths
* Propagate log options to plugins
* remove print
* reduce warnings
* improve tracing and prepare 0.5.0
* Update build-binaries.yml
* Update build-binaries.yml
* Update README.md
  • Loading branch information
XdoctorwhoZ authored Dec 3, 2024
1 parent 4776179 commit 2e6b24f
Show file tree
Hide file tree
Showing 23 changed files with 594 additions and 364 deletions.
43 changes: 38 additions & 5 deletions .github/workflows/build-binaries.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# create binary on windows

name: build_binaries
name: Build Binaries

on:
push:
Expand All @@ -12,7 +12,7 @@ jobs:

# ---------------------------------------------------------------------------
build-on-ubuntu:
runs-on: windows-latest
runs-on: ubuntu-latest
steps:
- name: Checkout project
uses: actions/checkout@v4
Expand All @@ -21,7 +21,27 @@ jobs:
run: cargo --version

- name: Try to Build
run: cargo build --features log,built-in-drivers --verbose --release
run: cargo build --verbose --release

# Release version is supposed to have a faster runtime than debug
# - name: Upload windows binaries release
# uses: actions/upload-artifact@v4
# with:
# name: windows_binaries
# path: target/release/panduza-rust-platform.exe

# ---------------------------------------------------------------------------
build-on-ubuntu-builtin:
runs-on: ubuntu-latest
steps:
- name: Checkout project
uses: actions/checkout@v4

- name: Display cargo version
run: cargo --version

- name: Try to Build
run: cargo build --features built-in-drivers --verbose --release

# Release version is supposed to have a faster runtime than debug
# - name: Upload windows binaries release
Expand All @@ -41,7 +61,20 @@ jobs:
run: cargo --version

- name: Try to Build
run: cargo build --features log,built-in-drivers --verbose --release
run: cargo build --verbose --release

# ---------------------------------------------------------------------------
build-on-windows-builtin:
runs-on: windows-latest
steps:
- name: Checkout project
uses: actions/checkout@v4

- name: Display cargo version
run: cargo --version

- name: Try to Build
run: cargo build --features built-in-drivers --verbose --release

# Release version is supposed to have a faster runtime than debug
- name: Upload windows binaries release
Expand All @@ -50,4 +83,4 @@ jobs:
name: windows_binaries
path: target/release/panduza-rust-platform.exe



4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
target
log.csv
platform-log.csv
*.swp
Cargo.lock
Cargo.lock
src/sys_info.rs
12 changes: 10 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
],
"rust-analyzer.showUnlinkedFileNotification": false,
"rust-analyzer.cargo.features": [
"built-in-drivers"
]
// "built-in-drivers"
],
"rainbow_csv.virtual_alignment_mode": "always",
"rainbow_csv.autodetect_separators": [
";",
"|",
],
"[csv]": {
"editor.inlayHints.maximumLength": 0
},
}
45 changes: 18 additions & 27 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
[package]
name = "panduza-rust-platform"
version = "0.4.2"
version = "0.5.0"
edition = "2021"


[dependencies]

# Main base code for Panduza platform and plugins
# panduza-platform-core = { git = "https://github.com/Panduza/panduza-platform-core", branch = "main", features = [
# "log",
# ] }
panduza-platform-core = { git = "https://github.com/Panduza/panduza-platform-core", tag = "0.1.4", features = [
# "serial",
"log",
] }

panduza-platform-core = { git = "https://github.com/Panduza/panduza-platform-core", tag = "0.1.5" }
# Main async framework for the platform
tokio = { version = "1.40.0", features = ["full", "tracing"] }


clap = { version = "4.5.21", features = ["derive"] }


libloading = "0.8"

#
Expand Down Expand Up @@ -77,9 +74,9 @@ tracing = { version = "0.1", features = [
# "release_max_level_off",
# "release_max_level_error",
# "release_max_level_warn",
"release_max_level_info",
# "release_max_level_info",
# "release_max_level_debug",
# "release_max_level_trace"
"release_max_level_trace",
] }

#
Expand All @@ -101,29 +98,23 @@ rust_decimal_macros = "1.35"
# --- BUILT-IN DRIVERS
# crate-type = ["lib"] => because plugins need to be compile as simple lib when built-in
# ---
# Fake devices
pza-plugin-fakes = { git = "https://github.com/Panduza/pza-plugin-fakes", tag = "0.1.0", optional = true }
# ---
# Korad devices
pza-plugin-korad = { git = "https://github.com/Panduza/pza-plugin-korad", tag = "1.0.3", features = [
"log",
], optional = true }
pza-plugin-korad = { git = "https://github.com/Panduza/pza-plugin-korad", tag = "1.0.4", optional = true }
# ---
# Hameg devices
pza-plugin-hameg = { git = "https://github.com/Panduza/pza-plugin-hameg", tag = "1.0.2", features = [
"log",
], optional = true }
pza-plugin-hameg = { git = "https://github.com/Panduza/pza-plugin-hameg", tag = "1.0.3", optional = true }

# --- FEATURES
[features]

# Enable tracing in stdout
log = []
log-issue = []
broker-log = []

# Enable tracing in tokio console
# trac-console = [ "console-subscriber" ]
[build-dependencies]
toml = "0.8.19"

# --- FEATURES
[features]

# ---
# Integrate some generic drivers directly into the platform
#
built-in-drivers = ["pza-plugin-korad", "pza-plugin-hameg"]
built-in-drivers = ["pza-plugin-korad", "pza-plugin-hameg", "pza-plugin-fakes"]
44 changes: 10 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,20 @@ You must install Rust and Cargo then execute this commands

```bash
# Enable fmt tracing (std terminal logs)
cargo run --features log
cargo run

# Log options
cargo run -- -h
# -l to log on terminal
# -b to enable broker logs on terminal
# -d to enable debug logs (terminal + file)
# -t to enable trace logs (terminal + file)
```

To embbed built-in drivers

```bash
# Enable fmt tracing (std terminal logs)
cargo run --features log,built-in-drivers
cargo build --features log,built-in-drivers
cargo run --features built-in-drivers
cargo build --features built-in-drivers
```

## Manage log levels

To set the log level at build time, please go to the Cargo.tml

Then edit the 'tacing' entry to set the level you want

```toml
tracing = { version = "0.1", features = [
# "max_level_off",
# "max_level_error",
# "max_level_warn",
"max_level_info",
# "max_level_debug",
# "max_level_trace",
# "release_max_level_off",
# "release_max_level_error",
# "release_max_level_warn",
"release_max_level_info",
# "release_max_level_debug",
# "release_max_level_trace"
]}
```

# Others


```bash
# Enable tokio console tracing
RUSTFLAGS="--cfg tokio_unstable" cargo run --features trac-console
```
32 changes: 32 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::fs::File;
use std::io::Write;
use std::process::Command;

fn main() {
// Get rustc version
let output = Command::new("rustc")
.args(&["--version"])
.output()
.expect("failed to execute process");
let rustc_version = String::from_utf8_lossy(&output.stdout);

// Get version from Cargo.toml (assuming it's in the same directory)
let cargo_toml = include_str!("Cargo.toml");
let package: toml::value::Table = toml::from_str(cargo_toml).unwrap();
let version = package["package"]["version"].as_str().unwrap();

// Format information for writing
let info = format!(
"pub static RUSTC_VERSION: &str = \"{}\";\n
pub static PLATFORM_VERSION: &str = \"{}\";\n",
rustc_version.trim_end_matches("\n"),
version
);

// Write information to file
let mut file = File::create("src/sys_info.rs").expect("Failed to create sys_info.rs");
file.write_all(info.as_bytes())
.expect("Failed to write to sys_info.rs");

println!("Information written to sys_info.rs");
}
1 change: 1 addition & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cargo run -- -l
1 change: 1 addition & 0 deletions run_with_builtin.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cargo run --features built-in-drivers -- -l
38 changes: 7 additions & 31 deletions src/__builtin_devices/cobolt/s0501/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,32 @@ use async_trait::async_trait;
use panduza_core::device::Device;
use serde_json::json;

use panduza_core::device::{traits::DeviceActions, traits::Hunter, traits::Producer};
use panduza_core::Error as PlatformError;
use panduza_core::device::{ traits::DeviceActions, traits::Producer, traits::Hunter };

use panduza_core::interface::builder::Builder as InterfaceBuilder;


use panduza_connectors::serial::tty::Config as SerialConfig;

use tokio_serial;

mod itf_cobolt_0501_blc;



static VID: u16 = 0x25dc;
static PID: u16 = 0x0006;

pub struct DeviceHunter;


#[async_trait]
impl Hunter for DeviceHunter {

async fn hunt(&self) -> Option<Vec<serde_json::Value>> {

let mut bag = Vec::new();

// println!("DeviceHunter::hunt");

let ports = match tokio_serial::available_ports() {
Ok(p) => p,
Err(_e) => return None
Err(_e) => return None,
};
for port in ports {
// println!("{:?}", port);

match port.port_type {
tokio_serial::SerialPortType::UsbPort(info) => {
if info.vid == VID && info.pid == PID {
Expand All @@ -56,29 +46,24 @@ impl Hunter for DeviceHunter {
}
))
}
},
}
_ => {}
}
}

if bag.is_empty() {
return None;
}
else {
} else {
return Some(bag);
}
}

}

struct S0501;

impl DeviceActions for S0501 {

/// Create the interfaces
fn interface_builders(&self, device: &Device)
-> Result<Vec<InterfaceBuilder>, PlatformError>
{
fn interface_builders(&self, device: &Device) -> Result<Vec<InterfaceBuilder>, PlatformError> {
let logger = device.clone_logger().clone();

let device_settings = device.settings.clone();
Expand All @@ -92,20 +77,14 @@ impl DeviceActions for S0501 {
serial_conf.serial_baudrate = Some(115200);

let mut list = Vec::new();
list.push(
itf_cobolt_0501_blc::build("blc", &serial_conf)
);
list.push(itf_cobolt_0501_blc::build("blc", &serial_conf));
return Ok(list);
}
}




pub struct DeviceProducer;

impl Producer for DeviceProducer {

fn settings_props(&self) -> serde_json::Value {
return json!([
{
Expand All @@ -131,10 +110,7 @@ impl Producer for DeviceProducer {
]);
}


fn produce(&self) -> Result<Box<dyn DeviceActions>, PlatformError> {
return Ok(Box::new(S0501{}));
return Ok(Box::new(S0501 {}));
}

}

Loading

0 comments on commit 2e6b24f

Please sign in to comment.