Skip to content

Commit

Permalink
Merge pull request #248 from anmenaga/debug_arg
Browse files Browse the repository at this point in the history
Added --logging-level arg
  • Loading branch information
anmenaga authored Nov 1, 2023
2 parents 7950e11 + 099f907 commit 84d0277
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
3 changes: 3 additions & 0 deletions dsc/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use clap::{Parser, Subcommand, ValueEnum};
use clap_complete::Shell;
use crate::util::LogLevel;

#[derive(Debug, Clone, PartialEq, Eq, ValueEnum)]
pub enum OutputFormat {
Expand All @@ -24,6 +25,8 @@ pub struct Args {
pub input: Option<String>,
#[clap(short = 'p', long, help = "The path to a file used as input to the configuration or resource")]
pub input_file: Option<String>,
#[clap(short = 'l', long = "logging-level", help = "Log level to display", value_enum, default_value = "info")]
pub logging_level: LogLevel,
}

#[derive(Debug, PartialEq, Eq, Subcommand)]
Expand Down
22 changes: 15 additions & 7 deletions dsc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use clap_complete::generate;
use std::io::{self, Read};
use std::process::exit;
use sysinfo::{Process, ProcessExt, RefreshKind, System, SystemExt, get_current_pid, ProcessRefreshKind};
use tracing::{error, info, warn};
use tracing::{Level, error, info, warn};

#[cfg(debug_assertions)]
use crossterm::event;
Expand All @@ -25,18 +25,26 @@ fn main() {
#[cfg(debug_assertions)]
check_debug();

// create subscriber that writes all events to stderr
let subscriber = tracing_subscriber::fmt().pretty().with_writer(std::io::stderr).finish();
if tracing::subscriber::set_global_default(subscriber).is_err() {
eprintln!("Unable to set global default subscriber");
}

if ctrlc::set_handler(ctrlc_handler).is_err() {
error!("Error: Failed to set Ctrl-C handler");
}

let args = Args::parse();

let tracing_level = match args.logging_level {
util::LogLevel::Error => Level::ERROR,
util::LogLevel::Warning => Level::WARN,
util::LogLevel::Info => Level::INFO,
util::LogLevel::Debug => Level::DEBUG,
util::LogLevel::Trace => Level::TRACE,
};

// create subscriber that writes all events to stderr
let subscriber = tracing_subscriber::fmt().pretty().with_max_level(tracing_level).with_writer(std::io::stderr).finish();
if tracing::subscriber::set_global_default(subscriber).is_err() {
eprintln!("Unable to set global default subscriber");
}

let input = if args.input.is_some() {
args.input
} else if args.input_file.is_some() {
Expand Down
10 changes: 10 additions & 0 deletions dsc/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ pub const EXIT_INVALID_INPUT: i32 = 4;
pub const EXIT_VALIDATION_FAILED: i32 = 5;
pub const EXIT_CTRL_C: i32 = 6;

#[derive(Debug)]
#[derive(clap::ValueEnum, Clone)]
pub enum LogLevel {
Error,
Warning,
Info,
Debug,
Trace
}

/// Get string representation of JSON value.
///
/// # Arguments
Expand Down
5 changes: 5 additions & 0 deletions dsc/tests/dsc_args.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,9 @@ resources:
$LASTEXITCODE | Should -Be 2
}

It '--logging-level has effect' {
dsc -l debug resource get -r Microsoft/OSInfo 2> $TestDrive/tracing.txt
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'DEBUG'
$LASTEXITCODE | Should -Be 0
}
}

0 comments on commit 84d0277

Please sign in to comment.