Merge 7a73ae7cc0 into release

This commit is contained in:
github-actions[bot] 2021-01-05 03:55:56 +00:00 committed by GitHub
commit b9f7e27de8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 24 deletions

View file

@ -8,9 +8,9 @@ extern crate diesel_migrations;
use anyhow::*;
use log::info;
use simplelog::{CombinedLogger, LevelFilter, TermLogger, TerminalMode, WriteLogger};
use simplelog::{CombinedLogger, LevelFilter, SharedLogger, TermLogger, TerminalMode, WriteLogger};
use std::fs;
use std::path::PathBuf;
use std::path::Path;
mod app;
mod db;
@ -23,15 +23,15 @@ mod ui;
mod utils;
#[cfg(unix)]
fn daemonize(foreground: bool, pid_file_path: &PathBuf) -> Result<()> {
fn daemonize<T: AsRef<Path>>(foreground: bool, pid_file_path: T) -> Result<()> {
if foreground {
return Ok(());
}
if let Some(parent) = pid_file_path.parent() {
if let Some(parent) = pid_file_path.as_ref().parent() {
fs::create_dir_all(parent)?;
}
let daemonize = daemonize::Daemonize::new()
.pid_file(pid_file_path)
.pid_file(pid_file_path.as_ref())
.working_directory(".");
daemonize.start()?;
Ok(())
@ -45,23 +45,29 @@ fn notify_ready() -> Result<()> {
Ok(())
}
fn init_logging(log_level: LevelFilter, log_file_path: &PathBuf) -> Result<()> {
fn init_logging<T: AsRef<Path>>(log_level: LevelFilter, log_file_path: &Option<T>) -> Result<()> {
let log_config = simplelog::ConfigBuilder::new()
.set_location_level(LevelFilter::Error)
.build();
if let Some(parent) = log_file_path.parent() {
fs::create_dir_all(parent)?;
}
let mut loggers: Vec<Box<dyn SharedLogger>> = vec![TermLogger::new(
log_level,
log_config.clone(),
TerminalMode::Mixed,
)];
CombinedLogger::init(vec![
TermLogger::new(log_level, log_config.clone(), TerminalMode::Mixed),
WriteLogger::new(
if let Some(path) = log_file_path {
if let Some(parent) = path.as_ref().parent() {
fs::create_dir_all(parent)?;
}
loggers.push(WriteLogger::new(
log_level,
log_config.clone(),
fs::File::create(log_file_path)?,
),
])?;
fs::File::create(path)?,
));
}
CombinedLogger::init(loggers)?;
Ok(())
}

View file

@ -4,7 +4,6 @@ use std::path::PathBuf;
pub struct CLIOptions {
pub show_help: bool,
#[cfg(unix)]
pub foreground: bool,
pub log_file_path: Option<PathBuf>,
#[cfg(unix)]
@ -36,6 +35,8 @@ impl Manager {
show_help: matches.opt_present("h"),
#[cfg(unix)]
foreground: matches.opt_present("f"),
#[cfg(windows)]
foreground: !cfg!(feature = "ui"),
log_file_path: matches.opt_str("log").map(PathBuf::from),
#[cfg(unix)]
pid_file_path: matches.opt_str("pid").map(PathBuf::from),

View file

@ -6,7 +6,7 @@ pub struct Paths {
pub cache_dir_path: PathBuf,
pub config_file_path: Option<PathBuf>,
pub db_file_path: PathBuf,
pub log_file_path: PathBuf,
pub log_file_path: Option<PathBuf>,
#[cfg(unix)]
pub pid_file_path: PathBuf,
pub swagger_dir_path: PathBuf,
@ -22,7 +22,7 @@ impl Default for Paths {
cache_dir_path: ["."].iter().collect(),
config_file_path: None,
db_file_path: [".", "db.sqlite"].iter().collect(),
log_file_path: [".", "polaris.log"].iter().collect(),
log_file_path: Some([".", "polaris.log"].iter().collect()),
pid_file_path: [".", "polaris.pid"].iter().collect(),
swagger_dir_path: [".", "docs", "swagger"].iter().collect(),
web_dir_path: [".", "web"].iter().collect(),
@ -40,7 +40,7 @@ impl Default for Paths {
cache_dir_path: install_directory.clone(),
config_file_path: None,
db_file_path: install_directory.join("db.sqlite"),
log_file_path: install_directory.join("polaris.log"),
log_file_path: Some(install_directory.join("polaris.log")),
swagger_dir_path: install_directory.join("swagger"),
web_dir_path: install_directory.join("web"),
}
@ -62,7 +62,7 @@ impl Paths {
log_file_path: option_env!("POLARIS_LOG_DIR")
.map(PathBuf::from)
.map(|p| p.join("polaris.log"))
.unwrap_or(defaults.log_file_path),
.or(defaults.log_file_path),
#[cfg(unix)]
pid_file_path: option_env!("POLARIS_PID_DIR")
.map(PathBuf::from)
@ -88,9 +88,6 @@ impl Paths {
if let Some(path) = &cli_options.database_file_path {
paths.db_file_path = path.clone();
}
if let Some(path) = &cli_options.log_file_path {
paths.log_file_path = path.clone();
}
#[cfg(unix)]
if let Some(path) = &cli_options.pid_file_path {
paths.pid_file_path = path.clone();
@ -101,6 +98,14 @@ impl Paths {
if let Some(path) = &cli_options.web_dir_path {
paths.web_dir_path = path.clone();
}
let log_to_file = cli_options.log_file_path.is_some() || !cli_options.foreground;
if log_to_file {
paths.log_file_path = cli_options.log_file_path.clone().or(paths.log_file_path);
} else {
paths.log_file_path = None;
};
return paths;
}
}

View file

@ -85,7 +85,7 @@ impl TestService for ActixTestService {
db_file_path: output_dir.join("db.sqlite"),
#[cfg(unix)]
pid_file_path: output_dir.join("polaris.pid"),
log_file_path: output_dir.join("polaris.log"),
log_file_path: None,
swagger_dir_path: ["docs", "swagger"].iter().collect(),
web_dir_path: ["test-data", "web"].iter().collect(),
};