Skip to content

Commit

Permalink
mcu-util: better logs (#91)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
fouge authored May 6, 2024
1 parent fb396e1 commit 2c6d491
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion mcu-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
24 changes: 13 additions & 11 deletions mcu-util/src/logging.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
10 changes: 3 additions & 7 deletions mcu-util/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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?;

Expand Down Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion mcu-util/src/messaging/can/canfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
4 changes: 1 addition & 3 deletions mcu-util/src/messaging/can/isotp.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<CommonAckError> {
let ack_number =
process::id() << 16 | self.ack_num_lsb.load(Ordering::Relaxed) as u32;
Expand Down
3 changes: 1 addition & 2 deletions mcu-util/src/messaging/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion mcu-util/src/messaging/serial/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
2 changes: 1 addition & 1 deletion mcu-util/src/orb/dfu.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion mcu-util/src/orb/main_board.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion mcu-util/src/orb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion mcu-util/src/orb/security_board.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 2c6d491

Please sign in to comment.