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 --- src/bin/obnam.rs | 44 ++++++++++++++++++++++++++++++++++---------- src/client.rs | 1 + 2 files changed, 35 insertions(+), 10 deletions(-) (limited to 'src') 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