From 1698992de6a601e65a0598ed132cab5419f7e578 Mon Sep 17 00:00:00 2001 From: Alex Vinyals Date: Sun, 9 Jul 2023 16:35:07 +0200 Subject: Fix `:log-open` when `--log` is specified (#7573) --- helix-loader/src/lib.rs | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) (limited to 'helix-loader') diff --git a/helix-loader/src/lib.rs b/helix-loader/src/lib.rs index ad4ad899..c51c9c7d 100644 --- a/helix-loader/src/lib.rs +++ b/helix-loader/src/lib.rs @@ -11,21 +11,20 @@ static RUNTIME_DIRS: once_cell::sync::Lazy> = static CONFIG_FILE: once_cell::sync::OnceCell = once_cell::sync::OnceCell::new(); -pub fn initialize_config_file(specified_file: Option) { - let config_file = specified_file.unwrap_or_else(|| { - let config_dir = config_dir(); - - if !config_dir.exists() { - std::fs::create_dir_all(&config_dir).ok(); - } +static LOG_FILE: once_cell::sync::OnceCell = once_cell::sync::OnceCell::new(); - config_dir.join("config.toml") - }); - - // We should only initialize this value once. +pub fn initialize_config_file(specified_file: Option) { + let config_file = specified_file.unwrap_or_else(default_config_file); + ensure_parent_dir(&config_file); CONFIG_FILE.set(config_file).ok(); } +pub fn initialize_log_file(specified_file: Option) { + let log_file = specified_file.unwrap_or_else(default_log_file); + ensure_parent_dir(&log_file); + LOG_FILE.set(log_file).ok(); +} + /// A list of runtime directories from highest to lowest priority /// /// The priority is: @@ -122,10 +121,11 @@ pub fn cache_dir() -> PathBuf { } pub fn config_file() -> PathBuf { - CONFIG_FILE - .get() - .map(|path| path.to_path_buf()) - .unwrap_or_else(|| config_dir().join("config.toml")) + CONFIG_FILE.get().map(|path| path.to_path_buf()).unwrap() +} + +pub fn log_file() -> PathBuf { + LOG_FILE.get().map(|path| path.to_path_buf()).unwrap() } pub fn workspace_config_file() -> PathBuf { @@ -136,7 +136,7 @@ pub fn lang_config_file() -> PathBuf { config_dir().join("languages.toml") } -pub fn log_file() -> PathBuf { +pub fn default_log_file() -> PathBuf { cache_dir().join("helix.log") } @@ -227,6 +227,18 @@ pub fn find_workspace() -> (PathBuf, bool) { (current_dir, true) } +fn default_config_file() -> PathBuf { + config_dir().join("config.toml") +} + +fn ensure_parent_dir(path: &Path) { + if let Some(parent) = path.parent() { + if !parent.exists() { + std::fs::create_dir_all(parent).ok(); + } + } +} + #[cfg(test)] mod merge_toml_tests { use std::str; -- cgit v1.2.3-70-g09d2