Refactor log location
This commit is contained in:
parent
d2b812b275
commit
7a680bf0b1
|
@ -8,7 +8,7 @@ use std::io::Read;
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
use crate::event::KeyModifier;
|
use crate::event::KeyModifier;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use log::{error};
|
use log::{error, LevelFilter};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::error::Error;
|
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_title() -> String{ "".to_owned() }
|
||||||
fn default_filter_class() -> String{ "".to_owned() }
|
fn default_filter_class() -> String{ "".to_owned() }
|
||||||
fn default_filter_exec() -> 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_config_caching_interval() -> i32 { 800 }
|
||||||
fn default_toggle_interval() -> u32 { 230 }
|
fn default_toggle_interval() -> u32 { 230 }
|
||||||
fn default_backspace_limit() -> i32 { 3 }
|
fn default_backspace_limit() -> i32 { 3 }
|
||||||
|
@ -44,9 +45,12 @@ pub struct Configs {
|
||||||
#[serde(default = "default_filter_exec")]
|
#[serde(default = "default_filter_exec")]
|
||||||
pub filter_exec: String,
|
pub filter_exec: String,
|
||||||
|
|
||||||
#[serde(default = "default_disable")]
|
#[serde(default = "default_disabled")]
|
||||||
pub disabled: bool,
|
pub disabled: bool,
|
||||||
|
|
||||||
|
#[serde(default = "default_log_level")]
|
||||||
|
pub log_level: i32,
|
||||||
|
|
||||||
#[serde(default = "default_config_caching_interval")]
|
#[serde(default = "default_config_caching_interval")]
|
||||||
pub config_caching_interval: i32,
|
pub config_caching_interval: i32,
|
||||||
|
|
||||||
|
@ -91,6 +95,7 @@ impl Configs {
|
||||||
let mut result = true;
|
let mut result = true;
|
||||||
|
|
||||||
validate_field!(result, self.config_caching_interval, default_config_caching_interval());
|
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_key, KeyModifier::default());
|
||||||
validate_field!(result, self.toggle_interval, default_toggle_interval());
|
validate_field!(result, self.toggle_interval, default_toggle_interval());
|
||||||
validate_field!(result, self.backspace_limit, default_backspace_limit());
|
validate_field!(result, self.backspace_limit, default_backspace_limit());
|
||||||
|
@ -137,8 +142,8 @@ impl Configs {
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
pub struct ConfigSet {
|
pub struct ConfigSet {
|
||||||
default: Configs,
|
pub default: Configs,
|
||||||
specific: Vec<Configs>,
|
pub specific: Vec<Configs>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConfigSet {
|
impl ConfigSet {
|
||||||
|
|
64
src/main.rs
64
src/main.rs
|
@ -60,42 +60,31 @@ fn main() {
|
||||||
.about("Check if the espanso daemon is running or not."))
|
.about("Check if the espanso daemon is running or not."))
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
|
let log_level = matches.occurrences_of("v") as i32;
|
||||||
|
|
||||||
// Setup logging
|
// Load the configuration
|
||||||
let log_level = match matches.occurrences_of("v") {
|
let mut config_set = match matches.value_of("config") {
|
||||||
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") {
|
|
||||||
None => {
|
None => {
|
||||||
info!("loading configuration from default location...");
|
if log_level > 1 {
|
||||||
|
println!("loading configuration from default location...");
|
||||||
|
}
|
||||||
ConfigSet::load_default()
|
ConfigSet::load_default()
|
||||||
},
|
},
|
||||||
Some(path) => {
|
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))
|
ConfigSet::load(Path::new(path))
|
||||||
},
|
},
|
||||||
}.unwrap_or_else(|e| {
|
}.unwrap_or_else(|e| {
|
||||||
error!("{}", e);
|
println!("{}", e);
|
||||||
exit(1);
|
exit(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
config_set.default.log_level = log_level;
|
||||||
|
|
||||||
|
// Match the correct subcommand
|
||||||
|
|
||||||
if let Some(matches) = matches.subcommand_matches("dump") {
|
if let Some(matches) = matches.subcommand_matches("dump") {
|
||||||
println!("{:#?}", config_set);
|
println!("{:#?}", config_set);
|
||||||
return;
|
return;
|
||||||
|
@ -127,10 +116,31 @@ fn daemon_main(config_set: ConfigSet) {
|
||||||
// Try to acquire lock file
|
// Try to acquire lock file
|
||||||
let lock_file = acquire_lock();
|
let lock_file = acquire_lock();
|
||||||
if lock_file.is_none() {
|
if lock_file.is_none() {
|
||||||
error!("espanso is already running.");
|
println!("espanso is already running.");
|
||||||
exit(3);
|
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...");
|
info!("starting daemon...");
|
||||||
|
|
||||||
let (send_channel, receive_channel) = mpsc::channel();
|
let (send_channel, receive_channel) = mpsc::channel();
|
||||||
|
@ -195,7 +205,7 @@ fn start_main(config_set: ConfigSet) {
|
||||||
exit(4);
|
exit(4);
|
||||||
}
|
}
|
||||||
if pid > 0 { // Parent process exit
|
if pid > 0 { // Parent process exit
|
||||||
println!("Daemon started!");
|
println!("daemon started!");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user