on master help sign in
Fix `:log-open` when `--log` is specified (#7573)
commit1698992de6a601e65a0598ed132cab5419f7e578 authorAlex Vinyals <alevinval@gmail.com> dateSun 9 Jul 2023, 16:35 (3 years ago) parent507dd50
2 files changed+34−34
helix-loader/src/lib.rs+28−16 helix-term/src/main.rs+6−18
helix-loader/src/lib.rs+28−16
@@ -11,21 +11,20 @@ static RUNTIME_DIRS: once_cell::sync::Lazy<Vec<PathBuf>> =
1111
1212static CONFIG_FILE: once_cell::sync::OnceCell<PathBuf> = once_cell::sync::OnceCell::new();
1313
14−pub fn initialize_config_file(specified_file: Option<PathBuf>) {
15− let config_file = specified_file.unwrap_or_else(|| {
16− let config_dir = config_dir();
17−
18− if !config_dir.exists() {
19− std::fs::create_dir_all(&config_dir).ok();
20− }
14+static LOG_FILE: once_cell::sync::OnceCell<PathBuf> = once_cell::sync::OnceCell::new();
2115
22− config_dir.join("config.toml")
23− });
24−
25− // We should only initialize this value once.
16+pub fn initialize_config_file(specified_file: Option<PathBuf>) {
17+ let config_file = specified_file.unwrap_or_else(default_config_file);
18+ ensure_parent_dir(&config_file);
2619 CONFIG_FILE.set(config_file).ok();
2720}
2821
22+pub fn initialize_log_file(specified_file: Option<PathBuf>) {
23+ let log_file = specified_file.unwrap_or_else(default_log_file);
24+ ensure_parent_dir(&log_file);
25+ LOG_FILE.set(log_file).ok();
26+}
27+
2928/// A list of runtime directories from highest to lowest priority
3029///
3130/// The priority is:
@@ -122,10 +121,11 @@ pub fn cache_dir() -> PathBuf {
122121}
123122
124123pub fn config_file() -> PathBuf {
125− CONFIG_FILE
126− .get()
127− .map(|path| path.to_path_buf())
128− .unwrap_or_else(|| config_dir().join("config.toml"))
124+ CONFIG_FILE.get().map(|path| path.to_path_buf()).unwrap()
125+}
126+
127+pub fn log_file() -> PathBuf {
128+ LOG_FILE.get().map(|path| path.to_path_buf()).unwrap()
129129}
130130
131131pub fn workspace_config_file() -> PathBuf {
@@ -136,7 +136,7 @@ pub fn lang_config_file() -> PathBuf {
136136 config_dir().join("languages.toml")
137137}
138138
139−pub fn log_file() -> PathBuf {
139+pub fn default_log_file() -> PathBuf {
140140 cache_dir().join("helix.log")
141141}
142142
@@ -227,6 +227,18 @@ pub fn find_workspace() -> (PathBuf, bool) {
227227 (current_dir, true)
228228}
229229
230+fn default_config_file() -> PathBuf {
231+ config_dir().join("config.toml")
232+}
233+
234+fn ensure_parent_dir(path: &Path) {
235+ if let Some(parent) = path.parent() {
236+ if !parent.exists() {
237+ std::fs::create_dir_all(parent).ok();
238+ }
239+ }
240+}
241+
230242#[cfg(test)]
231243mod merge_toml_tests {
232244 use std::str;
helix-term/src/main.rs+6−18
@@ -4,9 +4,8 @@ use helix_loader::VERSION_AND_GIT_HASH;
44use helix_term::application::Application;
55use helix_term::args::Args;
66use helix_term::config::{Config, ConfigLoadError};
7−use std::path::PathBuf;
87
9−fn setup_logging(logpath: PathBuf, verbosity: u64) -> Result<()> {
8+fn setup_logging(verbosity: u64) -> Result<()> {
109 let mut base_config = fern::Dispatch::new();
1110
1211 base_config = match verbosity {
@@ -27,7 +26,7 @@ fn setup_logging(logpath: PathBuf, verbosity: u64) -> Result<()> {
2726 message
2827 ))
2928 })
30− .chain(fern::log_file(logpath)?);
29+ .chain(fern::log_file(helix_loader::log_file())?);
3130
3231 base_config.chain(file_config).apply()?;
3332
@@ -41,12 +40,6 @@ fn main() -> Result<()> {
4140
4241#[tokio::main]
4342async fn main_impl() -> Result<i32> {
44− let logpath = helix_loader::log_file();
45− let parent = logpath.parent().unwrap();
46− if !parent.exists() {
47− std::fs::create_dir_all(parent).ok();
48− }
49−
5043 let help = format!(
5144 "\
5245{} {}
@@ -78,7 +71,7 @@ FLAGS:
7871 VERSION_AND_GIT_HASH,
7972 env!("CARGO_PKG_AUTHORS"),
8073 env!("CARGO_PKG_DESCRIPTION"),
81− logpath.display(),
74+ helix_loader::default_log_file().display(),
8275 );
8376
8477 let args = Args::parse_args().context("could not parse arguments")?;
@@ -116,15 +109,10 @@ FLAGS:
116109 return Ok(0);
117110 }
118111
119− let logpath = args.log_file.as_ref().cloned().unwrap_or(logpath);
120− setup_logging(logpath, args.verbosity).context("failed to initialize logging")?;
121−
122− let config_dir = helix_loader::config_dir();
123− if !config_dir.exists() {
124− std::fs::create_dir_all(&config_dir).ok();
125− }
126−
127112 helix_loader::initialize_config_file(args.config_file.clone());
113+ helix_loader::initialize_log_file(args.log_file.clone());
114+
115+ setup_logging(args.verbosity).context("failed to initialize logging")?;
128116
129117 let config = match Config::load_default() {
130118 Ok(config) => config,