Improve logging. Add log subcommand
This commit is contained in:
parent
b40778cc84
commit
43dc66c25e
18
Cargo.lock
generated
18
Cargo.lock
generated
|
@ -50,7 +50,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
|
||||
[[package]]
|
||||
name = "backtrace"
|
||||
version = "0.3.36"
|
||||
version = "0.3.37"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"backtrace-sys 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
@ -225,12 +225,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
name = "espanso"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"backtrace 0.3.37 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"cmake 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"fs2 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log-panics 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
@ -246,7 +248,7 @@ name = "failure"
|
|||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"backtrace 0.3.36 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"backtrace 0.3.37 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
|
@ -323,6 +325,15 @@ dependencies = [
|
|||
"cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log-panics"
|
||||
version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"backtrace 0.3.37 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.2.1"
|
||||
|
@ -741,7 +752,7 @@ dependencies = [
|
|||
"checksum arrayvec 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)" = "b8d73f9beda665eaa98ab9e4f7442bd4e7de6652587de55b2525e52e29c1b0ba"
|
||||
"checksum atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "1803c647a3ec87095e7ae7acfca019e98de5ec9a7d01343f611cf3152ed71a90"
|
||||
"checksum autocfg 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "b671c8fb71b457dd4ae18c4ba1e59aa81793daacc361d82fcd410cef0d491875"
|
||||
"checksum backtrace 0.3.36 (registry+https://github.com/rust-lang/crates.io-index)" = "e43b13e6ad6177777669f60f642e26307b44676392fae3610676edb54073681d"
|
||||
"checksum backtrace 0.3.37 (registry+https://github.com/rust-lang/crates.io-index)" = "5180c5a20655b14a819b652fd2378fa5f1697b6c9ddad3e695c2f9cedf6df4e2"
|
||||
"checksum backtrace-sys 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)" = "82a830b4ef2d1124a711c71d263c5abdc710ef8e907bd508c88be475cebc422b"
|
||||
"checksum base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e"
|
||||
"checksum bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3d155346769a6855b86399e9bc3814ab343cd3d62c7e985113d46a0ec3c281fd"
|
||||
|
@ -773,6 +784,7 @@ dependencies = [
|
|||
"checksum libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "34fcd2c08d2f832f376f4173a231990fa5aef4e99fb569867318a227ef4c06ba"
|
||||
"checksum linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ae91b68aebc4ddb91978b11a1b02ddd8602a05ec19002801c5666000e05e0f83"
|
||||
"checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
|
||||
"checksum log-panics 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ae0136257df209261daa18d6c16394757c63e032e27aafd8b07788b051082bef"
|
||||
"checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e"
|
||||
"checksum miniz_oxide 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7108aff85b876d06f22503dcce091e29f76733b2bfdd91eebce81f5e68203a10"
|
||||
"checksum nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9667ddcc6cc8a43afc9b7917599d7216aa09c463919ea32c59ed6cac8bc945"
|
||||
|
|
|
@ -17,6 +17,8 @@ simplelog = "0.7.1"
|
|||
zip = "0.5.3"
|
||||
fs2 = "0.4.3"
|
||||
serde_json = "1.0.40"
|
||||
log-panics = {version = "2.0.0", features = ["with-backtrace"]}
|
||||
backtrace = "0.3.37"
|
||||
|
||||
[target.'cfg(unix)'.dependencies]
|
||||
libc = "0.2.62"
|
||||
|
|
59
src/main.rs
59
src/main.rs
|
@ -9,7 +9,7 @@ use std::time::Duration;
|
|||
use clap::{App, Arg, SubCommand, ArgMatches};
|
||||
use fs2::FileExt;
|
||||
use log::{error, info, warn, LevelFilter};
|
||||
use simplelog::{CombinedLogger, SharedLogger, TerminalMode, TermLogger};
|
||||
use simplelog::{CombinedLogger, SharedLogger, TerminalMode, TermLogger, WriteLogger};
|
||||
|
||||
use crate::config::ConfigSet;
|
||||
use crate::config::runtime::RuntimeConfigManager;
|
||||
|
@ -20,6 +20,7 @@ use crate::matcher::scrolling::ScrollingMatcher;
|
|||
use crate::system::SystemManager;
|
||||
use crate::ui::UIManager;
|
||||
use crate::protocol::*;
|
||||
use std::io::{BufReader, BufRead};
|
||||
|
||||
mod ui;
|
||||
mod event;
|
||||
|
@ -34,6 +35,7 @@ mod protocol;
|
|||
mod clipboard;
|
||||
|
||||
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
||||
const LOG_FILE: &str = "espanso.log";
|
||||
|
||||
fn main() {
|
||||
let matches = App::new("espanso")
|
||||
|
@ -67,6 +69,8 @@ fn main() {
|
|||
.about("Tool to detect current window properties, to simplify filters creation."))
|
||||
.subcommand(SubCommand::with_name("daemon")
|
||||
.about("Start the daemon without spawning a new process."))
|
||||
.subcommand(SubCommand::with_name("log")
|
||||
.about("Print the latest daemon logs."))
|
||||
.subcommand(SubCommand::with_name("start")
|
||||
.about("Start the daemon spawning a new process in the background."))
|
||||
.subcommand(SubCommand::with_name("stop")
|
||||
|
@ -122,6 +126,11 @@ fn main() {
|
|||
return;
|
||||
}
|
||||
|
||||
if let Some(_) = matches.subcommand_matches("log") {
|
||||
log_main();
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(_) = matches.subcommand_matches("start") {
|
||||
start_main(config_set);
|
||||
return;
|
||||
|
@ -168,20 +177,35 @@ fn daemon_main(config_set: ConfigSet) {
|
|||
log_outputs.push(terminal_out);
|
||||
}
|
||||
|
||||
//TODO: WriteLogger::new(LevelFilter::Info, Config::default(), File::create("my_rust_binary.log").unwrap()),
|
||||
// Initialize log file output
|
||||
let espanso_dir = context::get_data_dir();
|
||||
let log_file_path = espanso_dir.join(LOG_FILE);
|
||||
let log_file = OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.open(log_file_path)
|
||||
.expect("Cannot create log file.");
|
||||
let file_out = WriteLogger::new(LevelFilter::Info, simplelog::Config::default(), log_file);
|
||||
log_outputs.push(file_out);
|
||||
|
||||
CombinedLogger::init(
|
||||
log_outputs
|
||||
).expect("Error opening log destination");
|
||||
|
||||
// Activate logging for panics
|
||||
log_panics::init();
|
||||
|
||||
info!("starting daemon...");
|
||||
|
||||
let (send_channel, receive_channel) = mpsc::channel();
|
||||
|
||||
let context = context::new(send_channel.clone());
|
||||
|
||||
thread::spawn(move || {
|
||||
thread::Builder::new().name("daemon_background".to_string()).spawn(move || {
|
||||
daemon_background(receive_channel, config_set);
|
||||
});
|
||||
}).expect("Unable to spawn daemon background thread");
|
||||
|
||||
let ipc_server = protocol::get_ipc_server(send_channel.clone());
|
||||
ipc_server.start();
|
||||
|
@ -293,6 +317,7 @@ fn stop_main(config_set: ConfigSet) {
|
|||
}
|
||||
}
|
||||
|
||||
/// Kill the daemon if running and start it again
|
||||
fn restart_main(config_set: ConfigSet) {
|
||||
// Kill the daemon if running
|
||||
let lock_file = acquire_lock();
|
||||
|
@ -346,6 +371,7 @@ fn detect_main() {
|
|||
}
|
||||
}
|
||||
|
||||
/// Send the given command to the espanso daemon
|
||||
fn cmd_main(config_set: ConfigSet, matches: &ArgMatches) {
|
||||
let command = if let Some(_) = matches.subcommand_matches("exit") {
|
||||
Some(IPCCommand {
|
||||
|
@ -389,6 +415,31 @@ fn send_command(config_set: ConfigSet, command: IPCCommand) -> Result<(), String
|
|||
ipc_client.send_command(command)
|
||||
}
|
||||
|
||||
fn log_main() {
|
||||
let espanso_dir = context::get_data_dir();
|
||||
let log_file_path = espanso_dir.join(LOG_FILE);
|
||||
|
||||
if !log_file_path.exists() {
|
||||
println!("No log file found.");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
let log_file = File::open(log_file_path);
|
||||
if let Ok(log_file) = log_file {
|
||||
let reader = BufReader::new(log_file);
|
||||
for line in reader.lines() {
|
||||
if let Ok(line) = line {
|
||||
println!("{}", line);
|
||||
}
|
||||
}
|
||||
|
||||
exit(0);
|
||||
}else{
|
||||
println!("Error reading log file");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
fn acquire_lock() -> Option<File> {
|
||||
let espanso_dir = context::get_data_dir();
|
||||
let lock_file_path = espanso_dir.join("espanso.lock");
|
||||
|
|
|
@ -23,7 +23,7 @@ impl UnixIPCServer {
|
|||
impl super::IPCServer for UnixIPCServer {
|
||||
fn start(&self) {
|
||||
let event_channel = self.event_channel.clone();
|
||||
std::thread::spawn(move || {
|
||||
std::thread::Builder::new().name("ipc_server".to_string()).spawn(move || {
|
||||
let espanso_dir = context::get_data_dir();
|
||||
let unix_socket = espanso_dir.join(UNIX_SOCKET_NAME);
|
||||
|
||||
|
@ -62,7 +62,7 @@ impl super::IPCServer for UnixIPCServer {
|
|||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}).expect("Unable to spawn IPC server thread");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@ impl super::UIManager for WindowsUIManager {
|
|||
|
||||
// Setup a timeout to close the notification
|
||||
let id = Arc::clone(&self.id);
|
||||
thread::spawn(move || {
|
||||
thread::Builder::new().name("notification_thread".to_string()).spawn(move || {
|
||||
for i in 1..10 {
|
||||
let duration = time::Duration::from_millis(200);
|
||||
thread::sleep(duration);
|
||||
|
|
Loading…
Reference in New Issue
Block a user