mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-01 18:25:30 +00:00
init logger in aquatic_cli_helpers crate
This commit is contained in:
parent
427c0bc7c3
commit
8d58f8bb70
15 changed files with 113 additions and 119 deletions
3
Cargo.lock
generated
3
Cargo.lock
generated
|
|
@ -43,6 +43,7 @@ dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"gumdrop",
|
"gumdrop",
|
||||||
"serde",
|
"serde",
|
||||||
|
"simplelog",
|
||||||
"toml",
|
"toml",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -78,7 +79,6 @@ dependencies = [
|
||||||
"quickcheck_macros",
|
"quickcheck_macros",
|
||||||
"rand",
|
"rand",
|
||||||
"serde",
|
"serde",
|
||||||
"simplelog",
|
|
||||||
"smartstring",
|
"smartstring",
|
||||||
"socket2",
|
"socket2",
|
||||||
]
|
]
|
||||||
|
|
@ -210,7 +210,6 @@ dependencies = [
|
||||||
"quickcheck_macros",
|
"quickcheck_macros",
|
||||||
"rand",
|
"rand",
|
||||||
"serde",
|
"serde",
|
||||||
"simplelog",
|
|
||||||
"socket2",
|
"socket2",
|
||||||
"tungstenite",
|
"tungstenite",
|
||||||
]
|
]
|
||||||
|
|
|
||||||
3
TODO.md
3
TODO.md
|
|
@ -2,9 +2,6 @@
|
||||||
|
|
||||||
## General
|
## General
|
||||||
|
|
||||||
* init logging in cli helper crate? kind of tricky, requires cli helper
|
|
||||||
knowledge of config, such as config implementing a trait to get log
|
|
||||||
level
|
|
||||||
* automatic tests running real clients in container?
|
* automatic tests running real clients in container?
|
||||||
|
|
||||||
## aquatic_http_load_test
|
## aquatic_http_load_test
|
||||||
|
|
|
||||||
|
|
@ -9,4 +9,5 @@ license = "Apache-2.0"
|
||||||
anyhow = "1"
|
anyhow = "1"
|
||||||
gumdrop = "0.8"
|
gumdrop = "0.8"
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
|
simplelog = "0.8"
|
||||||
toml = "0.5"
|
toml = "0.5"
|
||||||
|
|
@ -3,7 +3,34 @@ use std::io::Read;
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use gumdrop::Options;
|
use gumdrop::Options;
|
||||||
use serde::{Serialize, de::DeserializeOwned};
|
use serde::{Serialize, Deserialize, de::DeserializeOwned};
|
||||||
|
use simplelog::{ConfigBuilder, LevelFilter, TermLogger, TerminalMode};
|
||||||
|
|
||||||
|
|
||||||
|
#[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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub trait Config: Default + Serialize + DeserializeOwned {
|
||||||
|
fn get_log_level(&self) -> Option<LogLevel> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, Options)]
|
#[derive(Debug, Options)]
|
||||||
|
|
@ -21,7 +48,7 @@ pub fn run_app_with_cli_and_config<T>(
|
||||||
title: &str,
|
title: &str,
|
||||||
// Function that takes config file and runs application
|
// Function that takes config file and runs application
|
||||||
app_fn: fn(T) -> anyhow::Result<()>,
|
app_fn: fn(T) -> anyhow::Result<()>,
|
||||||
) where T: Default + Serialize + DeserializeOwned {
|
) where T: Config {
|
||||||
::std::process::exit(match run_inner(title, app_fn) {
|
::std::process::exit(match run_inner(title, app_fn) {
|
||||||
Ok(()) => 0,
|
Ok(()) => 0,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
|
@ -37,7 +64,7 @@ fn run_inner<T>(
|
||||||
title: &str,
|
title: &str,
|
||||||
// Function that takes config file and runs application
|
// Function that takes config file and runs application
|
||||||
app_fn: fn(T) -> anyhow::Result<()>,
|
app_fn: fn(T) -> anyhow::Result<()>,
|
||||||
) -> anyhow::Result<()> where T: Default + Serialize + DeserializeOwned {
|
) -> anyhow::Result<()> where T: Config {
|
||||||
let args: Vec<String> = ::std::env::args().collect();
|
let args: Vec<String> = ::std::env::args().collect();
|
||||||
|
|
||||||
let opts = AppOptions::parse_args_default(&args[1..])?;
|
let opts = AppOptions::parse_args_default(&args[1..])?;
|
||||||
|
|
@ -51,15 +78,36 @@ fn run_inner<T>(
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
} else if let Some(config_file) = opts.config_file {
|
} else if let Some(config_file) = opts.config_file {
|
||||||
let config = config_from_toml_file(config_file)?;
|
let config: T = config_from_toml_file(config_file)?;
|
||||||
|
|
||||||
|
if let Some(log_level) = config.get_log_level(){
|
||||||
|
start_logger(log_level)?;
|
||||||
|
}
|
||||||
|
|
||||||
app_fn(config)
|
app_fn(config)
|
||||||
} else {
|
} else {
|
||||||
app_fn(T::default())
|
let config = T::default();
|
||||||
|
|
||||||
|
if let Some(log_level) = config.get_log_level(){
|
||||||
|
start_logger(log_level)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
app_fn(config)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn print_help(title: &str, opt_error: Option<anyhow::Error>){
|
||||||
|
println!("{}", title);
|
||||||
|
|
||||||
|
if let Some(error) = opt_error {
|
||||||
|
println!("\nError: {:#}.", error);
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("\n{}", AppOptions::usage());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
fn config_from_toml_file<T>(path: String) -> anyhow::Result<T>
|
fn config_from_toml_file<T>(path: String) -> anyhow::Result<T>
|
||||||
where T: DeserializeOwned
|
where T: DeserializeOwned
|
||||||
{
|
{
|
||||||
|
|
@ -87,12 +135,27 @@ fn default_config_as_toml<T>() -> String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn print_help(title: &str, opt_error: Option<anyhow::Error>){
|
fn start_logger(log_level: LogLevel) -> ::anyhow::Result<()> {
|
||||||
println!("{}", title);
|
let level_filter = match 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,
|
||||||
|
};
|
||||||
|
|
||||||
if let Some(error) = opt_error {
|
// Note: logger doesn't seem to pick up thread names. Not a huge loss.
|
||||||
println!("\nError: {:#}.", error);
|
let simplelog_config = ConfigBuilder::new()
|
||||||
}
|
.set_time_to_local(true)
|
||||||
|
.set_location_level(LevelFilter::Off)
|
||||||
|
.build();
|
||||||
|
|
||||||
println!("\n{}", AppOptions::usage());
|
TermLogger::init(
|
||||||
}
|
level_filter,
|
||||||
|
simplelog_config,
|
||||||
|
TerminalMode::Stderr
|
||||||
|
).context("Couldn't initialize logger")?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
@ -32,7 +32,6 @@ parking_lot = "0.11"
|
||||||
privdrop = "0.3"
|
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"] }
|
||||||
simplelog = "0.8"
|
|
||||||
smartstring = "0.2"
|
smartstring = "0.2"
|
||||||
socket2 = { version = "0.3", features = ["reuseport"] }
|
socket2 = { version = "0.3", features = ["reuseport"] }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,5 @@
|
||||||
use anyhow::Context;
|
|
||||||
use aquatic_cli_helpers::run_app_with_cli_and_config;
|
use aquatic_cli_helpers::run_app_with_cli_and_config;
|
||||||
use simplelog::{ConfigBuilder, LevelFilter, TermLogger, TerminalMode};
|
use aquatic_http::config::Config;
|
||||||
|
|
||||||
use aquatic_http::config::{Config, LogLevel};
|
|
||||||
|
|
||||||
|
|
||||||
#[global_allocator]
|
#[global_allocator]
|
||||||
|
|
@ -12,33 +9,6 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
||||||
fn main(){
|
fn main(){
|
||||||
run_app_with_cli_and_config::<Config>(
|
run_app_with_cli_and_config::<Config>(
|
||||||
"aquatic: BitTorrent (HTTP/TLS) tracker",
|
"aquatic: BitTorrent (HTTP/TLS) tracker",
|
||||||
run
|
aquatic_http::run
|
||||||
)
|
)
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// almost identical to ws version
|
|
||||||
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,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Note: logger doesn't seem to pick up thread names. Not a huge loss.
|
|
||||||
let simplelog_config = ConfigBuilder::new()
|
|
||||||
.set_time_to_local(true)
|
|
||||||
.set_location_level(LevelFilter::Off)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
TermLogger::init(
|
|
||||||
level_filter,
|
|
||||||
simplelog_config,
|
|
||||||
TerminalMode::Stderr
|
|
||||||
).context("Couldn't initialize logger")?;
|
|
||||||
|
|
||||||
aquatic_http::run(config)
|
|
||||||
}
|
}
|
||||||
|
|
@ -2,24 +2,7 @@ use std::net::SocketAddr;
|
||||||
|
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
use aquatic_cli_helpers::LogLevel;
|
||||||
#[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)]
|
||||||
|
|
@ -38,6 +21,13 @@ pub struct Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl aquatic_cli_helpers::Config for Config {
|
||||||
|
fn get_log_level(&self) -> Option<LogLevel>{
|
||||||
|
Some(self.log_level.clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub struct TlsConfig {
|
pub struct TlsConfig {
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,9 @@ pub struct Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl aquatic_cli_helpers::Config for Config {}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub struct NetworkConfig {
|
pub struct NetworkConfig {
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,9 @@ pub struct Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl aquatic_cli_helpers::Config for Config {}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub struct NetworkConfig {
|
pub struct NetworkConfig {
|
||||||
|
|
|
||||||
|
|
@ -23,4 +23,7 @@ impl Default for BenchConfig {
|
||||||
num_hashes_per_scrape_request: 20,
|
num_hashes_per_scrape_request: 20,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl aquatic_cli_helpers::Config for BenchConfig {}
|
||||||
|
|
@ -32,6 +32,9 @@ pub fn main(){
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl aquatic_cli_helpers::Config for Config {}
|
||||||
|
|
||||||
|
|
||||||
fn run(config: Config) -> ::anyhow::Result<()> {
|
fn run(config: Config) -> ::anyhow::Result<()> {
|
||||||
if config.handler.weight_announce + config.handler.weight_connect + config.handler.weight_scrape == 0 {
|
if config.handler.weight_announce + config.handler.weight_connect + config.handler.weight_scrape == 0 {
|
||||||
panic!("Error: at least one weight must be larger than zero.");
|
panic!("Error: at least one weight must be larger than zero.");
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,6 @@ 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"] }
|
||||||
socket2 = { version = "0.3", features = ["reuseport"] }
|
socket2 = { version = "0.3", features = ["reuseport"] }
|
||||||
simplelog = "0.8"
|
|
||||||
tungstenite = "0.11"
|
tungstenite = "0.11"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,5 @@
|
||||||
use anyhow::Context;
|
|
||||||
use aquatic_cli_helpers::run_app_with_cli_and_config;
|
use aquatic_cli_helpers::run_app_with_cli_and_config;
|
||||||
use simplelog::{ConfigBuilder, LevelFilter, TermLogger, TerminalMode};
|
use aquatic_ws::config::Config;
|
||||||
|
|
||||||
use aquatic_ws::config::{Config, LogLevel};
|
|
||||||
|
|
||||||
|
|
||||||
#[global_allocator]
|
#[global_allocator]
|
||||||
|
|
@ -12,32 +9,6 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
||||||
fn main(){
|
fn main(){
|
||||||
run_app_with_cli_and_config::<Config>(
|
run_app_with_cli_and_config::<Config>(
|
||||||
"aquatic: webtorrent tracker",
|
"aquatic: webtorrent tracker",
|
||||||
run
|
aquatic_ws::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,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Note: logger doesn't seem to pick up thread names. Not a huge loss.
|
|
||||||
let simplelog_config = ConfigBuilder::new()
|
|
||||||
.set_time_to_local(true)
|
|
||||||
.set_location_level(LevelFilter::Off)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
TermLogger::init(
|
|
||||||
level_filter,
|
|
||||||
simplelog_config,
|
|
||||||
TerminalMode::Stderr
|
|
||||||
).context("Couldn't initialize logger")?;
|
|
||||||
|
|
||||||
aquatic_ws::run(config)
|
|
||||||
}
|
}
|
||||||
|
|
@ -2,24 +2,7 @@ use std::net::SocketAddr;
|
||||||
|
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
use aquatic_cli_helpers::LogLevel;
|
||||||
#[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)]
|
||||||
|
|
@ -38,6 +21,13 @@ pub struct Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl aquatic_cli_helpers::Config for Config {
|
||||||
|
fn get_log_level(&self) -> Option<LogLevel> {
|
||||||
|
Some(self.log_level.clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub struct NetworkConfig {
|
pub struct NetworkConfig {
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,9 @@ pub struct Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl aquatic_cli_helpers::Config for Config {}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub struct NetworkConfig {
|
pub struct NetworkConfig {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue