diff --git a/Cargo.lock b/Cargo.lock index 635d0b5..550e4b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -113,7 +113,7 @@ dependencies = [ "rand", "serde", "serde_json", - "simple_logger", + "simplelog", "tungstenite", ] @@ -169,17 +169,6 @@ dependencies = [ "toml", ] -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi", - "libc", - "winapi", -] - [[package]] name = "autocfg" version = "1.0.0" @@ -258,6 +247,17 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +[[package]] +name = "chrono" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80094f509cf8b5ae86a4966a39b3ff66cd7e2a3e594accec3743ff3fabeab5b2" +dependencies = [ + "num-integer", + "num-traits", + "time", +] + [[package]] name = "cli_helpers" version = "0.1.0" @@ -520,15 +520,6 @@ dependencies = [ "serde", ] -[[package]] -name = "hermit-abi" -version = "0.1.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91780f809e750b0a89f5544be56617ff6b1227ee485bcb06ebe10cdf89bd3b71" -dependencies = [ - "libc", -] - [[package]] name = "histogram" version = "0.6.9" @@ -1209,14 +1200,14 @@ dependencies = [ ] [[package]] -name = "simple_logger" -version = "1.6.0" +name = "simplelog" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fea0c4611f32f4c2bac73754f22dca1f57e6c1945e0590dae4e5f2a077b92367" +checksum = "2b2736f58087298a448859961d3f4a0850b832e72619d75adc69da7993c2cd3c" dependencies = [ - "atty", + "chrono", "log", - "winapi", + "termcolor", ] [[package]] @@ -1268,6 +1259,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "termcolor" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f" +dependencies = [ + "winapi-util", +] + [[package]] name = "terminal_size" version = "0.1.12" @@ -1296,6 +1296,16 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "time" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "toml" version = "0.5.6" diff --git a/README.md b/README.md index 5d1b975..398343a 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,7 @@ Default configuration: ```toml socket_workers = 1 +log_level = 'error' [network] address = '0.0.0.0:3000' diff --git a/TODO.md b/TODO.md index 1c8da5a..4fd504c 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,7 @@ # TODO ## aquatic_ws -* add sensible logging method, maybe stderrlog with quiet as default +* ipv4 and ipv6 state split: think about this more.. ## aquatic_udp * mio: set oneshot for epoll and kqueue? otherwise, stop reregistering? diff --git a/aquatic_ws/Cargo.toml b/aquatic_ws/Cargo.toml index 3037d6d..6571831 100644 --- a/aquatic_ws/Cargo.toml +++ b/aquatic_ws/Cargo.toml @@ -31,7 +31,7 @@ privdrop = "0.3" rand = { version = "0.7", features = ["small_rng"] } serde = { version = "1", features = ["derive"] } serde_json = "1" -simple_logger = { version = "1", default-features = false } +simplelog = "0.8" tungstenite = "0.10" [dev-dependencies] diff --git a/aquatic_ws/src/bin/main.rs b/aquatic_ws/src/bin/main.rs index 0bdd527..a1afa63 100644 --- a/aquatic_ws/src/bin/main.rs +++ b/aquatic_ws/src/bin/main.rs @@ -1,5 +1,8 @@ -use aquatic_ws; -use cli_helpers; +use anyhow::Context; +use cli_helpers::run_app_with_cli_and_config; +use simplelog::{ConfigBuilder, LevelFilter, TermLogger, TerminalMode, ThreadLogMode}; + +use aquatic_ws::config::{Config, LogLevel}; #[global_allocator] @@ -7,10 +10,35 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; fn main(){ - // ::simple_logger::init().unwrap(); - - cli_helpers::run_app_with_cli_and_config::( + run_app_with_cli_and_config::( "aquatic: webtorrent tracker", - aquatic_ws::run, + run ) +} + + +fn run(config: Config) -> anyhow::Result<()> { + let level_filter = match config.log_level { + LogLevel::Off => LevelFilter::Off, + LogLevel::Error => LevelFilter::Error, + LogLevel::Warn => LevelFilter::Warn, + LogLevel::Info => LevelFilter::Info, + LogLevel::Debug => LevelFilter::Debug, + LogLevel::Trace => LevelFilter::Trace, + }; + + let simplelog_config = ConfigBuilder::new() + .set_time_to_local(true) + .set_location_level(LevelFilter::Off) + .set_thread_level(LevelFilter::Error) + .set_thread_mode(ThreadLogMode::Both) + .build(); + + TermLogger::init( + level_filter, + simplelog_config, + TerminalMode::Stdout + ).context("Couldn't initialize logger")?; + + aquatic_ws::run(config) } \ No newline at end of file diff --git a/aquatic_ws/src/lib/common.rs b/aquatic_ws/src/lib/common.rs index 355d367..89dfa9b 100644 --- a/aquatic_ws/src/lib/common.rs +++ b/aquatic_ws/src/lib/common.rs @@ -4,8 +4,8 @@ use std::sync::Arc; use flume::{Sender, Receiver}; use hashbrown::HashMap; use indexmap::IndexMap; -use parking_lot::Mutex; use mio::Token; +use parking_lot::Mutex; pub use aquatic_common::ValidUntil; @@ -130,4 +130,4 @@ impl OutMessageSender { pub type SocketWorkerStatus = Option>; -pub type SocketWorkerStatuses = Arc>>; \ No newline at end of file +pub type SocketWorkerStatuses = Arc>>; diff --git a/aquatic_ws/src/lib/config.rs b/aquatic_ws/src/lib/config.rs index eca22e3..5defc63 100644 --- a/aquatic_ws/src/lib/config.rs +++ b/aquatic_ws/src/lib/config.rs @@ -3,6 +3,25 @@ use std::net::SocketAddr; use serde::{Serialize, Deserialize}; +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum LogLevel { + Off, + Error, + Warn, + Info, + Debug, + Trace +} + + +impl Default for LogLevel { + fn default() -> Self { + Self::Error + } +} + + #[derive(Clone, Debug, Serialize, Deserialize)] #[serde(default)] pub struct Config { @@ -10,6 +29,7 @@ pub struct Config { /// them on to the request handler. They then recieve responses from the /// request handler, encode them and send them back over the socket. pub socket_workers: usize, + pub log_level: LogLevel, pub network: NetworkConfig, pub handlers: HandlerConfig, pub cleaning: CleaningConfig, @@ -77,6 +97,7 @@ impl Default for Config { fn default() -> Self { Self { socket_workers: 1, + log_level: LogLevel::default(), network: NetworkConfig::default(), handlers: HandlerConfig::default(), cleaning: CleaningConfig::default(),