Set logging to output to file as well
This commit is contained in:
@@ -15,3 +15,4 @@ clap = "3.2.16"
|
||||
envconfig = "0.10.0"
|
||||
log = { version = "0.4.17", features = ["kv_unstable", "serde", "std"] }
|
||||
simplelog = "0.12.0"
|
||||
dirs = "4.0.0"
|
||||
|
@@ -5,11 +5,12 @@ use std::{
|
||||
|
||||
pub struct ShellAction {
|
||||
path: String,
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl ShellAction {
|
||||
pub fn new(path: String) -> Self {
|
||||
Self { path }
|
||||
pub fn new(name: String, path: String) -> Self {
|
||||
Self { path, name }
|
||||
}
|
||||
|
||||
pub fn execute(self) -> anyhow::Result<()> {
|
||||
@@ -17,6 +18,7 @@ impl ShellAction {
|
||||
|
||||
log::info!(
|
||||
"
|
||||
|
||||
===
|
||||
Starting running shell action: {}
|
||||
===
|
||||
@@ -35,7 +37,7 @@ Starting running shell action: {}
|
||||
let stdout_reader = BufReader::new(stdout);
|
||||
let mut stdout_lines = stdout_reader.lines();
|
||||
while let Some(Ok(line)) = stdout_lines.next() {
|
||||
log::info!("{}", line)
|
||||
log::info!(process=log::as_display!(self.name); "{}", line)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,6 +45,7 @@ Starting running shell action: {}
|
||||
|
||||
log::info!(
|
||||
"
|
||||
|
||||
===
|
||||
Finished running shell action
|
||||
===
|
||||
|
@@ -24,13 +24,16 @@ impl CuddleAction {
|
||||
pub fn execute(self) {
|
||||
match self.script {
|
||||
CuddleScript::Shell(_s) => {
|
||||
match actions::shell::ShellAction::new(format!(
|
||||
"{}/scripts/{}.sh",
|
||||
self.path
|
||||
.to_str()
|
||||
.expect("action doesn't have a name, this should never happen"),
|
||||
self.name
|
||||
))
|
||||
match actions::shell::ShellAction::new(
|
||||
self.name.clone(),
|
||||
format!(
|
||||
"{}/scripts/{}.sh",
|
||||
self.path
|
||||
.to_str()
|
||||
.expect("action doesn't have a name, this should never happen"),
|
||||
self.name
|
||||
),
|
||||
)
|
||||
.execute()
|
||||
{
|
||||
Ok(()) => {}
|
||||
|
@@ -1,5 +1,7 @@
|
||||
use std::fs::File;
|
||||
|
||||
use config::CuddleConfig;
|
||||
use simplelog::{ColorChoice, Config, TermLogger, TerminalMode};
|
||||
use simplelog::{ColorChoice, CombinedLogger, Config, TermLogger, TerminalMode, WriteLogger};
|
||||
|
||||
mod actions;
|
||||
mod cli;
|
||||
@@ -8,14 +10,7 @@ mod context;
|
||||
mod model;
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
TermLogger::init(
|
||||
log::LevelFilter::Info,
|
||||
Config::default(),
|
||||
TerminalMode::Mixed,
|
||||
ColorChoice::Auto,
|
||||
)?;
|
||||
|
||||
log::set_max_level(log::LevelFilter::Info);
|
||||
init_logging()?;
|
||||
|
||||
let config = CuddleConfig::from_env()?;
|
||||
|
||||
@@ -24,3 +19,24 @@ fn main() -> anyhow::Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn init_logging() -> anyhow::Result<()> {
|
||||
let mut log_file = dirs::state_dir().ok_or(anyhow::anyhow!("could not find state_dir"))?;
|
||||
log_file.push("cuddle_cli.log");
|
||||
|
||||
CombinedLogger::init(vec![
|
||||
TermLogger::new(
|
||||
log::LevelFilter::Info,
|
||||
Config::default(),
|
||||
TerminalMode::Mixed,
|
||||
ColorChoice::Auto,
|
||||
),
|
||||
WriteLogger::new(
|
||||
log::LevelFilter::Debug,
|
||||
Config::default(),
|
||||
File::create(log_file).unwrap(),
|
||||
),
|
||||
])?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user