Set logging to output to file as well

This commit is contained in:
Kasper Juul Hermansen 2022-08-10 17:55:56 +02:00
parent 270c138419
commit ce043d670e
Signed by: kjuulh
GPG Key ID: 0F95C140730F2F23
6 changed files with 120 additions and 24 deletions

78
Cargo.lock generated
View File

@ -90,6 +90,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"clap",
"dirs",
"envconfig",
"git2",
"log",
@ -99,6 +100,26 @@ dependencies = [
"walkdir",
]
[[package]]
name = "dirs"
version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059"
dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs-sys"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6"
dependencies = [
"libc",
"redox_users",
"winapi",
]
[[package]]
name = "envconfig"
version = "0.10.0"
@ -129,6 +150,17 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "getrandom"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "git2"
version = "0.15.0"
@ -322,6 +354,26 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "redox_syscall"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a"
dependencies = [
"bitflags",
]
[[package]]
name = "redox_users"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b"
dependencies = [
"getrandom",
"redox_syscall",
"thiserror",
]
[[package]]
name = "ryu"
version = "1.0.11"
@ -413,6 +465,26 @@ version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
[[package]]
name = "thiserror"
version = "1.0.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f5f6586b7f764adc0231f4c79be7b920e766bb2f3e51b3661cdb263828f19994"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12bafc5b54507e0149cdf1b145a5d80ab80a90bcd9275df43d4fff68460f6c21"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "time"
version = "0.3.13"
@ -518,6 +590,12 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "winapi"
version = "0.3.9"

5
\
View File

@ -1,5 +0,0 @@
#[derive(Debug)]
pub struct CuddleContext {
pub plan: CuddlePlan,
pub path: PathBuf,
}

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