feat(core): improve logging handling and create daemon skeleton
This commit is contained in:
parent
6cf5b51487
commit
0831d19841
43
espanso/src/cli/daemon/mod.rs
Normal file
43
espanso/src/cli/daemon/mod.rs
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
/*
|
||||||
|
* This file is part of espanso.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019-2021 Federico Terzi
|
||||||
|
*
|
||||||
|
* espanso is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* espanso is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use log::info;
|
||||||
|
|
||||||
|
use super::{CliModule, CliModuleArgs};
|
||||||
|
|
||||||
|
pub fn new() -> CliModule {
|
||||||
|
#[allow(clippy::needless_update)]
|
||||||
|
CliModule {
|
||||||
|
requires_paths: true,
|
||||||
|
requires_config: true,
|
||||||
|
enable_logs: true,
|
||||||
|
log_mode: super::LogMode::Write,
|
||||||
|
subcommand: "daemon".to_string(),
|
||||||
|
entry: daemon_main,
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
|
||||||
|
fn daemon_main(args: CliModuleArgs) {
|
||||||
|
let paths = args.paths.expect("missing paths in worker main");
|
||||||
|
|
||||||
|
info!("espanso version: {}", VERSION);
|
||||||
|
}
|
|
@ -21,12 +21,14 @@ use clap::ArgMatches;
|
||||||
use espanso_config::{config::ConfigStore, matches::store::MatchStore};
|
use espanso_config::{config::ConfigStore, matches::store::MatchStore};
|
||||||
use espanso_path::Paths;
|
use espanso_path::Paths;
|
||||||
|
|
||||||
|
pub mod daemon;
|
||||||
pub mod log;
|
pub mod log;
|
||||||
pub mod path;
|
pub mod path;
|
||||||
pub mod worker;
|
pub mod worker;
|
||||||
|
|
||||||
pub struct CliModule {
|
pub struct CliModule {
|
||||||
pub enable_logs: bool,
|
pub enable_logs: bool,
|
||||||
|
pub log_mode: LogMode,
|
||||||
pub requires_paths: bool,
|
pub requires_paths: bool,
|
||||||
pub requires_config: bool,
|
pub requires_config: bool,
|
||||||
pub subcommand: String,
|
pub subcommand: String,
|
||||||
|
@ -37,6 +39,7 @@ impl Default for CliModule {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
enable_logs: false,
|
enable_logs: false,
|
||||||
|
log_mode: LogMode::Read,
|
||||||
requires_paths: false,
|
requires_paths: false,
|
||||||
requires_config: false,
|
requires_config: false,
|
||||||
subcommand: "".to_string(),
|
subcommand: "".to_string(),
|
||||||
|
@ -45,6 +48,13 @@ impl Default for CliModule {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub enum LogMode {
|
||||||
|
Read,
|
||||||
|
Write,
|
||||||
|
Append,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct CliModuleArgs {
|
pub struct CliModuleArgs {
|
||||||
pub config_store: Option<Box<dyn ConfigStore>>,
|
pub config_store: Option<Box<dyn ConfigStore>>,
|
||||||
pub match_store: Option<Box<dyn MatchStore>>,
|
pub match_store: Option<Box<dyn MatchStore>>,
|
||||||
|
|
|
@ -31,6 +31,7 @@ pub fn new() -> CliModule {
|
||||||
requires_paths: true,
|
requires_paths: true,
|
||||||
requires_config: true,
|
requires_config: true,
|
||||||
enable_logs: true,
|
enable_logs: true,
|
||||||
|
log_mode: super::LogMode::Append,
|
||||||
subcommand: "worker".to_string(),
|
subcommand: "worker".to_string(),
|
||||||
entry: worker_main,
|
entry: worker_main,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
|
|
|
@ -31,25 +31,37 @@ use std::{
|
||||||
/// decision of the output file
|
/// decision of the output file
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub(crate) struct FileProxy {
|
pub(crate) struct FileProxy {
|
||||||
output: Arc<Mutex<Option<File>>>,
|
output: Arc<Mutex<Output>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Output {
|
||||||
|
Memory(Vec<u8>),
|
||||||
|
File(File),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FileProxy {
|
impl FileProxy {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
output: Arc::new(Mutex::new(None)),
|
output: Arc::new(Mutex::new(Output::Memory(Vec::new()))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_output_file(&self, path: &Path) -> Result<()> {
|
pub fn set_output_file(&self, path: &Path, truncate: bool, append: bool) -> Result<()> {
|
||||||
let log_file = OpenOptions::new()
|
let mut log_file = OpenOptions::new()
|
||||||
.read(true)
|
.read(true)
|
||||||
.write(true)
|
.write(true)
|
||||||
.create(true)
|
.create(true)
|
||||||
.append(true)
|
.truncate(truncate)
|
||||||
|
.append(append)
|
||||||
.open(path)?;
|
.open(path)?;
|
||||||
let mut lock = self.output.lock().expect("unable to obtain FileProxy lock");
|
let mut lock = self.output.lock().expect("unable to obtain FileProxy lock");
|
||||||
*lock = Some(log_file);
|
|
||||||
|
// Transfer the log content that has been buffered into the file
|
||||||
|
if let Output::Memory(buffered) = &mut (*lock) {
|
||||||
|
log_file.write_all(&buffered)?;
|
||||||
|
buffered.clear();
|
||||||
|
}
|
||||||
|
*lock = Output::File(log_file);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,11 +69,15 @@ impl FileProxy {
|
||||||
impl Write for FileProxy {
|
impl Write for FileProxy {
|
||||||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
||||||
match self.output.lock() {
|
match self.output.lock() {
|
||||||
Ok(lock) => {
|
Ok(mut lock) => {
|
||||||
if let Some(mut output) = lock.as_ref() {
|
match &mut (*lock) {
|
||||||
output.write(buf)
|
// Write to the memory buffer until a file is ready
|
||||||
} else {
|
Output::Memory(buffer) => {
|
||||||
Ok(0)
|
buffer.write(buf)
|
||||||
|
}
|
||||||
|
Output::File(output) => {
|
||||||
|
output.write(buf)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(_) => Err(std::io::Error::new(
|
Err(_) => Err(std::io::Error::new(
|
||||||
|
@ -73,11 +89,14 @@ impl Write for FileProxy {
|
||||||
|
|
||||||
fn flush(&mut self) -> std::io::Result<()> {
|
fn flush(&mut self) -> std::io::Result<()> {
|
||||||
match self.output.lock() {
|
match self.output.lock() {
|
||||||
Ok(lock) => {
|
Ok(mut lock) => {
|
||||||
if let Some(mut output) = lock.as_ref() {
|
match &mut (*lock) {
|
||||||
output.flush()
|
Output::Memory(buffer) => {
|
||||||
} else {
|
buffer.flush()
|
||||||
Ok(())
|
}
|
||||||
|
Output::File(output) => {
|
||||||
|
output.flush()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(_) => Err(std::io::Error::new(
|
Err(_) => Err(std::io::Error::new(
|
||||||
|
|
|
@ -30,6 +30,8 @@ use simplelog::{
|
||||||
CombinedLogger, ConfigBuilder, LevelFilter, TermLogger, TerminalMode, WriteLogger,
|
CombinedLogger, ConfigBuilder, LevelFilter, TermLogger, TerminalMode, WriteLogger,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::cli::LogMode;
|
||||||
|
|
||||||
mod cli;
|
mod cli;
|
||||||
mod engine;
|
mod engine;
|
||||||
mod gui;
|
mod gui;
|
||||||
|
@ -40,8 +42,12 @@ const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
const LOG_FILE_NAME: &str = "espanso.log";
|
const LOG_FILE_NAME: &str = "espanso.log";
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref CLI_HANDLERS: Vec<CliModule> =
|
static ref CLI_HANDLERS: Vec<CliModule> = vec![
|
||||||
vec![cli::path::new(), cli::log::new(), cli::worker::new(),];
|
cli::path::new(),
|
||||||
|
cli::log::new(),
|
||||||
|
cli::worker::new(),
|
||||||
|
cli::daemon::new()
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -128,8 +134,9 @@ fn main() {
|
||||||
// )
|
// )
|
||||||
// .subcommand(SubCommand::with_name("detect")
|
// .subcommand(SubCommand::with_name("detect")
|
||||||
// .about("Tool to detect current window properties, to simplify filters creation."))
|
// .about("Tool to detect current window properties, to simplify filters creation."))
|
||||||
// .subcommand(SubCommand::with_name("daemon")
|
.subcommand(SubCommand::with_name("daemon")
|
||||||
// .about("Start the daemon without spawning a new process."))
|
.setting(AppSettings::Hidden)
|
||||||
|
.about("Start the daemon without spawning a new process."))
|
||||||
// .subcommand(SubCommand::with_name("register")
|
// .subcommand(SubCommand::with_name("register")
|
||||||
// .about("MacOS and Linux only. Register espanso in the system daemon manager."))
|
// .about("MacOS and Linux only. Register espanso in the system daemon manager."))
|
||||||
// .subcommand(SubCommand::with_name("unregister")
|
// .subcommand(SubCommand::with_name("unregister")
|
||||||
|
@ -228,8 +235,7 @@ fn main() {
|
||||||
|
|
||||||
let matches = clap_instance.clone().get_matches();
|
let matches = clap_instance.clone().get_matches();
|
||||||
let log_level = match matches.occurrences_of("v") {
|
let log_level = match matches.occurrences_of("v") {
|
||||||
0 => LevelFilter::Warn,
|
0 | 1 => LevelFilter::Info,
|
||||||
1 => LevelFilter::Info,
|
|
||||||
|
|
||||||
// Trace mode is only available in debug mode for security reasons
|
// Trace mode is only available in debug mode for security reasons
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
|
@ -247,13 +253,14 @@ fn main() {
|
||||||
if handler.enable_logs {
|
if handler.enable_logs {
|
||||||
let config = ConfigBuilder::new()
|
let config = ConfigBuilder::new()
|
||||||
.set_time_to_local(true)
|
.set_time_to_local(true)
|
||||||
|
.set_time_format(format!("%H:%M:%S [{}]", handler.subcommand))
|
||||||
.set_location_level(LevelFilter::Off)
|
.set_location_level(LevelFilter::Off)
|
||||||
.add_filter_ignore_str("html5ever")
|
.add_filter_ignore_str("html5ever")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
CombinedLogger::init(vec![
|
CombinedLogger::init(vec![
|
||||||
TermLogger::new(log_level, config.clone(), TerminalMode::Mixed),
|
TermLogger::new(log_level, config.clone(), TerminalMode::Mixed),
|
||||||
WriteLogger::new(log_level, config, log_proxy.clone()),
|
WriteLogger::new(LevelFilter::Info, config, log_proxy.clone()),
|
||||||
])
|
])
|
||||||
.expect("unable to initialize logs");
|
.expect("unable to initialize logs");
|
||||||
}
|
}
|
||||||
|
@ -265,7 +272,11 @@ fn main() {
|
||||||
let force_package_path = get_path_override(&matches, "package_dir", "ESPANSO_PACKAGE_DIR");
|
let force_package_path = get_path_override(&matches, "package_dir", "ESPANSO_PACKAGE_DIR");
|
||||||
let force_runtime_path = get_path_override(&matches, "runtime_dir", "ESPANSO_RUNTIME_DIR");
|
let force_runtime_path = get_path_override(&matches, "runtime_dir", "ESPANSO_RUNTIME_DIR");
|
||||||
|
|
||||||
let paths = espanso_path::resolve_paths(force_config_path.as_deref(), force_package_path.as_deref(), force_runtime_path.as_deref());
|
let paths = espanso_path::resolve_paths(
|
||||||
|
force_config_path.as_deref(),
|
||||||
|
force_package_path.as_deref(),
|
||||||
|
force_runtime_path.as_deref(),
|
||||||
|
);
|
||||||
|
|
||||||
info!("reading configs from: {:?}", paths.config);
|
info!("reading configs from: {:?}", paths.config);
|
||||||
info!("reading packages from: {:?}", paths.packages);
|
info!("reading packages from: {:?}", paths.packages);
|
||||||
|
@ -291,7 +302,11 @@ fn main() {
|
||||||
|
|
||||||
if handler.enable_logs {
|
if handler.enable_logs {
|
||||||
log_proxy
|
log_proxy
|
||||||
.set_output_file(&paths.runtime.join(LOG_FILE_NAME))
|
.set_output_file(
|
||||||
|
&paths.runtime.join(LOG_FILE_NAME),
|
||||||
|
handler.log_mode == LogMode::Write,
|
||||||
|
handler.log_mode == LogMode::Append,
|
||||||
|
)
|
||||||
.expect("unable to set up log output file");
|
.expect("unable to set up log output file");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -315,7 +330,7 @@ fn get_path_override(matches: &ArgMatches, argument: &str, env_var: &str) -> Opt
|
||||||
if let Some(path) = matches.value_of(argument) {
|
if let Some(path) = matches.value_of(argument) {
|
||||||
let path = PathBuf::from(path.trim());
|
let path = PathBuf::from(path.trim());
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
return Some(path)
|
return Some(path);
|
||||||
} else {
|
} else {
|
||||||
error!("{} argument was specified, but it doesn't point to a valid directory. Make sure to create it first.", argument);
|
error!("{} argument was specified, but it doesn't point to a valid directory. Make sure to create it first.", argument);
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
|
@ -323,7 +338,7 @@ fn get_path_override(matches: &ArgMatches, argument: &str, env_var: &str) -> Opt
|
||||||
} else if let Ok(path) = std::env::var(env_var) {
|
} else if let Ok(path) = std::env::var(env_var) {
|
||||||
let path = PathBuf::from(path.trim());
|
let path = PathBuf::from(path.trim());
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
return Some(path)
|
return Some(path);
|
||||||
} else {
|
} else {
|
||||||
error!("{} env variable was specified, but it doesn't point to a valid directory. Make sure to create it first.", env_var);
|
error!("{} env variable was specified, but it doesn't point to a valid directory. Make sure to create it first.", env_var);
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user