Closed deepu105 closed 7 months ago
The plan is to implement a --debug
flag that enables logging to a file. The user can either use the default file path or specify a custom path. The solution involves updating the command-line interface to accept the new flag and implementing the logging functionality.
src/app/utils.rs
with initialize_debug_logging
function// src/app/utils.rs
use simplelog::*;
use std::fs::File;
pub fn initialize_debug_logging(log_file_path: Option<String>) -> Result<(), simplelog::TermLogError> {
let log_file = log_file_path.unwrap_or_else(|| "debug.log".to_string());
let log_config = ConfigBuilder::new()
.set_time_to_local(true)
.build();
WriteLogger::init(
LevelFilter::Debug,
log_config,
File::create(log_file)?
)
}
src/main.rs
to parse --debug
flag and initialize logging// src/main.rs
use clap::Parser;
use app::utils::initialize_debug_logging;
/// kdash CLI
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None, override_usage = "Press `?` while running the app to see keybindings", before_help = BANNER)]
pub struct Cli {
// ... existing fields ...
/// Enable debug mode and optionally specify a file path for the debug log
#[arg(long, value_parser, value_name = "FILE")]
pub debug: Option<Option<String>>,
}
#[tokio::main]
async fn main() -> Result<()> {
// ... existing code ...
// parse CLI arguments
let cli = Cli::parse();
// ... existing code ...
// Initialize debug logging if the --debug flag is present
if let Some(debug_option) = cli.debug {
initialize_debug_logging(debug_option)?;
}
// ... existing code ...
}
simplelog
to Cargo.toml
dependencies# Cargo.toml
[dependencies]
simplelog = "0.11.0" # Use the latest version compatible with your project
With these changes, the application will support a --debug
flag that initializes logging to a file. If no file path is specified, it defaults to debug.log
in the current directory. The initialize_debug_logging
function in src/app/utils.rs
sets up the logging, and the main
function in src/main.rs
parses the flag and calls this function if needed.
Click here to create a Pull Request with the proposed solution
Files used for this task:
Today when users encounter an issue its hard to reproduce as there are minimal debug info provided by the app. The debug flag should add logs to an external file. Default to a file in the current directory, but provide option to specify file path.