From c7ea3dc39b9594cae22405a2683ad823811dd12e Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Wed, 23 Dec 2020 10:36:53 +0200 Subject: feat: add log file to client --- Cargo.toml | 1 + client.yaml | 2 +- src/bin/obnam.rs | 44 ++++++++++++++++++++++++++++++++++---------- src/client.rs | 1 + 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 652b727..a8e404b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ bytes = "0.5" indicatif = "0.15" libc = "0.2" log = "0.4" +log4rs = "1" pretty_env_logger = "0.4" reqwest = { version = "0.10", features = ["blocking", "json"]} rusqlite = "0.24" diff --git a/client.yaml b/client.yaml index 1d1cd27..da46287 100644 --- a/client.yaml +++ b/client.yaml @@ -1,3 +1,3 @@ server_url: https://localhost:8888/chunks root: /home/liw/pers/obnam/git/live -dbname: foo.db +log: obnam.log diff --git a/src/bin/obnam.rs b/src/bin/obnam.rs index bc636dc..3ee7f56 100644 --- a/src/bin/obnam.rs +++ b/src/bin/obnam.rs @@ -1,25 +1,36 @@ -use log::{debug, info}; +use log::{debug, error, info, LevelFilter}; +use log4rs::append::file::FileAppender; +use log4rs::config::{Appender, Config, Logger, Root}; use obnam::client::ClientConfig; use obnam::cmd::{backup, list, restore}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use structopt::StructOpt; const BUFFER_SIZE: usize = 1024 * 1024; fn main() -> anyhow::Result<()> { - pretty_env_logger::init(); - let opt = Opt::from_args(); let config = ClientConfig::read_config(&opt.config)?; + if let Some(ref log) = config.log { + setup_logging(&log)?; + } + + info!("client starts"); + debug!("{:?}", opt); - info!("obnam starts"); - debug!("opt: {:?}", opt); + let result = match opt.cmd { + Command::Backup => backup(&config, BUFFER_SIZE), + Command::List => list(&config), + Command::Restore { gen_id, to } => restore(&config, &gen_id, &to), + }; - match opt.cmd { - Command::Backup => backup(&config, BUFFER_SIZE)?, - Command::List => list(&config)?, - Command::Restore { gen_id, to } => restore(&config, &gen_id, &to)?, + if let Err(ref e) = result { + error!("{}", e); + eprintln!("ERROR: {}", e); + return result; } + + info!("client ends successfully"); Ok(()) } @@ -45,3 +56,16 @@ enum Command { to: PathBuf, }, } + +fn setup_logging(filename: &Path) -> anyhow::Result<()> { + let logfile = FileAppender::builder().build(filename)?; + + let config = Config::builder() + .appender(Appender::builder().build("obnam", Box::new(logfile))) + .logger(Logger::builder().build("obnam", LevelFilter::Debug)) + .build(Root::builder().appender("obnam").build(LevelFilter::Debug))?; + + log4rs::init_config(config)?; + + Ok(()) +} diff --git a/src/client.rs b/src/client.rs index 745169b..41b8ae2 100644 --- a/src/client.rs +++ b/src/client.rs @@ -15,6 +15,7 @@ use std::path::{Path, PathBuf}; pub struct ClientConfig { pub server_url: String, pub root: PathBuf, + pub log: Option, } impl ClientConfig { -- cgit v1.2.1