Skip to content

Commit

Permalink
🔧 Add logging control to CLI and standardize log format
Browse files Browse the repository at this point in the history
Refactor `src/cli.rs` to include a new logging flag for debugging and control
logging behavior. Standardize the log format in `src/logger.rs` by consolidating
the timestamp and log message creation for file-only output. This change aims to
enhance debugging capabilities and consistency in logging output.
  • Loading branch information
hyperb1iss committed Jul 29, 2024
1 parent 7620dcf commit a7db0a3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 16 deletions.
53 changes: 42 additions & 11 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ use colored::*;

/// CLI structure defining the available commands and global arguments
#[derive(Parser)]
#[command(author, version = crate_version!(), about = "AI-assisted Git commit message generator", long_about = None)]
#[command(
author,
version = crate_version!(),
about = "AI-assisted Git commit message generator",
long_about = None,
disable_version_flag = true
)]
pub struct Cli {
/// Subcommands available for the CLI
#[command(subcommand)]
Expand All @@ -19,6 +25,24 @@ pub struct Cli {
help = "Automatically commit with the generated message"
)]
pub auto_commit: bool,

/// Log debug messages to a file
#[arg(
short = 'l',
long = "log",
global = true,
help = "Log debug messages to a file"
)]
pub log: bool,

/// Display the version
#[arg(
short = 'v',
long = "version",
global = true,
help = "Display the version"
)]
pub version: bool,
}

/// Enumeration of available subcommands
Expand All @@ -30,10 +54,7 @@ pub enum Commands {
after_help = "Default LLM provider: openai\nAvailable providers: claude, openai"
)]
Gen {
#[arg(short, long, help = "Enable verbose mode")]
verbose: bool,

#[arg(short, long, help = "Override use_gitmoji setting")]
#[arg(short = 'g', long, help = "Override use_gitmoji setting")]
gitmoji: Option<bool>,

#[arg(long, help = "Override default LLM provider")]
Expand All @@ -57,11 +78,11 @@ pub enum Commands {
)]
param: Option<Vec<String>>,

#[arg(short, long, help = "Set use_gitmoji preference")]
#[arg(short = 'g', long, help = "Set use_gitmoji preference")]
gitmoji: Option<bool>,

#[arg(
short,
short = 'c',
long,
help = "Set custom instructions for the commit message generation"
)]
Expand Down Expand Up @@ -100,24 +121,34 @@ pub fn print_dynamic_help() {
/// Main function to parse arguments and handle the command
pub async fn main() -> anyhow::Result<()> {
let cli = parse_args();

if cli.version {
println!("Version: {}", crate_version!());
return Ok(());
}

if cli.log {
crate::logger::enable_logging();
} else {
crate::logger::disable_logging();
}

handle_command(cli).await
}

/// Handle the command based on parsed arguments
pub async fn handle_command(cli: Cli) -> anyhow::Result<()> {
match cli.command {
Commands::Gen {
verbose,
gitmoji,
provider,
} => {
log_debug!(
"Handling 'gen' command with verbose: {}, gitmoji: {:?}, provider: {:?}",
verbose,
"Handling 'gen' command with gitmoji: {:?}, provider: {:?}",
gitmoji,
provider
);
commands::handle_gen_command(verbose, gitmoji, provider, cli.auto_commit).await?;
commands::handle_gen_command(cli.log, gitmoji, provider, cli.auto_commit).await?;
}
Commands::Config {
provider,
Expand Down
28 changes: 23 additions & 5 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,32 @@ static LOG_FILE: Lazy<Mutex<std::fs::File>> = Lazy::new(|| {
)
});

/// Flag to control whether logging is enabled
static LOGGING_ENABLED: Lazy<Mutex<bool>> = Lazy::new(|| Mutex::new(false));

/// Function to enable logging
pub fn enable_logging() {
let mut logging_enabled = LOGGING_ENABLED.lock().unwrap();
*logging_enabled = true;
}

/// Function to disable logging
pub fn disable_logging() {
let mut logging_enabled = LOGGING_ENABLED.lock().unwrap();
*logging_enabled = false;
}

/// Log a message with the given level
pub fn log(level: &str, message: &str) {
let timestamp = Local::now().format("%Y-%m-%d %H:%M:%S%.3f");
let log_message = format!("{} {}: {}\n", timestamp, level, message);
let logging_enabled = LOGGING_ENABLED.lock().unwrap();
if *logging_enabled {
let timestamp = Local::now().format("%Y-%m-%d %H:%M:%S%.3f");
let log_message = format!("{} {}: {}\n", timestamp, level, message);

// Write to file only
if let Ok(mut file) = LOG_FILE.lock() {
let _ = file.write_all(log_message.as_bytes());
// Write to file only
if let Ok(mut file) = LOG_FILE.lock() {
let _ = file.write_all(log_message.as_bytes());
}
}
}

Expand Down

0 comments on commit a7db0a3

Please sign in to comment.