Skip to content

Commit 2be5243

Browse files
committed
refactor: rewrite env logger, remove redundant logs
Signed-off-by: addrian-77 <lunguadrian30@gmail.com>
1 parent c3dcc93 commit 2be5243

7 files changed

Lines changed: 27 additions & 38 deletions

File tree

tockloader-cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ inquire = "0.7.5"
2727
tockloader-lib = { path = "../tockloader-lib/" }
2828
anyhow = "1.0.89"
2929
env_logger = "0.11.8"
30+
log = "0.4.27"

tockloader-cli/src/cli.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ pub fn make_cli() -> Command {
1818
arg!(--"log-level" <LEVEL>)
1919
.required(false)
2020
.value_parser(["error", "warn", "info", "debug", "trace"])
21+
.default_value("info")
22+
.num_args(0..=1)
23+
.default_missing_value("info")
2124
.global(true),
2225
)
2326
// Note: arg_require_else_help will trigger the help command if no argument/subcommand is given.

tockloader-cli/src/main.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,28 @@ async fn main() -> Result<()> {
133133
let user_level = matches
134134
.get_one::<String>("log-level")
135135
.map(String::as_str)
136-
.unwrap_or("info");
137-
138-
let filter = format!(
139-
"tockloader=trace,\
140-
tockloader_lib={user_level},\
141-
probe_rs=warn,\
142-
nusb=warn,\
143-
tracing=warn,\
144-
tracing_span=warn"
136+
.unwrap();
137+
138+
let mut builder = env_logger::Builder::new();
139+
140+
builder.filter_module(
141+
"tockloader_lib",
142+
match user_level {
143+
"error" => log::LevelFilter::Error,
144+
"warn" => log::LevelFilter::Warn,
145+
"info" => log::LevelFilter::Info,
146+
"debug" => log::LevelFilter::Debug,
147+
"trace" => log::LevelFilter::Trace,
148+
_ => log::LevelFilter::Info,
149+
},
145150
);
151+
builder.filter_module("tockloader", log::LevelFilter::Trace);
152+
builder.filter_module("probe_rs", log::LevelFilter::Warn);
153+
builder.filter_module("nusb", log::LevelFilter::Warn);
154+
builder.filter_module("tracing", log::LevelFilter::Warn);
155+
builder.filter_module("tracing_span", log::LevelFilter::Warn);
146156

147-
std::env::set_var("RUST_LOG", &filter);
148-
149-
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
157+
builder.init();
150158

151159
match matches.subcommand() {
152160
Some(("listen", sub_matches)) => {

tockloader-lib/src/attributes/app_attributes.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ use tokio_serial::SerialStream;
1212
use crate::bootloader_serial::{issue_command, Command, Response};
1313
use crate::errors::TockloaderError;
1414

15-
use log::info;
16-
1715
/// This structure contains all relevant information about a tock application.
1816
///
1917
/// All data is stored either within [TbfHeader]s, or [TbfFooter]s.
@@ -80,8 +78,6 @@ impl AppAttributes {
8078
.read(appaddr, &mut appdata)
8179
.map_err(TockloaderError::ProbeRsReadError)?;
8280

83-
info!("Read board_core");
84-
8581
let tbf_version: u16;
8682
let header_size: u16;
8783
let total_size: u32;
@@ -100,12 +96,8 @@ impl AppAttributes {
10096
tbf_version = data.0;
10197
header_size = data.1;
10298
total_size = data.2;
103-
info!("Obtained tbf header");
104-
}
105-
_ => {
106-
info!("Buffer is too short!");
107-
return Ok(apps_details);
10899
}
100+
_ => return Ok(apps_details),
109101
};
110102

111103
let mut header_data = vec![0u8; header_size as usize];
@@ -154,7 +146,6 @@ impl AppAttributes {
154146
break;
155147
}
156148
}
157-
info!("Obtained header and footer");
158149

159150
let details: AppAttributes = AppAttributes::new(header, footers);
160151

@@ -199,7 +190,7 @@ impl AppAttributes {
199190
// Read the first 8 bytes, which is the length of a TLV header.
200191
let (_, appdata) =
201192
issue_command(port, Command::ReadRange, pkt, true, 8, Response::ReadRange).await?;
202-
info!("Issued pkt and obtained appdata");
193+
203194
let tbf_version: u16;
204195
let header_size: u16;
205196
let total_size: u32;
@@ -241,7 +232,6 @@ impl AppAttributes {
241232

242233
let header = parse_tbf_header(&header_data, tbf_version)
243234
.map_err(TockloaderError::ParsingError)?;
244-
info!("Obtained header");
245235
let binary_end_offset = header.get_binary_end();
246236

247237
let mut footers: Vec<TbfFooter> = vec![];
@@ -286,7 +276,7 @@ impl AppAttributes {
286276
break;
287277
}
288278
}
289-
info!("Obtained footer");
279+
290280
let details: AppAttributes = AppAttributes::new(header, footers);
291281

292282
apps_details.insert(apps_counter, details);

tockloader-lib/src/attributes/decode.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed under the Apache License, Version 2.0 or the MIT License.
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33
// Copyright OXIDOS AUTOMOTIVE 2024.
4-
use log::info;
54

65
/// Attributes are key-value pairs that describe hardware configuration, stored
76
/// in a fixed 64-byte format:
@@ -44,7 +43,6 @@ pub(crate) fn decode_attribute(step: &[u8]) -> Option<DecodedAttribute> {
4443
for n in decoder_key {
4544
key.push(n.expect("Error getting key for attributes."));
4645
}
47-
info!("Decoded raw key");
4846
key = key.trim_end_matches('\0').to_string();
4947
let vlen = step[8];
5048

@@ -59,7 +57,6 @@ pub(crate) fn decode_attribute(step: &[u8]) -> Option<DecodedAttribute> {
5957
for n in decoder_value {
6058
value.push(n.expect("Error getting key for attributes."));
6159
}
62-
info!("Decoded raw value");
6360
value = value.trim_end_matches('\0').to_string();
6461
Some(DecodedAttribute::new(key, value))
6562
}
@@ -79,6 +76,5 @@ pub(crate) fn bytes_to_string(raw: &[u8]) -> String {
7976
for n in decoder {
8077
string.push(n.expect("Error getting key for attributes."));
8178
}
82-
info!("Turned whole byte sequence into a string");
8379
string
8480
}

tockloader-lib/src/attributes/system_attributes.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ use crate::errors::TockloaderError;
1111

1212
use super::decode::{bytes_to_string, decode_attribute};
1313

14-
// use log::info;
15-
// TODO: add actual logs here
1614
/// This structure contains all relevant information about board that is stored
1715
/// in the bootloader ROM.
1816
///

tockloader-lib/src/bootloader_serial.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use std::time::Duration;
1111
use tokio::io::{AsyncReadExt, AsyncWriteExt};
1212
use tokio_serial::{SerialPort, SerialStream};
1313

14-
use log::{error, info};
1514
// Tell the bootloader to reset its buffer to handle a new command
1615
pub const SYNC_MESSAGE: [u8; 3] = [0x00, 0xFC, 0x05];
1716

@@ -115,7 +114,6 @@ pub async fn toggle_bootloader_entry_dtr_rts(
115114
port.write_request_to_send(false)
116115
.map_err(TockloaderError::SerialInitializationError)?;
117116

118-
info!("Terminal is ready");
119117
Ok(())
120118
}
121119

@@ -137,11 +135,9 @@ pub async fn ping_bootloader_and_wait_for_response(
137135
read_bytes += port.read_buf(&mut ret).await?;
138136
}
139137
if ret[1] == Response::Pong as u8 {
140-
info!("Bootloader responded to ping");
141138
return Ok(Response::from(ret[1]));
142139
}
143140
}
144-
error!("Bootloader sent unexpected response");
145141
Ok(Response::from(ret[1]))
146142
}
147143

@@ -171,20 +167,17 @@ pub async fn issue_command(
171167
message.push(command as u8);
172168

173169
// If there should be a sync/reset message, prepend the outgoing message with it
174-
info!("Message generated");
175170
if sync {
176171
message.insert(0, SYNC_MESSAGE[0]);
177172
message.insert(1, SYNC_MESSAGE[1]);
178173
message.insert(2, SYNC_MESSAGE[2]);
179174
}
180-
info!("Inserted sync message");
181175

182176
// Write the command message
183177
let mut bytes_written = 0;
184178
while bytes_written != message.len() {
185179
bytes_written += port.write_buf(&mut &message[bytes_written..]).await?;
186180
}
187-
info!("Wrote command");
188181

189182
// Response has a two byte header, then response_len bytes
190183
let bytes_to_read = 2 + response_len;

0 commit comments

Comments
 (0)