Skip to content

Commit

Permalink
Merge pull request #35 from nttcom-webcore/feat/log-level-command
Browse files Browse the repository at this point in the history
ログレベル設定コマンドの追加
  • Loading branch information
yuki-uchida authored Apr 5, 2024
2 parents 9a750ee + 639216f commit 2308c10
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 4 deletions.
113 changes: 113 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -out cert.pem -subj '/

- `cargo run -p moqt-server-sample`

#### ログレベルの指定

- `cargo run -p moqt-server-sample -- -log <Log Level>`
- Default setting is `DEBUG`

### moqt-client-sample の実行

- `cd js && npm install && npm run dev`
Expand Down
1 change: 1 addition & 0 deletions moqt-server-sample/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2021"
anyhow = { version = "1.0.75", features = ["backtrace"] }
moqt-server = {path = "../moqt-server"}
tokio = { version = "1.32.0", features = ["rt-multi-thread"] }
clap = { version = "4.5.4", features = ["derive"] }


[features]
Expand Down
10 changes: 10 additions & 0 deletions moqt-server-sample/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
use anyhow::{Ok, Result};
use clap::Parser;
use moqt_server::{constants::UnderlayType, MOQTConfig, MOQT};

#[derive(Debug, Parser)]
struct Arg {
#[arg(short = 'l', long = "log", help = "Log Level", default_value = "DEBUG")]
log_level: String,
}

#[tokio::main]
async fn main() -> Result<()> {
let args = Arg::parse();

let mut config = MOQTConfig::new();
config.underlay = UnderlayType::WebTransport;
config.key_path = "./moqt-server-sample/keys/key.pem".to_string();
config.cert_path = "./moqt-server-sample/keys/cert.pem".to_string();
config.log_level = args.log_level.to_string();

// config.auth_callback = Some(|track_name: String, auth_payload: String, auth_callback_type: AuthCallbackType| {Ok(())});

Expand Down
28 changes: 24 additions & 4 deletions moqt-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct MOQTConfig {
pub keep_alive_interval_sec: u64,
pub underlay: UnderlayType,
pub auth_callback: Option<AuthCallbackFunctionType>,
pub log_level: String,
}

impl Default for MOQTConfig {
Expand All @@ -57,6 +58,7 @@ impl MOQTConfig {
keep_alive_interval_sec: 3,
underlay: UnderlayType::Both,
auth_callback: None,
log_level: "DEBUG".to_string(),
}
}
}
Expand All @@ -68,6 +70,7 @@ pub struct MOQT {
keep_alive_interval_sec: u64,
underlay: UnderlayType,
// auth_callback: Option<AuthCallbackFunctionType>,
log_level: String,
}

impl MOQT {
Expand All @@ -79,10 +82,11 @@ impl MOQT {
keep_alive_interval_sec: config.keep_alive_interval_sec,
underlay: config.underlay,
// auth_callback: config.auth_callback,
log_level: config.log_level,
}
}
pub async fn start(&self) -> Result<()> {
init_logging();
init_logging(self.log_level.to_string());

// For buffer management for each stream
let (buffer_tx, mut buffer_rx) = mpsc::channel::<BufferCommand>(1024);
Expand Down Expand Up @@ -337,15 +341,31 @@ async fn handle_stream(
Ok::<()>(())
}

// TODO: LogLevelを設定できるようにする
fn init_logging() {
fn init_logging(log_level: String) {
let level_filter: LevelFilter = match log_level.to_uppercase().as_str() {
"OFF" => LevelFilter::OFF,
"TRACE" => LevelFilter::TRACE,
"DEBUG" => LevelFilter::DEBUG,
"INFO" => LevelFilter::INFO,
"WARN" => LevelFilter::WARN,
"ERROR" => LevelFilter::ERROR,
_ => {
panic!(
"Invalid log level: '{}'.\n Valid log levels: [OFF, TRACE, DEBUG, INFO, WARN, ERROR]",
log_level
);
}
};

let env_filter = EnvFilter::builder()
.with_default_directive(LevelFilter::DEBUG.into())
.with_default_directive(level_filter.into())
.from_env_lossy();

tracing_subscriber::fmt()
.with_target(true)
.with_level(true)
.with_env_filter(env_filter)
.init();

tracing::info!("Logging initialized. (Level: {})", log_level);
}

0 comments on commit 2308c10

Please sign in to comment.