From 2d6cfe0b728fa143924270117c026ff4e0c23e6e Mon Sep 17 00:00:00 2001 From: Richard Hao Date: Tue, 10 Oct 2023 07:59:00 -0500 Subject: [PATCH] feat: beauty the log (#32) * feat: beauty the log * docs: resolve the log --- Cargo.lock | 136 ++++++++++++++++++++++++++++++++++++++++++------ Cargo.toml | 2 + docs/TODO.md | 3 +- src/config.rs | 8 +-- src/main.rs | 21 ++++++-- src/uploader.rs | 5 +- 6 files changed, 148 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a659a10..87880bf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -104,7 +104,7 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" dependencies = [ - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -114,7 +114,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0238ca56c96dfa37bdf7c373c8886dd591322500aceeeccdb2216fe06dc2f796" dependencies = [ "anstyle", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -776,6 +776,17 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +[[package]] +name = "colored" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2674ec482fbc38012cf31e6c42ba0177b431a0cb6f15fe40efa5aab1bda516f6" +dependencies = [ + "is-terminal", + "lazy_static", + "windows-sys 0.48.0", +] + [[package]] name = "const-oid" version = "0.9.5" @@ -1063,7 +1074,7 @@ checksum = "136526188508e25c6fef639d7927dfb3e0e3084488bf202267829cf7fc23dbdd" dependencies = [ "errno-dragonfly", "libc", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -1447,6 +1458,17 @@ dependencies = [ "generic-array", ] +[[package]] +name = "is-terminal" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" +dependencies = [ + "hermit-abi", + "rustix", + "windows-sys 0.48.0", +] + [[package]] name = "itoa" version = "1.0.9" @@ -1562,7 +1584,7 @@ checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2" dependencies = [ "libc", "wasi", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -1645,6 +1667,15 @@ dependencies = [ "libc", ] +[[package]] +name = "num_threads" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44" +dependencies = [ + "libc", +] + [[package]] name = "object" version = "0.32.1" @@ -2047,7 +2078,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -2111,7 +2142,7 @@ version = "0.1.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c3733bf4cf7ea0880754e19cb5a462007c4a8c1914bff372ccc95b464f1df88" dependencies = [ - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -2244,6 +2275,18 @@ dependencies = [ "rand_core", ] +[[package]] +name = "simple_logger" +version = "4.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2230cd5c29b815c9b699fb610b49a5ed65588f3509d9f0108be3a885da629333" +dependencies = [ + "colored", + "log", + "time", + "windows-sys 0.42.0", +] + [[package]] name = "slab" version = "0.4.9" @@ -2276,7 +2319,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4031e820eb552adee9295814c0ced9e5cf38ddf1e8b7d566d6de8e2538ea989e" dependencies = [ "libc", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -2305,9 +2348,11 @@ dependencies = [ "aws-sdk-s3", "clap", "dotenvy", + "log", "pgp", "rand", "rusqlite", + "simple_logger", "tempfile", "time", "tokio", @@ -2358,7 +2403,7 @@ dependencies = [ "fastrand", "redox_syscall", "rustix", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -2389,6 +2434,8 @@ checksum = "426f806f4089c493dcac0d24c29c01e2c38baf8e30f1b716ee37e83d200b18fe" dependencies = [ "deranged", "itoa", + "libc", + "num_threads", "serde", "time-core", "time-macros", @@ -2440,7 +2487,7 @@ dependencies = [ "signal-hook-registry", "socket2 0.5.4", "tokio-macros", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -2758,6 +2805,21 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "windows-sys" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" +dependencies = [ + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", +] + [[package]] name = "windows-sys" version = "0.48.0" @@ -2773,51 +2835,93 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" + [[package]] name = "windows_aarch64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" +[[package]] +name = "windows_i686_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" + [[package]] name = "windows_i686_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" +[[package]] +name = "windows_i686_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" + [[package]] name = "windows_i686_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" + [[package]] name = "windows_x86_64_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" + [[package]] name = "windows_x86_64_gnullvm" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" + [[package]] name = "windows_x86_64_msvc" version = "0.48.5" diff --git a/Cargo.toml b/Cargo.toml index 2f3353e..04dfa6c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,9 +12,11 @@ aws-config = "0.56.1" aws-sdk-s3 = "0.33.0" clap = { version = "4.4.6", features = ["derive"] } dotenvy = "0.15.7" +log = "0.4.20" pgp = "0.10.2" rand = "0.8.5" rusqlite = { version = "0.29.0", features = ["backup"] } +simple_logger = "4.2.0" tempfile = "3.8.0" time = { version = "0.3.29", features = ["formatting"] } tokio = { version = "1.33.0", features = ["full"] } diff --git a/docs/TODO.md b/docs/TODO.md index 1bdcbe7..6bdfddb 100644 --- a/docs/TODO.md +++ b/docs/TODO.md @@ -11,5 +11,4 @@ - [x] Add GPG encryption support - encrypt: `add env variable GPG_PASSPHRASE` - decrypt: `gpg -o backup.tar.gz -d backup.tar.gz.gpg` -- [ ] Beauty the output log -- [ ] Restore SQLite backup (low priority, because we could run `sqlite_backup` backward to restore the data) +- [x] Beauty the output log diff --git a/src/config.rs b/src/config.rs index 529e451..d250fbb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,7 +1,7 @@ use dotenvy::{dotenv, from_filename_override}; use std::{env, ffi::OsString, fmt::Display}; -use anyhow::Result; +use anyhow::{Context, Result}; #[derive(PartialEq, Debug)] pub enum AppEnv { @@ -44,16 +44,16 @@ impl Config { _ => AppEnv::Dev, }; - println!("Running in {app_env} mode"); + log::info!("Running in {app_env} mode"); match app_env { AppEnv::Dev => { // load environment variables from .env file - dotenv().expect(".env file not found"); + dotenv().context(".env file not found")?; } AppEnv::Test => { - from_filename_override(".env.test").expect(".env.test file not found"); + from_filename_override(".env.test").context(".env.test file not found")?; } _ => { diff --git a/src/main.rs b/src/main.rs index 99d87db..de099a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,12 +8,24 @@ use sqlite_backup::{ }; #[tokio::main] -async fn main() -> Result<()> { +async fn main() { + simple_logger::init_with_level(log::Level::Info).unwrap(); + + log::info!("================================================================================"); + if let Err(err) = execute().await { + log::error!("{:?}", err) + } + log::info!("================================================================================"); +} + +async fn execute() -> Result<()> { + log::info!("Starting backup..."); + let cfg = Config::load().context("load env vars")?; let args = Argument::parse(); run(&args, &cfg).await?; - println!("Done"); + log::info!("Ending backup..."); Ok(()) } @@ -30,9 +42,10 @@ async fn run(arg: &argument::Argument, cfg: &Config) -> Result<()> { src_file.path.display().to_string(), dest.clone(), |p| { - println!( + log::info!( "---Progress---- pagecount: {}, remaining: {}", - p.pagecount, p.remaining + p.pagecount, + p.remaining ) }, ) diff --git a/src/uploader.rs b/src/uploader.rs index 5fc4c13..39778cd 100644 --- a/src/uploader.rs +++ b/src/uploader.rs @@ -85,11 +85,12 @@ impl Uploader for R2Uploader { self.client .put_object() .bucket(self.bucket.clone()) - .key(object_key) + .key(&object_key) .body(body) .send() .await .context("upload object to r2")?; + log::info!("upload object to r2 success {}", object_key); Ok(()) } @@ -126,6 +127,8 @@ impl Uploader for R2Uploader { }) .collect::>(); self.delete_objects(deleted_objects.clone()).await?; + + log::info!("deleted {} objects from r2", deleted_objects.len()); } Ok(())