Refactor log location

This commit is contained in:
Federico Terzi 2019-09-14 10:30:51 +02:00
parent d2b812b275
commit 7a680bf0b1
2 changed files with 47 additions and 32 deletions

View File

@ -8,7 +8,7 @@ use std::io::Read;
use serde::{Serialize, Deserialize};
use crate::event::KeyModifier;
use std::collections::HashSet;
use log::{error};
use log::{error, LevelFilter};
use std::fmt;
use std::error::Error;
@ -24,7 +24,8 @@ fn default_name() -> String{ "default".to_owned() }
fn default_filter_title() -> String{ "".to_owned() }
fn default_filter_class() -> String{ "".to_owned() }
fn default_filter_exec() -> String{ "".to_owned() }
fn default_disable() -> bool{ false }
fn default_disabled() -> bool{ false }
fn default_log_level() -> i32 { 0 }
fn default_config_caching_interval() -> i32 { 800 }
fn default_toggle_interval() -> u32 { 230 }
fn default_backspace_limit() -> i32 { 3 }
@ -44,9 +45,12 @@ pub struct Configs {
#[serde(default = "default_filter_exec")]
pub filter_exec: String,
#[serde(default = "default_disable")]
#[serde(default = "default_disabled")]
pub disabled: bool,
#[serde(default = "default_log_level")]
pub log_level: i32,
#[serde(default = "default_config_caching_interval")]
pub config_caching_interval: i32,
@ -91,6 +95,7 @@ impl Configs {
let mut result = true;
validate_field!(result, self.config_caching_interval, default_config_caching_interval());
validate_field!(result, self.log_level, default_log_level());
validate_field!(result, self.toggle_key, KeyModifier::default());
validate_field!(result, self.toggle_interval, default_toggle_interval());
validate_field!(result, self.backspace_limit, default_backspace_limit());
@ -137,8 +142,8 @@ impl Configs {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ConfigSet {
default: Configs,
specific: Vec<Configs>,
pub default: Configs,
pub specific: Vec<Configs>,
}
impl ConfigSet {

View File

@ -60,42 +60,31 @@ fn main() {
.about("Check if the espanso daemon is running or not."))
.get_matches();
let log_level = matches.occurrences_of("v") as i32;
// Setup logging
let log_level = match matches.occurrences_of("v") {
0 => LevelFilter::Warn,
1 => LevelFilter::Info,
2 | _ => LevelFilter::Debug,
};
let mut log_outputs: Vec<Box<dyn SharedLogger>> = Vec::new();
// Initialize terminal output
let terminal_out = TermLogger::new(log_level, simplelog::Config::default(), TerminalMode::Mixed);
if let Some(terminal_out) = terminal_out {
log_outputs.push(terminal_out);
}
//TODO: WriteLogger::new(LevelFilter::Info, Config::default(), File::create("my_rust_binary.log").unwrap()),
CombinedLogger::init(
log_outputs
).expect("Error opening log destination");
info!("espanso is starting...");
let config_set = match matches.value_of("config") {
// Load the configuration
let mut config_set = match matches.value_of("config") {
None => {
info!("loading configuration from default location...");
if log_level > 1 {
println!("loading configuration from default location...");
}
ConfigSet::load_default()
},
Some(path) => {
info!("loading configuration from custom location: {}", path);
if log_level > 1 {
println!("loading configuration from custom location: {}", path);
}
ConfigSet::load(Path::new(path))
},
}.unwrap_or_else(|e| {
error!("{}", e);
println!("{}", e);
exit(1);
});
config_set.default.log_level = log_level;
// Match the correct subcommand
if let Some(matches) = matches.subcommand_matches("dump") {
println!("{:#?}", config_set);
return;
@ -127,10 +116,31 @@ fn daemon_main(config_set: ConfigSet) {
// Try to acquire lock file
let lock_file = acquire_lock();
if lock_file.is_none() {
error!("espanso is already running.");
println!("espanso is already running.");
exit(3);
}
// Initialize log
let log_level = match config_set.default.log_level {
0 => LevelFilter::Warn,
1 => LevelFilter::Info,
2 | _ => LevelFilter::Debug,
};
let mut log_outputs: Vec<Box<dyn SharedLogger>> = Vec::new();
// Initialize terminal output
let terminal_out = TermLogger::new(log_level,
simplelog::Config::default(), TerminalMode::Mixed);
if let Some(terminal_out) = terminal_out {
log_outputs.push(terminal_out);
}
//TODO: WriteLogger::new(LevelFilter::Info, Config::default(), File::create("my_rust_binary.log").unwrap()),
CombinedLogger::init(
log_outputs
).expect("Error opening log destination");
info!("starting daemon...");
let (send_channel, receive_channel) = mpsc::channel();
@ -195,7 +205,7 @@ fn start_main(config_set: ConfigSet) {
exit(4);
}
if pid > 0 { // Parent process exit
println!("Daemon started!");
println!("daemon started!");
exit(0);
}