worlds simplest logging to see where things are blocked (#3888)
Co-authored-by: Michael Neale <michael.neale@gmail.com>
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
use anyhow::{Context, Result};
|
||||
use etcetera::{choose_app_strategy, AppStrategy};
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Once;
|
||||
@@ -21,33 +19,36 @@ static INIT: Once = Once::new();
|
||||
/// Returns the directory where log files should be stored.
|
||||
/// Creates the directory structure if it doesn't exist.
|
||||
fn get_log_directory() -> Result<PathBuf> {
|
||||
get_log_directory_with_date(None)
|
||||
goose::logging::get_log_directory("cli", true)
|
||||
}
|
||||
|
||||
/// Internal function that allows specifying a custom date string for testing
|
||||
#[cfg(test)]
|
||||
fn get_log_directory_with_date(test_date: Option<String>) -> Result<PathBuf> {
|
||||
// choose_app_strategy().state_dir()
|
||||
// - macOS/Linux: ~/.local/state/goose/logs/cli
|
||||
// - Windows: ~\AppData\Roaming\Block\goose\data\logs\cli
|
||||
// - Windows has no convention for state_dir, use data_dir instead
|
||||
let home_dir = choose_app_strategy(crate::APP_STRATEGY.clone())
|
||||
.context("HOME environment variable not set")?;
|
||||
use etcetera::{choose_app_strategy, AppStrategy};
|
||||
use goose::config::APP_STRATEGY;
|
||||
|
||||
let home_dir =
|
||||
choose_app_strategy(APP_STRATEGY.clone()).context("HOME environment variable not set")?;
|
||||
|
||||
let base_log_dir = home_dir
|
||||
.in_state_dir("logs/cli")
|
||||
.unwrap_or_else(|| home_dir.in_data_dir("logs/cli"));
|
||||
.in_state_dir("logs")
|
||||
.unwrap_or_else(|| home_dir.in_data_dir("logs"));
|
||||
|
||||
// Create date-based subdirectory
|
||||
let date_str = test_date.unwrap_or_else(|| {
|
||||
let component_dir = base_log_dir.join("cli");
|
||||
|
||||
let log_dir = if let Some(date_str) = test_date {
|
||||
component_dir.join(date_str)
|
||||
} else {
|
||||
// Create date-based subdirectory
|
||||
let now = chrono::Local::now();
|
||||
now.format("%Y-%m-%d").to_string()
|
||||
});
|
||||
let date_dir = base_log_dir.join(date_str);
|
||||
component_dir.join(now.format("%Y-%m-%d").to_string())
|
||||
};
|
||||
|
||||
// Ensure log directory exists
|
||||
fs::create_dir_all(&date_dir).context("Failed to create log directory")?;
|
||||
std::fs::create_dir_all(&log_dir).context("Failed to create log directory")?;
|
||||
|
||||
Ok(date_dir)
|
||||
Ok(log_dir)
|
||||
}
|
||||
|
||||
/// Sets up the logging infrastructure for the application.
|
||||
@@ -274,14 +275,14 @@ mod tests {
|
||||
println!("Log directory: {}", log_dir.display());
|
||||
println!("Test directory: {}", test_dir.display());
|
||||
if log_dir.exists() {
|
||||
for entry in fs::read_dir(&log_dir).unwrap() {
|
||||
for entry in std::fs::read_dir(&log_dir).unwrap() {
|
||||
let entry = entry.unwrap();
|
||||
if entry.path().extension().map_or(false, |ext| ext == "log") {
|
||||
fs::remove_file(entry.path()).unwrap();
|
||||
std::fs::remove_file(entry.path()).unwrap();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fs::create_dir_all(&log_dir).unwrap();
|
||||
std::fs::create_dir_all(&log_dir).unwrap();
|
||||
}
|
||||
println!("Log directory created: {}", log_dir.exists());
|
||||
|
||||
@@ -306,7 +307,7 @@ mod tests {
|
||||
println!("Log directory exists: {}", log_dir.exists());
|
||||
|
||||
// Check if there are any log files directly
|
||||
let all_files = fs::read_dir(&log_dir)
|
||||
let all_files = std::fs::read_dir(&log_dir)
|
||||
.unwrap_or_else(|e| {
|
||||
println!("Error reading log directory: {}", e);
|
||||
panic!("Failed to read log directory: {}", e);
|
||||
@@ -331,12 +332,12 @@ mod tests {
|
||||
format!("{}.log", timestamp)
|
||||
};
|
||||
let log_path = log_dir.join(&log_filename);
|
||||
fs::write(&log_path, "Test log content").unwrap();
|
||||
std::fs::write(&log_path, "Test log content").unwrap();
|
||||
println!("Created test log file: {}", log_path.display());
|
||||
}
|
||||
|
||||
// Read directory again after potential manual creation
|
||||
let entries = fs::read_dir(&log_dir)
|
||||
let entries = std::fs::read_dir(&log_dir)
|
||||
.unwrap_or_else(|e| {
|
||||
println!("Error reading log directory: {}", e);
|
||||
panic!("Failed to read log directory: {}", e);
|
||||
|
||||
Reference in New Issue
Block a user