From 8d2e010849b64fc2d76c15b26c73a31786384481 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Thu, 3 Jun 2021 00:19:56 +0800 Subject: [PATCH] Default log file to cache --- helix-core/src/lib.rs | 11 ++++++++++- helix-term/src/main.rs | 21 +++++++++++++-------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/helix-core/src/lib.rs b/helix-core/src/lib.rs index d5b0cd1566740..3e00081f50ad7 100644 --- a/helix-core/src/lib.rs +++ b/helix-core/src/lib.rs @@ -58,13 +58,22 @@ pub fn runtime_dir() -> std::path::PathBuf { pub fn config_dir() -> std::path::PathBuf { // TODO: allow env var override - use etcetera::base_strategy::{choose_base_strategy, BaseStrategy}; let strategy = choose_base_strategy().expect("Unable to find the config directory!"); let mut path = strategy.config_dir(); path.push("helix"); path } +pub fn cache_dir() -> std::path::PathBuf { + // TODO: allow env var override + let strategy = choose_base_strategy().expect("Unable to find the config directory!"); + let mut path = strategy.cache_dir(); + path.push("helix"); + path +} + +use etcetera::base_strategy::{choose_base_strategy, BaseStrategy}; + pub use ropey::{Rope, RopeSlice}; pub use tendril::StrTendril as Tendril; diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs index 3a0f4d204ff4f..44a3c4d020be5 100644 --- a/helix-term/src/main.rs +++ b/helix-term/src/main.rs @@ -8,13 +8,11 @@ mod ui; use application::Application; -use helix_core::config_dir; - use std::path::PathBuf; use anyhow::{Context, Result}; -fn setup_logging(verbosity: u64) -> Result<()> { +fn setup_logging(logpath: PathBuf, verbosity: u64) -> Result<()> { let mut base_config = fern::Dispatch::new(); // Let's say we depend on something which whose "info" level messages are too @@ -40,7 +38,7 @@ fn setup_logging(verbosity: u64) -> Result<()> { message )) }) - .chain(fern::log_file(config_dir().join("helix.log"))?); + .chain(fern::log_file(logpath)?); base_config.chain(file_config).apply()?; @@ -52,6 +50,12 @@ pub struct Args { } fn main() -> Result<()> { + let cache_dir = helix_core::cache_dir(); + if !cache_dir.exists() { + std::fs::create_dir(&cache_dir); + } + + let logpath = cache_dir.join("helix.log"); let help = format!( "\ {} {} @@ -67,12 +71,14 @@ ARGS: FLAGS: -h, --help Prints help information -v Increases logging verbosity each use for up to 3 times + (default file: {}) -V, --version Prints version information ", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"), env!("CARGO_PKG_AUTHORS"), env!("CARGO_PKG_DESCRIPTION"), + logpath.display(), ); let mut pargs = pico_args::Arguments::from_env(); @@ -89,13 +95,12 @@ FLAGS: verbosity = 1; } - let conf_dir = config_dir(); - + let conf_dir = helix_core::config_dir(); if !conf_dir.exists() { std::fs::create_dir(&conf_dir); } - setup_logging(verbosity).context("failed to initialize logging")?; + setup_logging(logpath, verbosity).context("failed to initialize logging")?; let args = Args { files: pargs.finish().into_iter().map(|arg| arg.into()).collect(), @@ -105,7 +110,7 @@ FLAGS: use helix_core::syntax::{Loader, LOADER}; // load $HOME/.config/helix/languages.toml, fallback to default config - let config = std::fs::read(config_dir().join("languages.toml")); + let config = std::fs::read(helix_core::config_dir().join("languages.toml")); let toml = config .as_deref() .unwrap_or(include_bytes!("../../languages.toml"));