Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added --logging-level arg #248

Merged
merged 3 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}