Skip to content

Commit

Permalink
feat: Use env_logger instead of println
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulrahman1s committed Sep 3, 2022
1 parent 83c6fc4 commit c33c305
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 88 deletions.
122 changes: 73 additions & 49 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ dirs = "4.0.0"
lazy_static = "1.4.0"
notify = "5.0.0"
rust-s3 = { version = "0.32.3", default-features = false, features = ["sync", "tags", "sync-rustls-tls"] }
spinners = "4.1.0"
online = { version = "3.0.1", default-features = false, features = ["sync"] }
online = { version = "3.0.1", default-features = false, features = ["sync"] }
log = "0.4.17"
env_logger = "0.9.0"
1 change: 1 addition & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
provider = "s3"
interval = 600000
path = "/home/abdulrahman/Sync"
log = "info"

[s3]
bucket_name = "sync"
Expand Down
2 changes: 2 additions & 0 deletions src/cloud/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ pub trait CloudAdapter {
fn read_file(path: &Path) -> Result<Vec<u8>> {
Ok(std::fs::read(path)?)
}

fn kind(&self) -> &'static str;
}
4 changes: 4 additions & 0 deletions src/cloud/adapters/s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,8 @@ impl CloudAdapter for Cloud {
self.delete(oldpath)?;
Ok(())
}

fn kind(&self) -> &'static str {
"s3"
}
}
53 changes: 33 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ mod util;

use cloud::{adapters::*, CloudAdapter};
use config::Config;
use log::LevelFilter;
use notify::{event::*, recommended_watcher, RecursiveMode, Watcher};
use std::{
collections::HashSet,
path::PathBuf,
process::exit,
str::FromStr,
sync::{mpsc::channel, Arc, Mutex},
thread,
time::Duration,
Expand All @@ -36,6 +38,16 @@ lazy_static! {
}

fn main() -> Result<()> {
let log_level = SETTINGS
.get_string("main.log")
.unwrap_or_else(|_| "debug".to_owned())
.to_uppercase();

env_logger::builder()
.format_timestamp(None)
.filter_level(LevelFilter::from_str(&log_level).expect("Invalid log level format"))
.init();

let cloud_ref = Arc::new(
match SETTINGS
.get_string("main.provider")
Expand All @@ -52,7 +64,7 @@ fn main() -> Result<()> {
// "onedrive" => onedrive::Cloud::new(),
// "protondrive" => protondrive::Cloud::new(),
x => {
println!("Unspported cloud provider: {}", x);
log::error!("Unsupported cloud provider: {}", x);
exit(1);
}
},
Expand All @@ -62,18 +74,27 @@ fn main() -> Result<()> {
.get("main.interval")
.expect("Missing/Invalid interval value");

let cloud = cloud_ref.clone();
log::info!("Selected cloud provider: {}", cloud_ref.kind());
log::info!("Syncing directory: {}", SYNC_DIR.to_str().unwrap());
log::info!("Syncing delay: {interval:?}ms");

let cloud = cloud_ref.clone();
let online_task = thread::spawn(move || loop {
check_connectivity();

if *IS_INTERNET_AVAILABLE.lock().unwrap() {
*SYNCING.lock().unwrap() = true;
spinner("Syncing...", "Synced!", || cloud.sync().map(|_| {}));

maybe_error(
cloud
.sync()
.map(|count| log::debug!("{count:?} file has synced")),
);

*SYNCING.lock().unwrap() = false;
thread::sleep(Duration::from_millis(interval));
} else {
println!("Skip syncing.. there are no internet connection");
log::warn!("Skip syncing.. there are no internet connection");
}
});

Expand All @@ -89,39 +110,33 @@ fn main() -> Result<()> {
let event = match event {
Ok(e) => e,
Err(err) => {
println!("Notify Error: {:?}", err);
log::error!("Notify Error: {:?}", err);
continue;
}
};

println!("{:?}", event);
log::debug!("{:?}", event);

if !*IS_INTERNET_AVAILABLE.lock().unwrap() {
println!("Skip local syncing.. there are no internet connection");
log::warn!("Skip local syncing.. there are no internet connection");
continue;
}

if *SYNCING.lock().unwrap() {
println!("Ignore event since online syncing is working");
log::debug!("Ignore event since online syncing is working");
continue;
}

match event.kind {
EventKind::Create(CreateKind::File) => {
spinner("Saveing file...", "File saved", || {
cloud.save(&event.paths[0])
});
maybe_error(cloud.save(&event.paths[0]));
}
EventKind::Remove(RemoveKind::File | RemoveKind::Folder) => {
spinner("Deleteing file...", "File deleted", || {
cloud.delete(&event.paths[0])
});
maybe_error(cloud.delete(&event.paths[0]));
}
EventKind::Access(AccessKind::Close(AccessMode::Write)) => {
if changes.remove(&event.paths[0]) {
spinner("Changeing file...", "File changed", || {
cloud.save(&event.paths[0])
});
maybe_error(cloud.save(&event.paths[0]));
}
}
EventKind::Modify(kind) => match kind {
Expand All @@ -130,9 +145,7 @@ fn main() -> Result<()> {
}
ModifyKind::Name(_) => {
if event.paths.len() == 2 {
spinner("Renaming file...", "File renamed", || {
cloud.rename(&event.paths[0], &event.paths[1])
});
maybe_error(cloud.rename(&event.paths[0], &event.paths[1]));
}
}
_ => {}
Expand Down
Loading

0 comments on commit c33c305

Please sign in to comment.