feat(core): implement log command

This commit is contained in:
Federico Terzi 2021-03-29 21:10:30 +02:00
parent aa6853293a
commit 38fb74cdac
2 changed files with 128 additions and 72 deletions

55
espanso/src/cli/log.rs Normal file
View File

@ -0,0 +1,55 @@
/*
* 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 std::{fs::File, io::BufReader};
use std::io::BufRead;
use super::{CliModule, CliModuleArgs};
pub fn new() -> CliModule {
CliModule {
requires_paths: true,
subcommand: "log".to_string(),
entry: log_main,
..Default::default()
}
}
fn log_main(args: CliModuleArgs) {
let paths = args.paths.expect("missing paths argument");
let log_file = paths.runtime.join(crate::LOG_FILE_NAME);
if !log_file.exists() {
eprintln!("No log file found.");
std::process::exit(2);
}
let log_file = File::open(log_file);
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);
}
}
} else {
eprintln!("Error reading log file");
std::process::exit(1);
}
}

View File

@ -36,6 +36,7 @@ const LOG_FILE_NAME: &str = "espanso.log";
lazy_static! { lazy_static! {
static ref CLI_HANDLERS: Vec<CliModule> = vec![ static ref CLI_HANDLERS: Vec<CliModule> = vec![
cli::path::new(), cli::path::new(),
cli::log::new(),
]; ];
} }
@ -79,46 +80,46 @@ fn main() {
.short("v") .short("v")
.multiple(true) .multiple(true)
.help("Sets the level of verbosity")) .help("Sets the level of verbosity"))
.subcommand(SubCommand::with_name("cmd") // .subcommand(SubCommand::with_name("cmd")
.about("Send a command to the espanso daemon.") // .about("Send a command to the espanso daemon.")
.subcommand(SubCommand::with_name("exit") // .subcommand(SubCommand::with_name("exit")
.about("Terminate the daemon.")) // .about("Terminate the daemon."))
.subcommand(SubCommand::with_name("enable") // .subcommand(SubCommand::with_name("enable")
.about("Enable the espanso replacement engine.")) // .about("Enable the espanso replacement engine."))
.subcommand(SubCommand::with_name("disable") // .subcommand(SubCommand::with_name("disable")
.about("Disable the espanso replacement engine.")) // .about("Disable the espanso replacement engine."))
.subcommand(SubCommand::with_name("toggle") // .subcommand(SubCommand::with_name("toggle")
.about("Toggle the status of the espanso replacement engine.")) // .about("Toggle the status of the espanso replacement engine."))
) // )
.subcommand(SubCommand::with_name("edit") // .subcommand(SubCommand::with_name("edit")
.about("Open the default text editor to edit config files and reload them automatically when exiting") // .about("Open the default text editor to edit config files and reload them automatically when exiting")
.arg(Arg::with_name("config") // .arg(Arg::with_name("config")
.help("Defaults to \"default\". The configuration file name to edit (without the .yml extension).")) // .help("Defaults to \"default\". The configuration file name to edit (without the .yml extension)."))
.arg(Arg::with_name("norestart") // .arg(Arg::with_name("norestart")
.short("n") // .short("n")
.long("norestart") // .long("norestart")
.required(false) // .required(false)
.takes_value(false) // .takes_value(false)
.help("Avoid restarting espanso after editing the file")) // .help("Avoid restarting espanso after editing the file"))
) // )
.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.")) // .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")
.about("MacOS and Linux only. Unregister espanso from the system daemon manager.")) // .about("MacOS and Linux only. Unregister espanso from the system daemon manager."))
.subcommand(SubCommand::with_name("log") .subcommand(SubCommand::with_name("log")
.about("Print the latest daemon logs.")) .about("Print the daemon logs."))
.subcommand(SubCommand::with_name("start") // .subcommand(SubCommand::with_name("start")
.about("Start the daemon spawning a new process in the background.")) // .about("Start the daemon spawning a new process in the background."))
.subcommand(SubCommand::with_name("stop") // .subcommand(SubCommand::with_name("stop")
.about("Stop the espanso daemon.")) // .about("Stop the espanso daemon."))
.subcommand(SubCommand::with_name("restart") // .subcommand(SubCommand::with_name("restart")
.about("Restart the espanso daemon.")) // .about("Restart the espanso daemon."))
.subcommand(SubCommand::with_name("status") // .subcommand(SubCommand::with_name("status")
.about("Check if the espanso daemon is running or not.")) // .about("Check if the espanso daemon is running or not."))
.subcommand(SubCommand::with_name("path") .subcommand(SubCommand::with_name("path")
.about("Prints all the espanso directory paths to easily locate configuration and matches.") .about("Prints all the espanso directory paths to easily locate configuration and matches.")
.subcommand(SubCommand::with_name("config") .subcommand(SubCommand::with_name("config")
@ -135,39 +136,39 @@ fn main() {
.subcommand(SubCommand::with_name("base") .subcommand(SubCommand::with_name("base")
.about("Print the default match file path.")) .about("Print the default match file path."))
) )
.subcommand(SubCommand::with_name("match") // .subcommand(SubCommand::with_name("match")
.about("List and execute matches from the CLI") // .about("List and execute matches from the CLI")
.subcommand(SubCommand::with_name("list") // .subcommand(SubCommand::with_name("list")
.about("Print all matches to standard output") // .about("Print all matches to standard output")
.arg(Arg::with_name("json") // .arg(Arg::with_name("json")
.short("j") // .short("j")
.long("json") // .long("json")
.help("Return the matches as json") // .help("Return the matches as json")
.required(false) // .required(false)
.takes_value(false) // .takes_value(false)
) // )
.arg(Arg::with_name("onlytriggers") // .arg(Arg::with_name("onlytriggers")
.short("t") // .short("t")
.long("onlytriggers") // .long("onlytriggers")
.help("Print only triggers without replacement") // .help("Print only triggers without replacement")
.required(false) // .required(false)
.takes_value(false) // .takes_value(false)
) // )
.arg(Arg::with_name("preservenewlines") // .arg(Arg::with_name("preservenewlines")
.short("n") // .short("n")
.long("preservenewlines") // .long("preservenewlines")
.help("Preserve newlines when printing replacements") // .help("Preserve newlines when printing replacements")
.required(false) // .required(false)
.takes_value(false) // .takes_value(false)
) // )
) // )
.subcommand(SubCommand::with_name("exec") // .subcommand(SubCommand::with_name("exec")
.about("Triggers the expansion of the given match") // .about("Triggers the expansion of the given match")
.arg(Arg::with_name("trigger") // .arg(Arg::with_name("trigger")
.help("The trigger of the match to be expanded") // .help("The trigger of the match to be expanded")
) // )
) // )
) // )
// Package manager // Package manager
.subcommand(SubCommand::with_name("package") .subcommand(SubCommand::with_name("package")
.about("Espanso package manager commands") .about("Espanso package manager commands")