From 5515fbc6d9fecf2b284391cb02caf7ac4df13332 Mon Sep 17 00:00:00 2001 From: funbiscuit Date: Sun, 26 Mar 2023 14:36:54 +0300 Subject: [PATCH] Switch to standard logging Remove chrono from dependencies Show more info in log list --- Cargo.lock | 159 ++++++++++++++++++++------------------------ app/Cargo.toml | 4 +- app/src/app.rs | 33 +++++---- app/src/log_list.rs | 66 ++++++++++++------ app/src/logger.rs | 63 ++++++++++++++++++ app/src/main.rs | 1 + lib/Cargo.toml | 2 +- lib/src/lib.rs | 5 +- lib/src/logger.rs | 47 ------------- lib/src/scanner.rs | 31 +++------ 10 files changed, 216 insertions(+), 195 deletions(-) create mode 100644 app/src/logger.rs delete mode 100644 lib/src/logger.rs diff --git a/Cargo.lock b/Cargo.lock index 2ed8ddd..3fca445 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -117,11 +117,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e3c5919066adf22df73762e50cffcde3a758f2a848b113b586d1f86728b673b" dependencies = [ "iana-time-zone", - "js-sys", "num-integer", "num-traits", - "time", - "wasm-bindgen", "winapi", ] @@ -138,9 +135,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.1.10" +version = "4.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce38afc168d8665cfc75c7b1dd9672e50716a137f433f070991619744a67342a" +checksum = "3c911b090850d79fc64fe9ea01e28e465f65e821e08813ced95bced72f7a8a9b" dependencies = [ "bitflags", "clap_derive", @@ -153,15 +150,14 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.1.9" +version = "4.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fddf67631444a3a3e3e5ac51c36a5e01335302de677bd78759eaa90ab1f46644" +checksum = "9a932373bab67b984c790ddf2c9ca295d8e3af3b7ef92de5a5bacdccdee4b09b" dependencies = [ "heck", - "proc-macro-error", "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.10", ] [[package]] @@ -257,9 +253,9 @@ dependencies = [ [[package]] name = "cxx" -version = "1.0.93" +version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9c00419335c41018365ddf7e4d5f1c12ee3659ddcf3e01974650ba1de73d038" +checksum = "f61f1b6389c3fe1c316bf8a4dccc90a38208354b330925bce1f74a6c4756eb93" dependencies = [ "cc", "cxxbridge-flags", @@ -269,9 +265,9 @@ dependencies = [ [[package]] name = "cxx-build" -version = "1.0.93" +version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb8307ad413a98fff033c8545ecf133e3257747b3bae935e7602aab8aa92d4ca" +checksum = "12cee708e8962df2aeb38f594aae5d827c022b6460ac71a7a3e2c3c2aae5a07b" dependencies = [ "cc", "codespan-reporting", @@ -279,24 +275,24 @@ dependencies = [ "proc-macro2", "quote", "scratch", - "syn 2.0.0", + "syn 2.0.10", ] [[package]] name = "cxxbridge-flags" -version = "1.0.93" +version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "edc52e2eb08915cb12596d29d55f0b5384f00d697a646dbd269b6ecb0fbd9d31" +checksum = "7944172ae7e4068c533afbb984114a56c46e9ccddda550499caa222902c7f7bb" [[package]] name = "cxxbridge-macro" -version = "1.0.93" +version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "631569015d0d8d54e6c241733f944042623ab6df7bc3be7466874b05fcdb1c5f" +checksum = "2345488264226bf682893e25de0769f3360aac9957980ec49361b083ddaa5bc5" dependencies = [ "proc-macro2", "quote", - "syn 2.0.0", + "syn 2.0.10", ] [[package]] @@ -315,12 +311,12 @@ name = "diskscan" version = "0.2.0" dependencies = [ "byte-unit", - "chrono", "crc", "derivative", "fsevent", "inotify", "libproc", + "log", "mountpoints", "nix", "page_size", @@ -413,16 +409,16 @@ checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" [[package]] name = "iana-time-zone" -version = "0.1.53" +version = "0.1.54" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64c122667b287044802d6ce17ee2ddf13207ed924c712de9a66a5814d5b64765" +checksum = "0c17cc76786e99f8d2f055c11159e7f0091c42474dcc3189fbab96072e873e6d" dependencies = [ "android_system_properties", "core-foundation-sys", "iana-time-zone-haiku", "js-sys", "wasm-bindgen", - "winapi", + "windows", ] [[package]] @@ -459,9 +455,9 @@ dependencies = [ [[package]] name = "io-lifetimes" -version = "1.0.8" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0dd6da19f25979c7270e70fa95ab371ec3b701cd0eefc47667a09785b3c59155" +checksum = "09270fd4fa1111bc614ed2246c7ef56239a3063d5be0d1ec3b589c505d400aeb" dependencies = [ "hermit-abi", "libc", @@ -606,7 +602,7 @@ checksum = "5b9d9a46eff5b4ff64b45a9e316a6d1e0bc719ef429cbec4dc630684212bfdf9" dependencies = [ "libc", "log", - "wasi 0.11.0+wasi-snapshot-preview1", + "wasi", "windows-sys 0.45.0", ] @@ -681,9 +677,9 @@ checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" [[package]] name = "os_str_bytes" -version = "6.4.1" +version = "6.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee" +checksum = "ceedf44fb00f2d1984b0bc98102627ce622e083e49a5bacdb3e514fa4238e267" [[package]] name = "page_size" @@ -745,35 +741,11 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" -[[package]] -name = "proc-macro-error" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" -dependencies = [ - "proc-macro-error-attr", - "proc-macro2", - "quote", - "syn 1.0.109", - "version_check", -] - -[[package]] -name = "proc-macro-error-attr" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" -dependencies = [ - "proc-macro2", - "quote", - "version_check", -] - [[package]] name = "proc-macro2" -version = "1.0.52" +version = "1.0.53" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d0e1ae9e836cc3beddd63db0df682593d7e2d3d891ae8c9083d2113e1744224" +checksum = "ba466839c78239c09faf015484e5cc04860f88242cff4d03eb038f04b4699b73" dependencies = [ "unicode-ident", ] @@ -831,18 +803,18 @@ dependencies = [ [[package]] name = "regex" -version = "1.7.1" +version = "1.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733" +checksum = "8b1f693b24f6ac912f4893ef08244d70b6067480d2f1a46e950c9691e6749d1d" dependencies = [ "regex-syntax", ] [[package]] name = "regex-syntax" -version = "0.6.28" +version = "0.6.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" [[package]] name = "rustc-hash" @@ -852,9 +824,9 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" [[package]] name = "rustix" -version = "0.36.10" +version = "0.36.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fe885c3a125aa45213b68cc1472a49880cb5923dc23f522ad2791b882228778" +checksum = "db4165c9963ab29e422d6c26fbc1d37f15bace6b2810221f9d925023480fcf0e" dependencies = [ "bitflags", "errno", @@ -878,22 +850,22 @@ checksum = "1792db035ce95be60c3f8853017b3999209281c24e2ba5bc8e59bf97a0c590c1" [[package]] name = "serde" -version = "1.0.157" +version = "1.0.158" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "707de5fcf5df2b5788fca98dd7eab490bc2fd9b7ef1404defc462833b83f25ca" +checksum = "771d4d9c4163ee138805e12c710dd365e4f44be8be0503cb1bb9eb989425d9c9" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.157" +version = "1.0.158" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78997f4555c22a7971214540c4a661291970619afd56de19f77e0de86296e1e5" +checksum = "e801c1712f48475582b7696ac71e0ca34ebb30e09338425384269d9717c62cad" dependencies = [ "proc-macro2", "quote", - "syn 2.0.0", + "syn 2.0.10", ] [[package]] @@ -954,12 +926,14 @@ version = "0.2.0" dependencies = [ "anyhow", "byte-unit", - "chrono", "clap", "crossterm 0.26.1", "derivative", "diskscan", + "lazy_static", + "log", "num-format", + "time", "tui", "unicode-width", ] @@ -989,9 +963,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.0" +version = "2.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cff13bb1732bccfe3b246f3fdb09edfd51c01d6f5299b7ccd9457c2e4e37774" +checksum = "5aad1363ed6d37b84299588d62d3a7d95b5a5c2d9aad5c85609fda12afaa1f40" dependencies = [ "proc-macro2", "quote", @@ -1024,18 +998,34 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.0", + "syn 2.0.10", ] [[package]] name = "time" -version = "0.1.45" +version = "0.3.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" +checksum = "cd0cbfecb4d19b5ea75bb31ad904eb5b9fa13f21079c3b92017ebdf4999a5890" dependencies = [ - "libc", - "wasi 0.10.0+wasi-snapshot-preview1", - "winapi", + "itoa", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd" + +[[package]] +name = "time-macros" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd80a657e71da814b8e5d60d3374fc6d35045062245d80224748ae522dd76f36" +dependencies = [ + "time-core", ] [[package]] @@ -1089,18 +1079,6 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5190c9442dcdaf0ddd50f37420417d219ae5261bbf5db120d0f9bab996c9cba1" -[[package]] -name = "version_check" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" - -[[package]] -name = "wasi" -version = "0.10.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" - [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -1198,6 +1176,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdacb41e6a96a052c6cb63a144f24900236121c6f63f4f8219fef5977ecb0c25" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-sys" version = "0.42.0" diff --git a/app/Cargo.toml b/app/Cargo.toml index d37a17b..4d79bf7 100644 --- a/app/Cargo.toml +++ b/app/Cargo.toml @@ -17,10 +17,12 @@ diskscan = { path = "../lib", version = "0.2.0" } anyhow = "1.0" byte-unit = { version = "4.0", default-features = false, features = ["std"] } -chrono = "0.4.22" clap = { version = "4.0.18", features = ["derive"] } crossterm = { version = "0.26.1", features = ["serde"] } derivative = "2.2.0" +lazy_static = "1.4.0" +log = "0.4.17" num-format = "0.4.3" +time = { version = "0.3.20", features = ["formatting", "macros"] } tui = { version = "0.19", default-features = false, features = ["crossterm"] } unicode-width = "0.1.10" diff --git a/app/src/app.rs b/app/src/app.rs index eca11db..6d9e14d 100644 --- a/app/src/app.rs +++ b/app/src/app.rs @@ -1,15 +1,15 @@ -use std::sync::Arc; - use derivative::Derivative; +use log::LevelFilter; use diskscan::{ - EntryPath, EntrySnapshot, EntrySnapshotRef, LogEntry, Logger, ScanStats, Scanner, - ScannerBuilder, SnapshotConfig, TreeSnapshot, + EntryPath, EntrySnapshot, EntrySnapshotRef, ScanStats, Scanner, ScannerBuilder, SnapshotConfig, + TreeSnapshot, }; use crate::dialog::{DeleteDialog, Dialog, NewScanDialog, ScanStatsDialog}; use crate::file_list::FileListState; use crate::log_list::LogListState; +use crate::logger::{LogEntry, Logger}; use crate::term::{InputHandler, InputProvider}; #[derive(Clone, Copy, Debug, Eq, PartialEq)] @@ -21,15 +21,15 @@ pub enum Screen { #[derive(Debug)] pub struct LogsApp { - pub logger: Arc, + pub logger: &'static Logger, pub entries: Vec, pub list_state: LogListState, } impl LogsApp { - fn new(logger: Arc) -> Self { + fn new() -> Self { Self { - logger, + logger: Logger::global(), entries: vec![], list_state: LogListState::default(), } @@ -74,14 +74,11 @@ pub struct FilesApp { pub path_history: Vec, pub snapshot: Option>, pub stats: ScanStats, - pub logger: Arc, } impl FilesApp { - pub fn new_scan(path: String, logger: Arc) -> Self { - let scanner = ScannerBuilder::default() - .logger(Arc::clone(&logger)) - .scan(path); + pub fn new_scan(path: String) -> Self { + let scanner = ScannerBuilder::default().scan(path); let file_list_state = FileListState::default(); let current_path = scanner.get_scan_path().clone(); let stats = scanner.stats(); @@ -92,7 +89,6 @@ impl FilesApp { path_history: vec![], snapshot: None, stats, - logger, } } @@ -235,21 +231,22 @@ pub struct App { pub dialog: Option>, pub dialog_menu: Option, pub should_quit: bool, - pub logger: Arc, pub logs_app: LogsApp, } impl App { pub fn new() -> Self { - let logger = Arc::new(Logger::default()); + // register our logger + log::set_logger(Logger::global()).unwrap(); + log::set_max_level(LevelFilter::Info); + App { files: None, screen: Screen::Help, dialog: None, dialog_menu: None, should_quit: false, - logger: Arc::clone(&logger), - logs_app: LogsApp::new(logger), + logs_app: LogsApp::new(), } } @@ -286,7 +283,7 @@ impl App { } pub fn start_scan(&mut self, path: String) { - self.files = Some(FilesApp::new_scan(path, Arc::clone(&self.logger))); + self.files = Some(FilesApp::new_scan(path)); self.screen = Screen::Files; } diff --git a/app/src/log_list.rs b/app/src/log_list.rs index 4f0955e..95e0899 100644 --- a/app/src/log_list.rs +++ b/app/src/log_list.rs @@ -1,10 +1,12 @@ +use log::Level; +use time::format_description::FormatItem; use tui::buffer::Buffer; use tui::layout::Rect; -use tui::style::{Color, Style}; +use tui::style::{Color, Modifier, Style}; +use tui::text::{Span, Spans}; use tui::widgets::{Block, StatefulWidget, Widget}; -use unicode_width::UnicodeWidthStr; -use diskscan::LogEntry; +use crate::logger::LogEntry; #[derive(Debug, Clone, Default)] pub struct LogListState { @@ -46,13 +48,19 @@ impl LogListState { pub struct LogList<'a> { block: Option>, entries: &'a [LogEntry], + time_formatter: &'static [FormatItem<'static>], } impl<'a> LogList<'a> { pub fn new(entries: &'a [LogEntry]) -> LogList<'a> { + let time_formatter = time::macros::format_description!( + "[year]-[month]-[day] [hour]:[minute]:[second].[subsecond digits:3]" + ); + LogList { block: None, entries, + time_formatter, } } @@ -89,6 +97,15 @@ impl<'a> LogList<'a> { (start, end) } + + fn level_color(level: Level) -> Color { + match level { + Level::Error => Color::Red, + Level::Warn => Color::Yellow, + Level::Info => Color::Green, + _ => Color::DarkGray, + } + } } impl<'a> StatefulWidget for LogList<'a> { @@ -142,24 +159,33 @@ impl<'a> StatefulWidget for LogList<'a> { { let (x, y) = (list_area.left(), list_area.top() + i as u16); - let time = format!("{}", item.timestamp.format("%Y-%m-%d %H:%M:%S%.3f")); - let time_width = time.width(); - let max_text_width = list_area.width.saturating_sub(time_width as u16 + 2); - - buf.set_stringn( - x, - y, - &time, - time_width, - Style::default().fg(Color::DarkGray), - ); - buf.set_stringn( - x + time_width as u16 + 1, - y, + let time = item + .timestamp + .format(&self.time_formatter) + .unwrap() + .to_string(); + + let mut spans = vec![ + Span::styled(time, Style::default().fg(Color::DarkGray)), + Span::raw(" "), + Span::styled( + item.level.as_str(), + Style::default() + .fg(LogList::level_color(item.level)) + .add_modifier(Modifier::BOLD), + ), + Span::raw(" "), + ]; + if let Some(module) = &item.module { + spans.push(Span::styled(module, Style::default())); + spans.push(Span::raw(" ")); + } + spans.push(Span::styled( &item.text, - max_text_width as usize, - Style::default(), - ); + Style::default().add_modifier(Modifier::BOLD), + )); + + buf.set_spans(x, y, &Spans::from(spans), list_area.width - 1); if self.entries.len() > list_height && i >= scroll_offset diff --git a/app/src/logger.rs b/app/src/logger.rs new file mode 100644 index 0000000..5e92181 --- /dev/null +++ b/app/src/logger.rs @@ -0,0 +1,63 @@ +use std::sync::Mutex; + +use lazy_static::lazy_static; +use log::{Level, Log, Metadata, Record}; +use time::OffsetDateTime; + +#[derive(Clone, Debug)] +pub struct LogEntry { + pub timestamp: OffsetDateTime, + pub level: Level, + pub module: Option, + pub text: String, +} + +lazy_static! { + static ref LOGGER: Logger = Logger::default(); +} + +#[derive(Debug)] +pub struct Logger { + entries: Mutex>, +} + +impl Logger { + pub fn global() -> &'static Logger { + &LOGGER + } + + /// Return all new entries from last call + /// + /// Entries are sorted from oldest (first) to newest (last) + pub fn read_entries(&self) -> Vec { + std::mem::take(self.entries.lock().unwrap().as_mut()) + } +} + +impl Default for Logger { + fn default() -> Self { + Self { + entries: Mutex::new(vec![]), + } + } +} + +impl Log for Logger { + fn enabled(&self, _: &Metadata) -> bool { + true + } + + fn log(&self, record: &Record) { + //todo use channel instead of locking + self.entries.lock().unwrap().push(LogEntry { + timestamp: OffsetDateTime::now_utc(), + level: record.level(), + module: record.module_path().map(|s| s.to_string()), + text: record.args().to_string(), + }); + } + + fn flush(&self) { + // everything in memory + } +} diff --git a/app/src/main.rs b/app/src/main.rs index 14b96c0..364319f 100644 --- a/app/src/main.rs +++ b/app/src/main.rs @@ -7,6 +7,7 @@ mod app; mod dialog; mod file_list; mod log_list; +mod logger; mod no_ui; mod progressbar; mod term; diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 3650676..aa10b0a 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -14,9 +14,9 @@ edition = "2021" [dependencies] byte-unit = { version = "4.0", default-features = false, features = ["std"] } -chrono = "0.4.22" crc = "3.0.0" derivative = "2.2.0" +log = "0.4.17" mountpoints = "0.2.1" ptree = { version = "0.4.0", default-features = false } diff --git a/lib/src/lib.rs b/lib/src/lib.rs index bf10d44..a75f12c 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -1,7 +1,9 @@ #![warn(rust_2018_idioms, missing_debug_implementations)] +#[macro_use] +extern crate log; + pub use entry_snapshot::{EntrySnapshot, EntrySnapshotRef}; -pub use logger::{LogEntry, Logger}; pub use path::EntryPath; pub use platform::{delete_path, get_available_mounts}; pub use scanner::{ScanStats, Scanner, ScannerBuilder}; @@ -10,7 +12,6 @@ pub use tree_snapshot::{SnapshotConfig, TreeSnapshot}; mod arena; mod entry; mod entry_snapshot; -mod logger; mod path; mod platform; mod scanner; diff --git a/lib/src/logger.rs b/lib/src/logger.rs deleted file mode 100644 index c0486a0..0000000 --- a/lib/src/logger.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::sync::Mutex; - -use chrono::{DateTime, Utc}; - -#[derive(Clone, Debug)] -pub struct LogEntry { - pub timestamp: DateTime, - pub text: String, -} - -impl LogEntry { - fn new(msg: String) -> Self { - Self { - timestamp: Utc::now(), - text: msg, - } - } -} - -#[derive(Debug)] -pub struct Logger { - entries: Mutex>, -} - -impl Logger { - /// Return all new entries from last call - /// - /// Entries are sorted from oldest (first) to newest (last) - pub fn read_entries(&self) -> Vec { - let mut entries = vec![]; - std::mem::swap(self.entries.lock().unwrap().as_mut(), &mut entries); - entries - } - - /// Add given message to log - pub fn log(&self, msg: String) { - self.entries.lock().unwrap().push(LogEntry::new(msg)); - } -} - -impl Default for Logger { - fn default() -> Self { - Self { - entries: Mutex::new(vec![]), - } - } -} diff --git a/lib/src/scanner.rs b/lib/src/scanner.rs index 4fed7af..391a33b 100644 --- a/lib/src/scanner.rs +++ b/lib/src/scanner.rs @@ -10,7 +10,6 @@ use std::time::{Duration, Instant}; use byte_unit::Byte; use crate::entry::DirEntry; -use crate::logger::Logger; use crate::tree::FileTree; use crate::watcher::Watcher; use crate::{platform, EntryPath, EntrySnapshot, SnapshotConfig, TreeSnapshot}; @@ -38,8 +37,6 @@ struct ScanState { scan_flag: AtomicBool, scan_duration_ms: AtomicU32, - - logger: Option>, } #[derive(Debug, Eq, PartialEq)] @@ -49,19 +46,13 @@ struct ScanTask { recursive: bool, } +#[non_exhaustive] #[derive(Debug, Default)] -pub struct ScannerBuilder { - logger: Option>, -} +pub struct ScannerBuilder; impl ScannerBuilder { - pub fn logger(mut self, logger: Arc) -> Self { - self.logger = Some(logger); - self - } - pub fn scan(self, path: String) -> Scanner { - Scanner::new(path, self.logger) + Scanner::new(path) } } @@ -116,6 +107,7 @@ impl Scanner { } pub fn rescan_path(&self, path: EntryPath, reset_stopwatch: bool) { + info!("Start rescan of '{}'", path); self.tx .send(ScanTask { path, @@ -175,7 +167,7 @@ impl Scanner { queue.push(task); } - fn new(path: String, logger: Option>) -> Self { + fn new(path: String) -> Self { let tree = FileTree::new(path.clone()); let root = tree.get_root().get_path(tree.get_arena()); let (tx, rx) = std::sync::mpsc::channel(); @@ -191,7 +183,6 @@ impl Scanner { is_scanning: AtomicBool::new(true), scan_flag: AtomicBool::new(true), scan_duration_ms: AtomicU32::new(0), - logger, }); let scan_handle = Scanner::start_scan(path, Arc::clone(&state), rx); @@ -245,7 +236,8 @@ impl Scanner { .chain(available.into_iter()) .filter(|p| p != &root) .collect(); - let log = |msg| state.logger.as_ref().map(|l| l.log(msg)); + + info!("Start scan of '{}'", root); while state.scan_flag.load(Ordering::SeqCst) { while state.scan_flag.load(Ordering::SeqCst) { @@ -267,7 +259,6 @@ impl Scanner { // add all tasks to queue for task in rx.try_iter() { if task.reset_stopwatch && !state.is_scanning.load(Ordering::SeqCst) { - log("Start scan".to_string()); start = Instant::now(); state.is_scanning.store(true, Ordering::SeqCst); } @@ -293,7 +284,7 @@ impl Scanner { let entries: Vec<_> = std::fs::read_dir(&task.path.get_path()) .and_then(|dir| dir.collect::>()) .unwrap_or_else(|_| { - log(format!("Unable to scan {}", task.path)); + warn!("Unable to scan '{}'", task.path); vec![] }); @@ -320,7 +311,7 @@ impl Scanner { files_size += platform::get_file_size(&metadata) as i64; } } else { - log(format!("Unable to get metadata for {:?}", entry.path())); + warn!("Failed to get metadata for {:?}", entry.path()); } } let new_dirs = { @@ -348,12 +339,12 @@ impl Scanner { state.scan_duration_ms.store(duration, Ordering::SeqCst); if queue.is_empty() { let stats = state.tree.lock().unwrap().stats(); - log(format!( + info!( "Scan finished: {} files {} dirs in {:?}", stats.files, stats.dirs, Duration::from_millis(duration as u64) - )); + ); } } if queue.is_empty() {