aquatic_ws: add logging with level configured by config file

This commit is contained in:
Joakim Frostegård 2020-05-23 20:15:34 +02:00
parent 0d4bc25ea9
commit bc8916dce4
7 changed files with 96 additions and 36 deletions

62
Cargo.lock generated
View file

@ -113,7 +113,7 @@ dependencies = [
"rand", "rand",
"serde", "serde",
"serde_json", "serde_json",
"simple_logger", "simplelog",
"tungstenite", "tungstenite",
] ]
@ -169,17 +169,6 @@ dependencies = [
"toml", "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]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.0.0" version = "1.0.0"
@ -258,6 +247,17 @@ version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" 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]] [[package]]
name = "cli_helpers" name = "cli_helpers"
version = "0.1.0" version = "0.1.0"
@ -520,15 +520,6 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "hermit-abi"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91780f809e750b0a89f5544be56617ff6b1227ee485bcb06ebe10cdf89bd3b71"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "histogram" name = "histogram"
version = "0.6.9" version = "0.6.9"
@ -1209,14 +1200,14 @@ dependencies = [
] ]
[[package]] [[package]]
name = "simple_logger" name = "simplelog"
version = "1.6.0" version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fea0c4611f32f4c2bac73754f22dca1f57e6c1945e0590dae4e5f2a077b92367" checksum = "2b2736f58087298a448859961d3f4a0850b832e72619d75adc69da7993c2cd3c"
dependencies = [ dependencies = [
"atty", "chrono",
"log", "log",
"winapi", "termcolor",
] ]
[[package]] [[package]]
@ -1268,6 +1259,15 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "termcolor"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f"
dependencies = [
"winapi-util",
]
[[package]] [[package]]
name = "terminal_size" name = "terminal_size"
version = "0.1.12" version = "0.1.12"
@ -1296,6 +1296,16 @@ dependencies = [
"lazy_static", "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]] [[package]]
name = "toml" name = "toml"
version = "0.5.6" version = "0.5.6"

View file

@ -140,6 +140,7 @@ Default configuration:
```toml ```toml
socket_workers = 1 socket_workers = 1
log_level = 'error'
[network] [network]
address = '0.0.0.0:3000' address = '0.0.0.0:3000'

View file

@ -1,7 +1,7 @@
# TODO # TODO
## aquatic_ws ## aquatic_ws
* add sensible logging method, maybe stderrlog with quiet as default * ipv4 and ipv6 state split: think about this more..
## aquatic_udp ## aquatic_udp
* mio: set oneshot for epoll and kqueue? otherwise, stop reregistering? * mio: set oneshot for epoll and kqueue? otherwise, stop reregistering?

View file

@ -31,7 +31,7 @@ privdrop = "0.3"
rand = { version = "0.7", features = ["small_rng"] } rand = { version = "0.7", features = ["small_rng"] }
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = "1" serde_json = "1"
simple_logger = { version = "1", default-features = false } simplelog = "0.8"
tungstenite = "0.10" tungstenite = "0.10"
[dev-dependencies] [dev-dependencies]

View file

@ -1,5 +1,8 @@
use aquatic_ws; use anyhow::Context;
use cli_helpers; use cli_helpers::run_app_with_cli_and_config;
use simplelog::{ConfigBuilder, LevelFilter, TermLogger, TerminalMode, ThreadLogMode};
use aquatic_ws::config::{Config, LogLevel};
#[global_allocator] #[global_allocator]
@ -7,10 +10,35 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
fn main(){ fn main(){
// ::simple_logger::init().unwrap(); run_app_with_cli_and_config::<Config>(
cli_helpers::run_app_with_cli_and_config::<aquatic_ws::config::Config>(
"aquatic: webtorrent tracker", "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)
} }

View file

@ -4,8 +4,8 @@ use std::sync::Arc;
use flume::{Sender, Receiver}; use flume::{Sender, Receiver};
use hashbrown::HashMap; use hashbrown::HashMap;
use indexmap::IndexMap; use indexmap::IndexMap;
use parking_lot::Mutex;
use mio::Token; use mio::Token;
use parking_lot::Mutex;
pub use aquatic_common::ValidUntil; pub use aquatic_common::ValidUntil;
@ -130,4 +130,4 @@ impl OutMessageSender {
pub type SocketWorkerStatus = Option<Result<(), String>>; pub type SocketWorkerStatus = Option<Result<(), String>>;
pub type SocketWorkerStatuses = Arc<Mutex<Vec<SocketWorkerStatus>>>; pub type SocketWorkerStatuses = Arc<Mutex<Vec<SocketWorkerStatus>>>;

View file

@ -3,6 +3,25 @@ use std::net::SocketAddr;
use serde::{Serialize, Deserialize}; 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)] #[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default)] #[serde(default)]
pub struct Config { pub struct Config {
@ -10,6 +29,7 @@ pub struct Config {
/// them on to the request handler. They then recieve responses from the /// them on to the request handler. They then recieve responses from the
/// request handler, encode them and send them back over the socket. /// request handler, encode them and send them back over the socket.
pub socket_workers: usize, pub socket_workers: usize,
pub log_level: LogLevel,
pub network: NetworkConfig, pub network: NetworkConfig,
pub handlers: HandlerConfig, pub handlers: HandlerConfig,
pub cleaning: CleaningConfig, pub cleaning: CleaningConfig,
@ -77,6 +97,7 @@ impl Default for Config {
fn default() -> Self { fn default() -> Self {
Self { Self {
socket_workers: 1, socket_workers: 1,
log_level: LogLevel::default(),
network: NetworkConfig::default(), network: NetworkConfig::default(),
handlers: HandlerConfig::default(), handlers: HandlerConfig::default(),
cleaning: CleaningConfig::default(), cleaning: CleaningConfig::default(),