Skip to content

Commit

Permalink
Add --level flag
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Guzik <[email protected]>
  • Loading branch information
Bravo555 committed Oct 24, 2024
1 parent 94b992d commit 8f6d522
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 22 deletions.
19 changes: 15 additions & 4 deletions crates/core/tedge_agent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ pub struct AgentOpt {
#[clap(long)]
pub debug: bool,

/// Logging level.
///
/// One of error/warn/info/debug/trace. Takes precedence over `--debug`
#[clap(long)]
pub level: Option<tracing::Level>,

/// Start the agent with clean session off, subscribe to the topics, so that no messages are lost
#[clap(short, long)]
pub init: bool,
Expand Down Expand Up @@ -72,12 +78,17 @@ pub async fn run(agent_opt: AgentOpt) -> Result<(), anyhow::Error> {
let tedge_config_location =
tedge_config::TEdgeConfigLocation::from_custom_root(agent_opt.config_dir.clone());

// If `--level` was provided, use that log level.
// If `debug` is `false` then only `error!`, `warn!` and `info!` are reported.
// If `debug` is `true` then also `debug!` is reported.
let log_level = if agent_opt.debug {
tracing::Level::DEBUG
} else {
get_log_level("tedge-agent", &tedge_config_location.tedge_config_root_path)?
// If neither was provided, use a log level from a config file.
let log_level = agent_opt
.level
.or(agent_opt.debug.then_some(tracing::Level::DEBUG));

let log_level = match log_level {
Some(log_level) => log_level,
None => get_log_level("tedge-agent", &tedge_config_location.tedge_config_root_path)?,
};

set_log_level(log_level);
Expand Down
25 changes: 18 additions & 7 deletions crates/core/tedge_mapper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ pub struct MapperOpt {
#[clap(subcommand)]
pub name: MapperName,

/// Logging level.
///
/// One of error/warn/info/debug/trace. Takes precedence over `--debug`
#[clap(long)]
pub level: Option<tracing::Level>,

/// Turn-on the debug log level.
///
/// If off only reports ERROR, WARN, and INFO
Expand Down Expand Up @@ -122,14 +128,19 @@ pub async fn run(mapper_opt: MapperOpt) -> anyhow::Result<()> {
tedge_config::TEdgeConfigLocation::from_custom_root(&mapper_opt.config_dir);
let config = tedge_config::TEdgeConfig::try_new(tedge_config_location.clone())?;

let log_level = if mapper_opt.debug {
tracing::Level::DEBUG
} else {
get_log_level(
"tedge-mapper",
&tedge_config_location.tedge_config_root_path,
)?
// If `--level` was provided, use that log level.
// If `debug` is `false` then only `error!`, `warn!` and `info!` are reported.
// If `debug` is `true` then also `debug!` is reported.
// If neither was provided, use a log level from a config file.
let log_level = mapper_opt
.level
.or(mapper_opt.debug.then_some(tracing::Level::DEBUG));

let log_level = match log_level {
Some(log_level) => log_level,
None => get_log_level("tedge-agent", &tedge_config_location.tedge_config_root_path)?,
};

set_log_level(log_level);

// Run only one instance of a mapper (if enabled)
Expand Down
24 changes: 17 additions & 7 deletions crates/core/tedge_watchdog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ version = clap::crate_version!(),
about = clap::crate_description!()
)]
pub struct WatchdogOpt {
/// Logging level.
///
/// One of error/warn/info/debug/trace. Takes precedence over `--debug`
#[clap(long)]
pub level: Option<tracing::Level>,

/// Turn-on the debug log level.
///
/// If off only reports ERROR, WARN, and INFO
Expand All @@ -46,13 +52,17 @@ pub async fn run(watchdog_opt: WatchdogOpt) -> Result<(), anyhow::Error> {
let tedge_config_location =
tedge_config::TEdgeConfigLocation::from_custom_root(watchdog_opt.config_dir.clone());

let log_level = if watchdog_opt.debug {
tracing::Level::DEBUG
} else {
get_log_level(
"tedge-watchdog",
&tedge_config_location.tedge_config_root_path,
)?
// If `--level` was provided, use that log level.
// If `debug` is `false` then only `error!`, `warn!` and `info!` are reported.
// If `debug` is `true` then also `debug!` is reported.
// If neither was provided, use a log level from a config file.
let log_level = watchdog_opt
.level
.or(watchdog_opt.debug.then_some(tracing::Level::DEBUG));

let log_level = match log_level {
Some(log_level) => log_level,
None => get_log_level("tedge-agent", &tedge_config_location.tedge_config_root_path)?,
};

set_log_level(log_level);
Expand Down
22 changes: 18 additions & 4 deletions plugins/c8y_firmware_plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ about = clap::crate_description!(),
after_help = AFTER_HELP_TEXT
)]
pub struct FirmwarePluginOpt {
/// Logging level.
///
/// One of error/warn/info/debug/trace. Takes precedence over `--debug`
#[clap(long)]
pub level: Option<tracing::Level>,

/// Turn-on the debug log level.
///
/// If off only reports ERROR, WARN, and INFO
Expand Down Expand Up @@ -67,10 +73,18 @@ pub async fn run(firmware_plugin_opt: FirmwarePluginOpt) -> Result<(), anyhow::E
// Load tedge config from the provided location
let tedge_config_location =
tedge_config::TEdgeConfigLocation::from_custom_root(&firmware_plugin_opt.config_dir);
let log_level = if firmware_plugin_opt.debug {
tracing::Level::DEBUG
} else {
get_log_level(PLUGIN_NAME, &tedge_config_location.tedge_config_root_path)?

// If `--level` was provided, use that log level.
// If `debug` is `false` then only `error!`, `warn!` and `info!` are reported.
// If `debug` is `true` then also `debug!` is reported.
// If neither was provided, use a log level from a config file.
let log_level = firmware_plugin_opt
.level
.or(firmware_plugin_opt.debug.then_some(tracing::Level::DEBUG));

let log_level = match log_level {
Some(log_level) => log_level,
None => get_log_level("tedge-agent", &tedge_config_location.tedge_config_root_path)?,
};

set_log_level(log_level);
Expand Down

0 comments on commit 8f6d522

Please sign in to comment.