diff --git a/packages/obs/src/config.rs b/packages/obs/src/config.rs index 88078fe4c..be20c5b0b 100644 --- a/packages/obs/src/config.rs +++ b/packages/obs/src/config.rs @@ -142,14 +142,20 @@ const DEFAULT_CONFIG_FILE: &str = "obs"; /// ``` pub fn load_config(config_dir: Option) -> AppConfig { let config_dir = if let Some(path) = config_dir { - // Use the provided path - let path = std::path::Path::new(&path); - if path.extension().is_some() { - // If path has extension, use it as is (extension will be added by Config::builder) - path.with_extension("").to_string_lossy().into_owned() + // If a path is provided, check if it's empty + if path.is_empty() { + // If empty, use the default config file name + DEFAULT_CONFIG_FILE.to_string() } else { - // If path is a directory, append the default config file name - path.to_string_lossy().into_owned() + // Use the provided path + let path = std::path::Path::new(&path); + if path.extension().is_some() { + // If path has extension, use it as is (extension will be added by Config::builder) + path.with_extension("").to_string_lossy().into_owned() + } else { + // If path is a directory, append the default config file name + path.to_string_lossy().into_owned() + } } } else { // If no path provided, use current directory + default config file @@ -166,11 +172,11 @@ pub fn load_config(config_dir: Option) -> AppConfig { println!("Using config file base: {}", config_dir); let config = Config::builder() - .add_source(File::with_name(config_dir.as_str()).format(FileFormat::Toml)) + .add_source(File::with_name(config_dir.as_str()).format(FileFormat::Toml).required(false)) .add_source(File::with_name(config_dir.as_str()).format(FileFormat::Yaml).required(false)) .add_source(config::Environment::with_prefix("")) .build() - .unwrap(); + .unwrap_or_default(); - config.try_deserialize().unwrap() + config.try_deserialize().unwrap_or_default() } diff --git a/packages/obs/src/sink.rs b/packages/obs/src/sink.rs index f362d815f..cc3aed3e0 100644 --- a/packages/obs/src/sink.rs +++ b/packages/obs/src/sink.rs @@ -4,6 +4,7 @@ use std::sync::Arc; use tokio::fs::OpenOptions; use tokio::io; use tokio::io::AsyncWriteExt; +use tracing::debug; /// Sink Trait definition, asynchronously write logs #[async_trait] @@ -268,8 +269,19 @@ impl FileSink { flush_interval_ms: u64, flush_threshold: usize, ) -> Result { - let file = OpenOptions::new().append(true).create(true).open(&path).await?; - + // check if the file exists + let file_exists = tokio::fs::metadata(&path).await.is_ok(); + let file = if file_exists { + // If the file exists, open it in append mode + debug!("FileSink: File exists, opening in append mode."); + OpenOptions::new().append(true).create(true).open(&path).await? + } else { + // If the file does not exist, create it + debug!("FileSink: File does not exist, creating a new file."); + // Create the file and write a header or initial content if needed + OpenOptions::new().create(true).write(true).open(&path).await? + }; + // let file = OpenOptions::new().append(true).create(true).open(&path).await?; let writer = io::BufWriter::with_capacity(buffer_size, file); let now = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH)