Skip to content

Commit

Permalink
refactor: log_level and add config #76
Browse files Browse the repository at this point in the history
  • Loading branch information
a-wing committed Dec 29, 2023
1 parent 7adc4d4 commit b0181fd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
6 changes: 6 additions & 0 deletions config-dist.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ tokens = ["live777"]
# [[auth.accounts]]
# username = "live777"
# password = "live777"

[log]
# Env: `LOG_LEVEL`
# Default: info
# Values: off, error, warn, info, debug, trace
# level = "warn"
19 changes: 19 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub struct Config {
pub ice_servers: Vec<IceServer>,
#[serde(default)]
pub auth: Auth,
#[serde(default)]
pub log: Log,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Auth {
Expand All @@ -27,6 +29,12 @@ pub struct Account {
pub password: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Log {
#[serde(default = "default_log_level")]
pub level: String,
}

fn default_listen() -> String {
format!("[::]:{}", env::var("PORT").unwrap_or(String::from("7777")))
}
Expand All @@ -40,6 +48,16 @@ fn default_ice_servers() -> Vec<IceServer> {
}]
}

fn default_log_level() -> String {
env::var("LOG_LEVEL").unwrap_or_else(|_|
if cfg!(debug_assertions) {
"debug".to_string()
} else {
"info".to_string()
}
)
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct IceServer {
#[serde(default)]
Expand Down Expand Up @@ -128,6 +146,7 @@ impl Config {
ice_servers: default_ice_servers(),
listen: default_listen(),
auth: Default::default(),
log: Default::default(),
}
}
}
Expand Down
20 changes: 3 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::env;
use std::net::SocketAddr;
use std::str::FromStr;
use std::sync::Arc;
Expand Down Expand Up @@ -47,27 +46,14 @@ async fn main() {
metrics::REGISTRY
.register(Box::new(metrics::SUBSCRIBE.clone()))
.unwrap();
let log_level = if cfg!(debug_assertions) {
env::var("LOG_LEVEL").unwrap_or_else(|_| "debug".to_string())
} else {
env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string())
};
let level_filter = match log_level.as_str() {
"off" => log::LevelFilter::Off,
"error" => log::LevelFilter::Error,
"warn" => log::LevelFilter::Warn,
"info" => log::LevelFilter::Info,
"debug" => log::LevelFilter::Debug,
"trace" => log::LevelFilter::Trace,
_ => log::LevelFilter::Info,
};

let cfg = Config::parse();
env_logger::builder()
.filter_level(level_filter)
.parse_filters(cfg.log.level.as_str())
.filter_module("webrtc", log::LevelFilter::Error)
.write_style(env_logger::WriteStyle::Auto)
.target(env_logger::Target::Stdout)
.init();
let cfg = Config::parse();
let addr = SocketAddr::from_str(&cfg.listen).expect("invalid listen address");
info!("Server listening on {}", addr);
let ice_servers = cfg
Expand Down

0 comments on commit b0181fd

Please sign in to comment.