Set logging to output to file as well

This commit is contained in:
2022-08-10 17:55:56 +02:00
parent 270c138419
commit ce043d670e
6 changed files with 120 additions and 24 deletions

View File

@@ -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"

View File

@@ -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
===

View File

@@ -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(()) => {}

View File

@@ -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(())
}