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

View file

@ -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]

View file

@ -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::<aquatic_ws::config::Config>(
run_app_with_cli_and_config::<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)
}

View file

@ -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<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};
#[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(),