Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: set logger without mucking around with env vars #503

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions examples/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ use str0m::media::{Direction, KeyframeRequest, MediaData, Mid, Rid};
use str0m::media::{KeyframeRequestKind, MediaKind};
use str0m::net::Protocol;
use str0m::{net::Receive, Candidate, Event, IceConnectionState, Input, Output, Rtc, RtcError};
use tracing::subscriber::DefaultGuard;
use tracing_subscriber::util::SubscriberInitExt as _;
use tracing_subscriber::EnvFilter;

mod util;

fn init_log() {
use std::env;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};

if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", "chat=info,str0m=info");
}

tracing_subscriber::registry()
.with(fmt::layer())
.with(EnvFilter::from_default_env())
.init();
fn init_log() -> DefaultGuard {
tracing_subscriber::fmt()
.with_env_filter(
EnvFilter::builder()
.with_default_directive("chat=info,str0m=info".parse().unwrap())
.from_env()
.unwrap(),
)
.set_default()
}

pub fn main() {
init_log();
let _guard = init_log();

let certificate = include_bytes!("cer.pem").to_vec();
let private_key = include_bytes!("key.pem").to_vec();
Expand Down
26 changes: 13 additions & 13 deletions examples/http-post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ use str0m::change::SdpOffer;
use str0m::net::Protocol;
use str0m::net::Receive;
use str0m::{Candidate, Event, IceConnectionState, Input, Output, Rtc, RtcError};
use tracing::subscriber::DefaultGuard;
use tracing_subscriber::util::SubscriberInitExt as _;
use tracing_subscriber::EnvFilter;

mod util;

fn init_log() {
use std::env;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};

if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", "http_post=debug,str0m=debug");
}

tracing_subscriber::registry()
.with(fmt::layer())
.with(EnvFilter::from_default_env())
.init();
fn init_log() -> DefaultGuard {
tracing_subscriber::fmt()
.with_env_filter(
EnvFilter::builder()
.with_default_directive("http_post=debug,str0m=debug".parse().unwrap())
.from_env()
.unwrap(),
)
.set_default()
}

pub fn main() {
init_log();
let _guard = init_log();

let certificate = include_bytes!("cer.pem").to_vec();
let private_key = include_bytes!("key.pem").to_vec();
Expand Down
21 changes: 5 additions & 16 deletions src/ice/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1571,7 +1571,7 @@ impl IceAgent {
mod test {
use super::*;
use std::net::SocketAddr;
use std::sync::Once;
use tracing_subscriber::util::SubscriberInitExt as _;

impl IceAgent {
fn pair_indexes(&self) -> Vec<(usize, usize)> {
Expand Down Expand Up @@ -1746,21 +1746,10 @@ mod test {

#[test]
fn form_pairs_replace_remote_redundant() {
use std::env;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};

if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", "debug");
}

static START: Once = Once::new();

START.call_once(|| {
tracing_subscriber::registry()
.with(fmt::layer())
.with(EnvFilter::from_default_env())
.init();
});
let _guard = tracing_subscriber::fmt()
.with_env_filter("debug")
.with_test_writer()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.set_default();

let mut agent = IceAgent::new();
agent.set_ice_lite(true);
Expand Down
14 changes: 0 additions & 14 deletions src/ice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,20 +398,6 @@ mod test {
);
}

// pub fn init_log() {
// use std::env;
// use tracing_subscriber::{fmt, prelude::*, EnvFilter};

// if env::var("RUST_LOG").is_err() {
// env::set_var("RUST_LOG", "trace");
// }

// tracing_subscriber::registry()
// .with(fmt::layer())
// .with(EnvFilter::from_default_env())
// .init();
// }

#[test]
pub fn ice_lite_disconnect_reconnect() {
// init_log();
Expand Down
2 changes: 1 addition & 1 deletion tests/bidirectional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use common::{init_log, progress, TestRtc};

#[test]
pub fn bidirectional_same_m_line() -> Result<(), RtcError> {
init_log();
let _guard = init_log();

let mut l = TestRtc::new(info_span!("L"));
let mut r = TestRtc::new(info_span!("R"));
Expand Down
28 changes: 12 additions & 16 deletions tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ use str0m::rtp::RtpHeader;
use str0m::Candidate;
use str0m::{Event, Input, Output, Rtc, RtcError};
use tracing::info_span;
use tracing::subscriber::DefaultGuard;
use tracing::Span;
use tracing_subscriber::util::SubscriberInitExt as _;
use tracing_subscriber::EnvFilter;

pub struct TestRtc {
pub span: Span,
Expand Down Expand Up @@ -201,22 +204,15 @@ impl DerefMut for TestRtc {
}
}

pub fn init_log() {
use std::env;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};

if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", "str0m=debug");
}

static START: Once = Once::new();

START.call_once(|| {
tracing_subscriber::registry()
.with(fmt::layer())
.with(EnvFilter::from_default_env())
.init();
});
pub fn init_log() -> DefaultGuard {
tracing_subscriber::fmt()
.with_env_filter(
EnvFilter::builder()
.with_default_directive("str0m=debug".parse().unwrap())
.from_env()
.unwrap(),
)
.set_default()
}

pub fn connect_l_r() -> (TestRtc, TestRtc) {
Expand Down
8 changes: 4 additions & 4 deletions tests/contiguous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use common::{init_log, progress, TestRtc};

#[test]
pub fn contiguous_all_the_way() -> Result<(), RtcError> {
init_log();
let _guard = init_log();

let output = Server::with_vp8_input()
.timeout(Duration::seconds(5))
Expand All @@ -34,7 +34,7 @@ pub fn contiguous_all_the_way() -> Result<(), RtcError> {

#[test]
pub fn not_contiguous() -> Result<(), RtcError> {
init_log();
let _guard = init_log();

let output = Server::with_vp8_input()
.skip_packet(14337)
Expand All @@ -61,7 +61,7 @@ pub fn not_contiguous() -> Result<(), RtcError> {

#[test]
pub fn vp9_contiguous_all_the_way() -> Result<(), RtcError> {
init_log();
let _guard = init_log();

let output = Server::with_vp9_input().get_output()?;
let mut count = 0;
Expand All @@ -82,7 +82,7 @@ pub fn vp9_contiguous_all_the_way() -> Result<(), RtcError> {

#[test]
pub fn vp9_not_contiguous() -> Result<(), RtcError> {
init_log();
let _guard = init_log();

let output = Server::with_vp9_input().skip_packet(30952).get_output()?;
let mut count = 0;
Expand Down
2 changes: 1 addition & 1 deletion tests/data-channel-direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use common::{init_log, progress, TestRtc};

#[test]
pub fn data_channel_direct() -> Result<(), RtcError> {
init_log();
let _guard = init_log();

let mut l = TestRtc::new(info_span!("L"));

Expand Down
2 changes: 1 addition & 1 deletion tests/data-channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use common::{init_log, progress, TestRtc};

#[test]
pub fn data_channel() -> Result<(), RtcError> {
init_log();
let _guard = init_log();

let mut l = TestRtc::new(info_span!("L"));
let mut r = TestRtc::new(info_span!("R"));
Expand Down
2 changes: 1 addition & 1 deletion tests/flappy-ice-state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use common::{init_log, progress, TestRtc};

#[test]
pub fn flappy_ice_lite_state() -> Result<(), RtcError> {
init_log();
let _guard = init_log();

let mut l = TestRtc::new(info_span!("L"));

Expand Down
2 changes: 1 addition & 1 deletion tests/ice-restart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use common::{init_log, progress, TestRtc};

#[test]
pub fn ice_restart() -> Result<(), RtcError> {
init_log();
let _guard = init_log();

let mut l = TestRtc::new(info_span!("L"));

Expand Down
6 changes: 3 additions & 3 deletions tests/keyframes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use common::{init_log, progress, TestRtc};

#[test]
pub fn test_vp8_keyframes_detection() -> Result<(), RtcError> {
init_log();
let _guard = init_log();

let mut l = TestRtc::new(info_span!("L"));
let mut r = TestRtc::new(info_span!("R"));
Expand Down Expand Up @@ -103,7 +103,7 @@ pub fn test_vp8_keyframes_detection() -> Result<(), RtcError> {

#[test]
pub fn test_vp9_keyframes_detection() -> Result<(), RtcError> {
init_log();
let _guard = init_log();

let mut l = TestRtc::new(info_span!("L"));
let mut r = TestRtc::new(info_span!("R"));
Expand Down Expand Up @@ -195,7 +195,7 @@ pub fn test_vp9_keyframes_detection() -> Result<(), RtcError> {

#[test]
pub fn test_h264_keyframes_detection() -> Result<(), RtcError> {
init_log();
let _guard = init_log();

let mut l = TestRtc::new(info_span!("L"));
let mut r = TestRtc::new(info_span!("R"));
Expand Down
4 changes: 2 additions & 2 deletions tests/nack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::common::progress_with_loss;

#[test]
pub fn loss_recovery() -> Result<(), RtcError> {
init_log();
let _guard = init_log();

let (mut l, mut r) = connect_l_r();

Expand Down Expand Up @@ -170,7 +170,7 @@ pub fn loss_recovery() -> Result<(), RtcError> {

#[test]
pub fn nack_delay() -> Result<(), RtcError> {
init_log();
let _guard = init_log();

let (mut l, mut r) = connect_l_r();

Expand Down
2 changes: 1 addition & 1 deletion tests/remb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use common::{init_log, negotiate, progress, TestRtc};

#[test]
pub fn remb() -> Result<(), RtcError> {
init_log();
let _guard = init_log();
let l_rtc = Rtc::builder().build();
let r_rtc = Rtc::builder().build();

Expand Down
2 changes: 1 addition & 1 deletion tests/repeated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use common::{connect_l_r, init_log, progress};

#[test]
pub fn repeated() -> Result<(), RtcError> {
init_log();
let _guard = init_log();

let (mut l, mut r) = connect_l_r();

Expand Down
2 changes: 1 addition & 1 deletion tests/rtp-direct-mid-rid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use common::{connect_l_r, init_log, progress};

#[test]
pub fn rtp_direct_mid_rid() -> Result<(), RtcError> {
init_log();
let _guard = init_log();

let (mut l, mut r) = connect_l_r();

Expand Down
2 changes: 1 addition & 1 deletion tests/rtp-direct-mid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use common::{connect_l_r, init_log, progress};

#[test]
pub fn rtp_direct_mid() -> Result<(), RtcError> {
init_log();
let _guard = init_log();

let (mut l, mut r) = connect_l_r();

Expand Down
2 changes: 1 addition & 1 deletion tests/rtp-direct-ssrc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use common::{connect_l_r, init_log, progress};

#[test]
pub fn rtp_direct_ssrc() -> Result<(), RtcError> {
init_log();
let _guard = init_log();

let (mut l, mut r) = connect_l_r();

Expand Down
2 changes: 1 addition & 1 deletion tests/rtp-direct-with-roc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use common::{connect_l_r, init_log, progress};

#[test]
pub fn rtp_direct_with_roc() -> Result<(), RtcError> {
init_log();
let _guard = init_log();

let (mut l, mut r) = connect_l_r();

Expand Down
2 changes: 1 addition & 1 deletion tests/rtx-cache-0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use common::{connect_l_r, init_log, progress};

#[test]
pub fn rtx_cache_0() -> Result<(), RtcError> {
init_log();
let _guard = init_log();

let (mut l, mut r) = connect_l_r();

Expand Down
Loading
Loading