From 2c6d4917f42f6524cb09a71ebe7b1f01ebbb00f8 Mon Sep 17 00:00:00 2001 From: Cyril Fougeray Date: Mon, 6 May 2024 23:21:15 +0200 Subject: [PATCH] mcu-util: better logs (#91) * mcu-util: better logs use color-eyre use RUST_LOG env variable when given fallback on INFO log level * mcu-util: remove some allow(dead_code) --- Cargo.lock | 2 +- mcu-util/Cargo.toml | 2 +- mcu-util/src/logging.rs | 24 +++++++++++++----------- mcu-util/src/main.rs | 10 +++------- mcu-util/src/messaging/can/canfd.rs | 2 +- mcu-util/src/messaging/can/isotp.rs | 4 +--- mcu-util/src/messaging/mod.rs | 3 +-- mcu-util/src/messaging/serial/mod.rs | 2 +- mcu-util/src/orb/dfu.rs | 2 +- mcu-util/src/orb/main_board.rs | 2 +- mcu-util/src/orb/mod.rs | 2 +- mcu-util/src/orb/security_board.rs | 2 +- 12 files changed, 26 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 19f61163..f0e603b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2340,8 +2340,8 @@ dependencies = [ "async-trait", "can-rs", "clap", + "color-eyre", "crc32fast", - "eyre", "image", "orb-messages", "prost", diff --git a/mcu-util/Cargo.toml b/mcu-util/Cargo.toml index d74fe899..a51f5cd5 100644 --- a/mcu-util/Cargo.toml +++ b/mcu-util/Cargo.toml @@ -16,7 +16,7 @@ async-trait = "0.1.77" can-rs = { path = "../can", features = ["isotp"] } clap = { workspace = true, features = ["derive"] } crc32fast = "1.3.2" -eyre.workspace = true +color-eyre.workspace = true image = "0.24.8" orb-messages.workspace = true prost = "0.12.3" diff --git a/mcu-util/src/logging.rs b/mcu-util/src/logging.rs index 990dde90..12518879 100644 --- a/mcu-util/src/logging.rs +++ b/mcu-util/src/logging.rs @@ -1,22 +1,24 @@ -use eyre::Result; +use color_eyre::eyre::Result; use tracing::level_filters::LevelFilter; use tracing_subscriber::layer::SubscriberExt; use tracing_subscriber::util::SubscriberInitExt; -use tracing_subscriber::Layer; +use tracing_subscriber::{EnvFilter, Layer}; + +/// Initialize the logger +pub fn init() -> Result<()> { + let filter = EnvFilter::builder() + .with_default_directive(LevelFilter::INFO.into()) + .from_env_lossy(); -fn try_init_stdout_logger(loglevel: tracing::Level) -> Result<()> { let stdout_log = tracing_subscriber::fmt::layer() .compact() .with_writer(std::io::stdout) - .with_filter(LevelFilter::from_level(loglevel)); - - tracing_subscriber::registry().with(stdout_log).try_init()?; + .with_filter(filter); - Ok(()) -} + let registry = tracing_subscriber::registry(); + #[cfg(tokio_unstable)] + let registry = registry.with(console_subscriber::spawn()); + registry.with(stdout_log).try_init()?; -/// Initialize the logger -pub fn init(loglevel: tracing::Level) -> Result<()> { - try_init_stdout_logger(loglevel)?; Ok(()) } diff --git a/mcu-util/src/main.rs b/mcu-util/src/main.rs index 44b4ed08..2a444914 100644 --- a/mcu-util/src/main.rs +++ b/mcu-util/src/main.rs @@ -3,7 +3,7 @@ extern crate core; use crate::orb::Orb; use clap::Parser; -use eyre::{Context, Result}; +use color_eyre::eyre::{Context, Result}; use std::io::Write; use std::path::PathBuf; use std::time::Duration; @@ -122,11 +122,6 @@ enum SecureElement { PowerCycle, } -#[cfg(debug_assertions)] -const LOG_LEVEL: tracing::Level = tracing::Level::DEBUG; -#[cfg(not(debug_assertions))] -const LOG_LEVEL: tracing::Level = tracing::Level::INFO; - async fn execute(args: Args) -> Result<()> { let mut orb = Orb::new().await?; @@ -190,7 +185,8 @@ async fn execute(args: Args) -> Result<()> { #[tokio::main] async fn main() -> Result<()> { - logging::init(LOG_LEVEL)?; + color_eyre::install()?; + logging::init()?; let args = Args::parse(); diff --git a/mcu-util/src/messaging/can/canfd.rs b/mcu-util/src/messaging/can/canfd.rs index 589b8a93..c917dbac 100644 --- a/mcu-util/src/messaging/can/canfd.rs +++ b/mcu-util/src/messaging/can/canfd.rs @@ -2,7 +2,7 @@ use async_trait::async_trait; use can_rs::filter::Filter; use can_rs::stream::FrameStream; use can_rs::{Frame, Id, CANFD_DATA_LEN}; -use eyre::{eyre, Context, Result}; +use color_eyre::eyre::{eyre, Context, Result}; use orb_messages::CommonAckError; use prost::Message; use std::sync::atomic::{AtomicU16, Ordering}; diff --git a/mcu-util/src/messaging/can/isotp.rs b/mcu-util/src/messaging/can/isotp.rs index 44ce265e..cd0a41da 100644 --- a/mcu-util/src/messaging/can/isotp.rs +++ b/mcu-util/src/messaging/can/isotp.rs @@ -1,5 +1,5 @@ use async_trait::async_trait; -use eyre::{eyre, Context, Result}; +use color_eyre::eyre::{eyre, Context, Result}; use orb_messages::CommonAckError; use prost::Message; use std::io::{Read, Write}; @@ -30,7 +30,6 @@ const CAN_ADDR_IS_DEST: u32 = 1 << 9; /// for bidirectional communication, addresses are comprised of source and destination digit /// along with some flags. #[derive(Clone, Copy, PartialEq, Debug)] -#[allow(dead_code)] pub enum IsoTpNodeIdentifier { MainMcu = 0x1, SecurityMcu = 0x2, @@ -223,7 +222,6 @@ fn can_rx( impl MessagingInterface for CanIsoTpMessaging { /// Send payload into McuMessage /// One could decide to only listen for ISO-TP message so allow dead code for `send` method - #[allow(dead_code)] async fn send(&mut self, payload: McuPayload) -> Result { let ack_number = process::id() << 16 | self.ack_num_lsb.load(Ordering::Relaxed) as u32; diff --git a/mcu-util/src/messaging/mod.rs b/mcu-util/src/messaging/mod.rs index bd37b938..6e78c41f 100644 --- a/mcu-util/src/messaging/mod.rs +++ b/mcu-util/src/messaging/mod.rs @@ -2,7 +2,7 @@ use std::process; use std::sync::mpsc; use async_trait::async_trait; -use eyre::{eyre, Result}; +use color_eyre::eyre::{eyre, Result}; use orb_messages::CommonAckError; use tracing::debug; @@ -19,7 +19,6 @@ pub enum McuPayload { /// CAN(-FD) addressing scheme #[derive(Clone, Copy, PartialEq, Debug)] -#[allow(dead_code)] pub enum Device { Main = 0x01, Security = 0x02, diff --git a/mcu-util/src/messaging/serial/mod.rs b/mcu-util/src/messaging/serial/mod.rs index 41c9b045..16ec665f 100644 --- a/mcu-util/src/messaging/serial/mod.rs +++ b/mcu-util/src/messaging/serial/mod.rs @@ -1,6 +1,6 @@ use crate::messaging::{Device, McuPayload, MessagingInterface}; use async_trait::async_trait; -use eyre::{eyre, Result}; +use color_eyre::eyre::{eyre, Result}; use orb_messages::CommonAckError; use prost::Message; use std::sync::atomic::{AtomicU16, Ordering}; diff --git a/mcu-util/src/orb/dfu.rs b/mcu-util/src/orb/dfu.rs index ab35bac2..29112fdf 100644 --- a/mcu-util/src/orb/dfu.rs +++ b/mcu-util/src/orb/dfu.rs @@ -1,4 +1,4 @@ -use eyre::{eyre, Result}; +use color_eyre::eyre::{eyre, Result}; use orb_messages::mcu_main as main_messaging; use orb_messages::mcu_sec as sec_messaging; use std::cmp::min; diff --git a/mcu-util/src/orb/main_board.rs b/mcu-util/src/orb/main_board.rs index 3d95bae3..a8d640ea 100644 --- a/mcu-util/src/orb/main_board.rs +++ b/mcu-util/src/orb/main_board.rs @@ -1,5 +1,5 @@ use async_trait::async_trait; -use eyre::{eyre, Context, Result}; +use color_eyre::eyre::{eyre, Context, Result}; use orb_messages::{mcu_main as main_messaging, CommonAckError}; use std::ops::Sub; use std::sync::mpsc; diff --git a/mcu-util/src/orb/mod.rs b/mcu-util/src/orb/mod.rs index a0b526bb..32f51b2e 100644 --- a/mcu-util/src/orb/mod.rs +++ b/mcu-util/src/orb/mod.rs @@ -2,7 +2,7 @@ use std::fmt::{Display, Formatter}; use std::time::Duration; use async_trait::async_trait; -use eyre::Result; +use color_eyre::eyre::Result; use orb_messages::mcu_main as main_messaging; use orb_messages::mcu_sec as sec_messaging; diff --git a/mcu-util/src/orb/security_board.rs b/mcu-util/src/orb/security_board.rs index c6d6eaeb..76f4d1df 100644 --- a/mcu-util/src/orb/security_board.rs +++ b/mcu-util/src/orb/security_board.rs @@ -1,5 +1,5 @@ use async_trait::async_trait; -use eyre::{eyre, Context, Result}; +use color_eyre::eyre::{eyre, Context, Result}; use orb_messages::mcu_sec::battery_status::BatteryState; use orb_messages::{mcu_sec as security_messaging, CommonAckError}; use std::ops::Sub;