Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helix-term: add config option for specifying log file #566

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions helix-term/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use serde::Deserialize;
use crate::keymap::Keymaps;

#[derive(Debug, Default, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Config {
pub theme: Option<String>,
pub log_file: Option<String>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would call this log-path rather than log file. You can also use Option<PathBuf> here to parse straight into a PathBuf.

#[serde(default)]
pub lsp: LspConfig,
#[serde(default)]
Expand Down
40 changes: 23 additions & 17 deletions helix-term/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,24 @@ fn setup_logging(logpath: PathBuf, verbosity: u64) -> Result<()> {

#[tokio::main]
async fn main() -> Result<()> {
let cache_dir = helix_core::cache_dir();
if !cache_dir.exists() {
std::fs::create_dir_all(&cache_dir).ok();
let conf_dir = helix_core::config_dir();
if !conf_dir.exists() {
std::fs::create_dir_all(&conf_dir).ok();
}

let logpath = cache_dir.join("helix.log");
let config = match std::fs::read_to_string(conf_dir.join("config.toml")) {
Ok(config) => merge_keys(toml::from_str(&config)?),
Err(err) if err.kind() == std::io::ErrorKind::NotFound => Config::default(),
Err(err) => return Err(Error::new(err)),
};

let default_log_path = helix_core::cache_dir().join("helix.log");
let log_path = config
.log_file
.clone()
.map(PathBuf::from)
.unwrap_or(default_log_path);

let help = format!(
"\
{} {}
Expand All @@ -56,14 +68,14 @@ ARGS:
FLAGS:
-h, --help Prints help information
-v Increases logging verbosity each use for up to 3 times
(default file: {})
(file: {})
-V, --version Prints version information
",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION"),
env!("CARGO_PKG_AUTHORS"),
env!("CARGO_PKG_DESCRIPTION"),
logpath.display(),
log_path.display(),
);

let args = Args::parse_args().context("could not parse arguments")?;
Expand All @@ -79,18 +91,12 @@ FLAGS:
std::process::exit(0);
}

let conf_dir = helix_core::config_dir();
if !conf_dir.exists() {
std::fs::create_dir_all(&conf_dir).ok();
}

let config = match std::fs::read_to_string(conf_dir.join("config.toml")) {
Ok(config) => merge_keys(toml::from_str(&config)?),
Err(err) if err.kind() == std::io::ErrorKind::NotFound => Config::default(),
Err(err) => return Err(Error::new(err)),
};
let log_dir = log_path.parent().unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be .context("unable fetch parent path")?


setup_logging(logpath, args.verbosity).context("failed to initialize logging")?;
if !log_dir.exists() {
std::fs::create_dir_all(log_dir).ok();
}
setup_logging(log_path, args.verbosity).context("failed to initialize logging")?;

// TODO: use the thread local executor to spawn the application task separately from the work pool
let mut app = Application::new(args, config).context("unable to create new application")?;
Expand Down