Skip to content

Commit e4a925e

Browse files
committed
replace slog with tracing
1 parent edf7e9f commit e4a925e

File tree

7 files changed

+112
-94
lines changed

7 files changed

+112
-94
lines changed

Cargo.toml

+17-4
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,31 @@ cfg-if = "1.0.0"
2525
err-derive = "0.3.0"
2626
xtra = "0.5.1"
2727
async-trait = "0.1.49"
28-
slog = "2.7.0"
2928
xtra_proc = "0.1.0"
30-
tokio = { version = "1.5.0", features = ["process", "io-util", "io-std", "macros", "time", "rt"] }
31-
tokio-stream = { version = "0.1.5", features = ["io-util"] }
3229
mp4 = { git = "https://github.com/vgarleanu/mp4-rust" }
3330
once_cell = "1.8.0"
3431

32+
tracing = "0.1.29"
33+
tokio-stream = { version = "0.1.5", features = ["io-util"] }
34+
tokio = { version = "1.5.0", features = [
35+
"process",
36+
"io-util",
37+
"io-std",
38+
"macros",
39+
"time",
40+
"rt",
41+
] }
42+
43+
3544
[target.'cfg(unix)'.dependencies]
3645
nix = "0.20.0"
3746
psutil = "3.2.0"
3847
rusty_vainfo = { version = "0.1.4", optional = true }
3948

4049
[target.'cfg(windows)'.dependencies]
41-
winapi = { version = "^0.3.9", features = ["winerror", "synchapi", "minwinbase"] }
4250
ntapi = "^0.3.6"
51+
winapi = { version = "^0.3.9", features = [
52+
"winerror",
53+
"synchapi",
54+
"minwinbase",
55+
] }

examples/dump_args.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ fn main() {
88
let profile = args.next().expect("Usage: dump_args <profile> <file>");
99
let file = args.next().expect("Usage: dump_args <profile> <file>");
1010

11-
let drain = slog::Discard;
12-
let log = slog::Logger::root(drain, slog::o!());
13-
14-
profiles_init(log, "/usr/bin/ffmpeg".to_string());
11+
profiles_init("/usr/bin/ffmpeg".to_string());
1512

1613
let profile = get_active_profiles()
1714
.into_iter()

examples/list_device_profiles.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
use nightfall::profiles::*;
22

33
fn main() {
4-
let drain = slog::Discard;
5-
let log = slog::Logger::root(drain, slog::o!());
6-
7-
profiles_init(log, "/usr/bin/ffmpeg".to_string());
4+
profiles_init("/usr/bin/ffmpeg".to_string());
85

96
for profile in get_active_profiles() {
107
println!("{:?}", profile);

src/lib.rs

+38-40
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,17 @@ use crate::profiles::*;
2020
use crate::session::Session;
2121

2222
use std::collections::HashMap;
23+
use std::fmt;
2324
use std::time::Duration;
2425
use std::time::Instant;
25-
use std::fmt;
2626

2727
use async_trait::async_trait;
28+
use tracing::debug;
29+
use tracing::info;
30+
use tracing::warn;
2831
use xtra_proc::actor;
2932
use xtra_proc::handler;
3033

31-
use slog::debug;
32-
use slog::error;
33-
use slog::info;
34-
use slog::warn;
35-
3634
pub use tokio::process::ChildStdout;
3735

3836
pub struct StreamStat {
@@ -61,8 +59,6 @@ pub struct StateManager {
6159
pub stream_stats: HashMap<String, StreamStat>,
6260
/// Contains the exit status of dead sessions
6361
pub exit_statuses: HashMap<String, String>,
64-
/// Logger
65-
pub logger: slog::Logger,
6662
}
6763

6864
impl fmt::Debug for __ActorStateManager::StateManager {
@@ -78,18 +74,16 @@ impl fmt::Debug for __ActorStateManager::StateManager {
7874

7975
impl fmt::Debug for StateManager {
8076
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81-
f.debug_struct("StateManagerActor")
82-
.finish()
77+
f.debug_struct("StateManagerActor").finish()
8378
}
8479
}
8580

8681
#[actor]
8782
impl StateManager {
88-
pub fn new(outdir: String, ffmpeg: String, logger: slog::Logger) -> Self {
83+
pub fn new(outdir: String, ffmpeg: String) -> Self {
8984
Self {
9085
outdir,
9186
ffmpeg,
92-
logger,
9387
sessions: HashMap::new(),
9488
stream_stats: HashMap::new(),
9589
exit_statuses: HashMap::new(),
@@ -107,7 +101,11 @@ impl StateManager {
107101
let first_tag = if let Some(x) = profile_chain.first() {
108102
x.tag()
109103
} else {
110-
error!(self.logger, "Supplied profile chain is empty"; "profile" => format!("{:?}", profile_args));
104+
tracing::error!(
105+
"Supplied profile chain is empty {}",
106+
profile = format!("{:?}", profile_args)
107+
);
108+
111109
return Err(NightfallError::ProfileChainExhausted);
112110
};
113111

@@ -137,8 +135,8 @@ impl StateManager {
137135
};
138136

139137
info!(
140-
self.logger,
141-
"New session {} map {} -> {}", &session_id, profile_args.input_ctx.stream, tag
138+
"New session {} map {} -> {}",
139+
&session_id, profile_args.input_ctx.stream, tag
142140
);
143141

144142
profile_args.output_ctx.outdir = format!("{}/{}", &self.outdir, session_id);
@@ -150,7 +148,12 @@ impl StateManager {
150148
.unwrap_or(false)
151149
&& profile_chain.len() == 1;
152150

153-
info!(self.logger, "Session {} chain {}", &session_id, chain; "direct_play" => is_direct_play);
151+
info!(
152+
"Session {} chain {} {}",
153+
&session_id,
154+
chain,
155+
direct_play = is_direct_play
156+
);
154157

155158
let new_session = Session::new(
156159
session_id.clone(),
@@ -176,10 +179,7 @@ impl StateManager {
176179
if let Some(status) = session.exit_status.take() {
177180
if !status.success() {
178181
if let Some(x) = session.next_profile() {
179-
info!(
180-
self.logger,
181-
"Session {} chunk={} trying profile {}", &id, chunk, x
182-
);
182+
info!("Session {} chunk={} trying profile {}", &id, chunk, x);
183183
session.reset_to(session.start_num());
184184
} else {
185185
return Err(NightfallError::ProfileChainExhausted);
@@ -253,16 +253,12 @@ impl StateManager {
253253
stats.last_hard_seek = Instant::now();
254254
stats.hard_seeked_at = chunk;
255255

256-
debug!(
257-
self.logger,
258-
"Resetting {} to chunk {} because user seeked.", &id, chunk
259-
);
256+
debug!("Resetting {} to chunk {} because user seeked.", &id, chunk);
260257
}
261258

262259
Err(NightfallError::ChunkNotDone)
263260
} else {
264261
let chunk_path = session.chunk_to_path(chunk);
265-
let log = self.logger.clone();
266262
let path = chunk_path.clone();
267263
let real_segment = session.real_segment;
268264

@@ -272,31 +268,33 @@ impl StateManager {
272268
}
273269

274270
if !session.is_direct_play {
275-
match patch_segment(log, path, real_segment).await {
271+
match patch_segment(path, real_segment).await {
276272
Ok(seq) => session.real_segment = seq,
277273
// Sometimes we get partial chunks, when playback goes linearly (no hard seeks have
278274
// occured) we can ignore this, but when the user seeks, the player doesnt query
279275
// `init.mp4` again, so we have to move the video data from `init.mp4` into
280276
// `N.m4s`.
281277
Err(NightfallError::PartialSegment(_)) => {
282278
if session.chunks_since_init >= 1 {
283-
debug!(self.logger, "Got a partial segment, patching because the user has most likely seeked.");
279+
debug!( "Got a partial segment, patching because the user has most likely seeked.");
280+
284281
match patch_init_segment(
285-
self.logger.clone(),
286282
session.init_seg(),
287283
chunk_path.clone(),
288284
real_segment,
289285
)
290-
.await
291-
{
292-
Ok(seq) => session.real_segment = seq,
293-
Err(e) => {
294-
warn!(self.logger, "Failed to patch init segment."; "error" => e.to_string())
295-
}
286+
.await
287+
{
288+
Ok(seq) => session.real_segment = seq,
289+
Err(e) => {
290+
warn!("Failed to patch init segment. {}", error = e.to_string())
296291
}
292+
}
297293
}
298294
}
299-
Err(e) => warn!(self.logger, "Failed to patch segment."; "error" => e.to_string()),
295+
Err(e) => {
296+
warn!("Failed to patch segment. {}", error = e.to_string())
297+
}
300298
}
301299
}
302300

@@ -325,7 +323,7 @@ impl StateManager {
325323

326324
let stats = self.stream_stats.entry(id).or_default();
327325

328-
if !session.has_started() || session.is_direct_play{
326+
if !session.has_started() || session.is_direct_play {
329327
return Ok(false);
330328
}
331329
// if we are seeking backwards we always want to restart the stream
@@ -356,7 +354,7 @@ impl StateManager {
356354
.sessions
357355
.get_mut(&id)
358356
.ok_or(NightfallError::SessionDoesntExist)?;
359-
info!(self.logger, "Killing session {}", id);
357+
info!("Killing session {}", id);
360358
session.join().await;
361359
session.set_timeout();
362360

@@ -369,7 +367,7 @@ impl StateManager {
369367
.sessions
370368
.get_mut(&id)
371369
.ok_or(NightfallError::SessionDoesntExist)?;
372-
info!(self.logger, "Killing session {}", id);
370+
info!("Killing session {}", id);
373371
session.join().await;
374372

375373
Ok(())
@@ -427,7 +425,7 @@ impl StateManager {
427425
};
428426

429427
if !to_reap.is_empty() {
430-
info!(self.logger, "Reaping {} streams", to_reap.len());
428+
info!("Reaping {} streams", to_reap.len());
431429
}
432430

433431
for (k, v) in to_reap.iter_mut() {
@@ -446,7 +444,7 @@ impl StateManager {
446444
}
447445

448446
if cnt != 0 {
449-
info!(self.logger, "Paused {} streams", cnt);
447+
info!("Paused {} streams", cnt);
450448
}
451449

452450
Ok(())

src/patch/init_segment.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::Result;
1212
use tokio::task::spawn_blocking;
1313

1414
use mp4::mp4box::*;
15-
use slog::warn;
15+
use tracing::warn;
1616

1717
#[derive(Default)]
1818
pub struct InitSegment {
@@ -24,11 +24,7 @@ pub struct InitSegment {
2424
}
2525

2626
impl InitSegment {
27-
pub fn from_reader(
28-
mut reader: impl BufRead + Seek,
29-
size: u64,
30-
log: slog::Logger,
31-
) -> Result<Self> {
27+
pub fn from_reader(mut reader: impl BufRead + Seek, size: u64) -> Result<Self> {
3228
let mut segment = Self::default();
3329
let start = reader.seek(SeekFrom::Current(0))?;
3430

@@ -69,7 +65,7 @@ impl InitSegment {
6965
current_segment.styp = Some(FtypBox::read_box(&mut reader, s)?);
7066
}
7167
b => {
72-
warn!(log, "Got a weird box type."; "box_type" => b.to_string());
68+
warn!("Got a weird box type. {}", box_type = b.to_string());
7369
BoxHeader { name: b, size: s }.write(&mut segment.moov)?;
7470
let mut boks = vec![0; (s - 8) as usize];
7571
reader.read_exact(boks.as_mut_slice())?;
@@ -113,7 +109,6 @@ impl InitSegment {
113109
/// * `segment` - Path to the segment
114110
/// * `seq` - starting sequence number
115111
pub async fn patch_init_segment(
116-
log: slog::Logger,
117112
init: impl AsRef<Path> + Send + 'static,
118113
segment_path: impl AsRef<Path> + Send + 'static,
119114
mut seq: u32,
@@ -123,7 +118,7 @@ pub async fn patch_init_segment(
123118
let size = f.metadata()?.len();
124119
let mut reader = BufReader::new(f);
125120

126-
let mut segment = InitSegment::from_reader(&mut reader, size, log.clone())?;
121+
let mut segment = InitSegment::from_reader(&mut reader, size)?;
127122

128123
let mut f = File::create(&segment_path)?;
129124
while let Some(segment) = segment.segments.pop_front() {

src/patch/segment.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ use std::path::Path;
99
use crate::NightfallError;
1010
use crate::Result;
1111

12-
use slog::debug;
1312
use tokio::task::spawn_blocking;
1413

1514
use mp4::mp4box::*;
15+
use tracing::debug;
1616

1717
/// Struct represents an individual segment from a stream.
1818
#[derive(Clone, Default, Debug)]
@@ -39,11 +39,7 @@ impl Segment {
3939
);
4040
}
4141

42-
pub fn from_reader(
43-
mut reader: impl BufRead + Seek,
44-
size: u64,
45-
log: slog::Logger,
46-
) -> Result<(Self, u64)> {
42+
pub fn from_reader(mut reader: impl BufRead + Seek, size: u64) -> Result<(Self, u64)> {
4743
let start = reader.seek(SeekFrom::Current(0))?;
4844

4945
let mut current = start;
@@ -74,7 +70,7 @@ impl Segment {
7470
segment.styp = Some(styp);
7571
}
7672
b => {
77-
debug!(log, "Got a weird box type."; "box_type" => b.to_string());
73+
debug!("Got a weird box type. {}", box_type = b.to_string());
7874
skip_box(&mut reader, s)?;
7975
}
8076
}
@@ -166,11 +162,7 @@ impl Segment {
166162
///
167163
/// # Returns
168164
/// This function will return the index of the current segment.
169-
pub async fn patch_segment(
170-
log: slog::Logger,
171-
file: impl AsRef<Path> + Send + 'static,
172-
mut seq: u32,
173-
) -> Result<u32> {
165+
pub async fn patch_segment(file: impl AsRef<Path> + Send + 'static, mut seq: u32) -> Result<u32> {
174166
spawn_blocking(move || {
175167
let f = File::open(&file)?;
176168
let size = f.metadata()?.len();
@@ -180,7 +172,7 @@ pub async fn patch_segment(
180172
let mut current = reader.seek(SeekFrom::Current(0))?;
181173

182174
while current < size {
183-
let (segment, new_position) = Segment::from_reader(&mut reader, size, log.clone())?;
175+
let (segment, new_position) = Segment::from_reader(&mut reader, size)?;
184176
segments.push_back(segment);
185177
current = new_position;
186178
}

0 commit comments

Comments
 (0)