From 1d5f5fd0f3af33dda561631d8e5e3a4bc9b1cf14 Mon Sep 17 00:00:00 2001 From: finga Date: Sat, 23 Apr 2022 20:31:57 +0200 Subject: [PATCH] Add logging Print some somewhat usefull log messages. --- Cargo.lock | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/main.rs | 16 +++++++++++-- 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9a86816..6d39586 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -38,6 +38,17 @@ version = "1.0.57" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08f9b8508dccb7687a1d6c4ce66b2b0ecef467c94667de27d8d7fe1f8d2a9cdc" +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -62,6 +73,34 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "env_logger" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b2cf0344971ee6c64c31be0d530793fba457d322dfec2810c453d0ef228f9c3" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "libc" version = "0.2.124" @@ -88,6 +127,15 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "log" +version = "0.4.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6389c490849ff5bc16be905ae24bc913a9c8892e19b2341dbc175e14c341c2b8" +dependencies = [ + "cfg-if", +] + [[package]] name = "mach" version = "0.1.2" @@ -126,6 +174,8 @@ name = "mightyohm-gc-exporter" version = "0.1.0-dev" dependencies = [ "anyhow", + "env_logger", + "log", "serialport", ] @@ -182,6 +232,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + [[package]] name = "winapi" version = "0.3.9" @@ -198,6 +257,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 69355f2..52a83f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,5 @@ categories = ["api-bindings", "command-line-utilities", "visualization"] [dependencies] anyhow = "1.0" serialport = "4.1" +log = "0.4" +env_logger = "0.9" diff --git a/src/main.rs b/src/main.rs index 971348e..f062d93 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use anyhow::{bail, Error, Result}; +use log::{debug, trace}; use std::{ io::{BufRead, BufReader}, str::FromStr, @@ -33,6 +34,14 @@ fn parse(input: &str) -> Result<(usize, usize, f32, Speed)> { .replace(", ", " "); let input: Vec<&str> = input.split(' ').collect(); + trace!( + "Parsed values: cps = {}, cpm = {}, μSv/h = {}, mode = {:?}", + input[0], + input[1], + input[2], + input[3] + ); + Ok(( input[0].parse::()?, input[1].parse::()?, @@ -42,6 +51,8 @@ fn parse(input: &str) -> Result<(usize, usize, f32, Speed)> { } fn main() -> Result<()> { + env_logger::init(); + let port_name = "/dev/serial0"; let baud_rate = 9600; @@ -53,7 +64,8 @@ fn main() -> Result<()> { loop { let mut line = String::new(); port.read_line(&mut line)?; - println!("{:?}", line); - println!("{:?}", parse(line.trim())?); + let line = line.trim(); + debug!("Reading line from serial port: {}", line); + let (_cps, _cpm, _radiation, _mode) = parse(line)?; } }