diff options
author | Lars Wirzenius <liw@liw.fi> | 2020-12-23 10:36:53 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2020-12-23 10:47:09 +0200 |
commit | c7ea3dc39b9594cae22405a2683ad823811dd12e (patch) | |
tree | 6c3e5e1410d53d3445a8f8680eae57d2da08292e /src | |
parent | 0f6e2e00ea36eab0b60dce8d36c3f4973a70ba4d (diff) | |
download | obnam2-c7ea3dc39b9594cae22405a2683ad823811dd12e.tar.gz |
feat: add log file to client
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/obnam.rs | 44 | ||||
-rw-r--r-- | src/client.rs | 1 |
2 files changed, 35 insertions, 10 deletions
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<PathBuf>, } impl ClientConfig { |