summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2020-12-23 08:49:49 +0000
committerLars Wirzenius <liw@liw.fi>2020-12-23 08:49:49 +0000
commit8f98fb2c4fcee7310985c011cc7f8fe6153c09bb (patch)
tree6c3e5e1410d53d3445a8f8680eae57d2da08292e
parent0f6e2e00ea36eab0b60dce8d36c3f4973a70ba4d (diff)
parentc7ea3dc39b9594cae22405a2683ad823811dd12e (diff)
downloadobnam2-8f98fb2c4fcee7310985c011cc7f8fe6153c09bb.tar.gz
Merge branch 'logging' into 'main'
feat: add log file to client Closes #9 See merge request larswirzenius/obnam!41
-rw-r--r--Cargo.toml1
-rw-r--r--client.yaml2
-rw-r--r--src/bin/obnam.rs44
-rw-r--r--src/client.rs1
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<PathBuf>,
}
impl ClientConfig {