Skip to content
Snippets Groups Projects
Commit 00507e3b authored by Éloïs's avatar Éloïs
Browse files

feat: configure log level with env var

parent e15b7782
No related branches found
No related tags found
1 merge request!1392feat: configure log level with env var
......@@ -38,16 +38,18 @@ fn gen_webstart_args(args: &DuniterWebstartArgs, duniter_ts_args: &mut Vec<Strin
}
}
pub(crate) fn gen_duniter_ts_args(args: &DuniterArgs, duniter_js_exe: String) -> Vec<String> {
pub(crate) fn gen_duniter_ts_args(
args: &DuniterArgs,
duniter_js_exe: String,
log_level_filter: log::LevelFilter,
) -> Vec<String> {
let mut duniter_ts_args = vec!["--max-old-space-size=4096".to_owned(), duniter_js_exe];
if let Some(ref home) = args.home {
duniter_ts_args.push("--home".to_owned());
duniter_ts_args.push(home.to_str().expect("invalid home path").to_owned());
}
if let Some(ref log_level) = args.log {
duniter_ts_args.push("--loglevel".to_owned());
duniter_ts_args.push(log_level.to_string().to_lowercase());
}
duniter_ts_args.push("--loglevel".to_owned());
duniter_ts_args.push(log_level_filter.to_string().to_lowercase());
if let Some(ref profile) = args.profile {
duniter_ts_args.push("--mdb".to_owned());
duniter_ts_args.push(profile.clone());
......
......@@ -38,7 +38,7 @@ use logwatcher::{LogWatcher, LogWatcherAction};
use nix::{errno::Errno, sys::signal::Signal, unistd::Pid, Error};
use std::{
fs::File, io::prelude::*, path::Path, path::PathBuf, process::Command, process::Output,
process::Stdio,
process::Stdio, str::FromStr,
};
use structopt::{clap::Shell, StructOpt};
......@@ -214,6 +214,7 @@ fn main() -> Result<()> {
DuniterArgs::clap().gen_completions_to(APP_NAME, shell, &mut std::io::stdout());
Ok(())
} else {
let log_level_filter = get_log_level(args.log)?;
let profile_path = get_profile_path(args.profile.as_deref())?;
#[cfg(feature = "gva")]
......@@ -225,7 +226,8 @@ fn main() -> Result<()> {
let prod = current_exe == PathBuf::from(DUNITER_EXE_LINK_PATH)
|| current_exe == PathBuf::from(DUNITER_EXE_PATH);
let duniter_ts_args = duniter_ts_args::gen_duniter_ts_args(&args, duniter_js_exe()?);
let duniter_ts_args =
duniter_ts_args::gen_duniter_ts_args(&args, duniter_js_exe()?, log_level_filter);
match args.command {
DuniterCommand::Restart => {
......@@ -325,6 +327,15 @@ pub(crate) fn get_node_version(node_path: &str) -> Result<String> {
}
}
fn get_log_level(opt: Option<log::LevelFilter>) -> Result<log::LevelFilter> {
if let Ok(log_level_str) = std::env::var("DUNITER_LOG_LEVEL") {
Ok(log::LevelFilter::from_str(&log_level_str)
.map_err(|e| anyhow::Error::msg(e.to_string()))?)
} else {
Ok(opt.unwrap_or(log::LevelFilter::Info))
}
}
fn get_profile_path(profile: Option<&str>) -> Result<PathBuf> {
let mut profile_path = dirs::config_dir().expect("unsupported operating system");
profile_path.push(APP_NAME);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment