Skip to content

Commit 575b0dd

Browse files
authored
Merge pull request #142 from rillian/log
Remove callers of `mp4parse_log` in favour of `env_logger`.
2 parents baba741 + ebf93e1 commit 575b0dd

File tree

6 files changed

+61
-68
lines changed

6 files changed

+61
-68
lines changed

mp4parse/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ abort_on_panic = { version = "1.0.0", optional = true }
2929
bitreader = { version = "0.3.0" }
3030
num-traits = "0.1.37"
3131
mp4parse_fallible = { version = "0.0.1", optional = true }
32+
log = "0.4"
3233

3334
[dev-dependencies]
3435
test-assembler = "0.1.2"

mp4parse/src/lib.rs

Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
#[cfg(feature = "fuzz")]
77
extern crate afl;
88

9+
#[macro_use]
10+
extern crate log;
11+
912
extern crate byteorder;
1013
extern crate bitreader;
1114
extern crate num_traits;
@@ -36,25 +39,6 @@ const BUF_SIZE_LIMIT: usize = 1024 * 1024;
3639
// frame per table entry in 30 fps.
3740
const TABLE_SIZE_LIMIT: u32 = 30 * 60 * 60 * 24 * 7;
3841

39-
static DEBUG_MODE: std::sync::atomic::AtomicBool = std::sync::atomic::ATOMIC_BOOL_INIT;
40-
41-
pub fn set_debug_mode(mode: bool) {
42-
DEBUG_MODE.store(mode, std::sync::atomic::Ordering::SeqCst);
43-
}
44-
45-
#[inline(always)]
46-
fn get_debug_mode() -> bool {
47-
DEBUG_MODE.load(std::sync::atomic::Ordering::Relaxed)
48-
}
49-
50-
macro_rules! log {
51-
($($args:tt)*) => (
52-
if get_debug_mode() {
53-
println!( $( $args )* );
54-
}
55-
)
56-
}
57-
5842
// TODO: vec_push() and vec_reserve() needs to be replaced when Rust supports
5943
// fallible memory allocation in raw_vec.
6044
#[allow(unreachable_code)]
@@ -582,7 +566,7 @@ impl<'a, T: Read> Drop for BMFFBox<'a, T> {
582566
fn drop(&mut self) {
583567
if self.content.limit() > 0 {
584568
let name: FourCC = From::from(self.head.name);
585-
log!("Dropping {} bytes in '{}'", self.content.limit(), name);
569+
debug!("Dropping {} bytes in '{}'", self.content.limit(), name);
586570
}
587571
}
588572
}
@@ -636,7 +620,7 @@ fn skip_box_content<T: Read>(src: &mut BMFFBox<T>) -> Result<()> {
636620
// Skip the contents of unknown chunks.
637621
let to_skip = {
638622
let header = src.get_header();
639-
log!("{:?} (skipped)", header);
623+
debug!("{:?} (skipped)", header);
640624
(header.size - header.offset) as usize
641625
};
642626
assert_eq!(to_skip, src.bytes_left());
@@ -648,7 +632,7 @@ fn skip_box_remain<T: Read>(src: &mut BMFFBox<T>) -> Result<()> {
648632
let remain = {
649633
let header = src.get_header();
650634
let len = src.bytes_left();
651-
log!("remain {} (skipped) in {:?}", len, header);
635+
debug!("remain {} (skipped) in {:?}", len, header);
652636
len
653637
};
654638
skip(src, remain)
@@ -657,7 +641,7 @@ fn skip_box_remain<T: Read>(src: &mut BMFFBox<T>) -> Result<()> {
657641
macro_rules! check_parser_state {
658642
( $src:expr ) => {
659643
if $src.limit() > 0 {
660-
log!("bad parser state: {} content bytes left", $src.limit());
644+
debug!("bad parser state: {} content bytes left", $src.limit());
661645
return Err(Error::InvalidData("unread box content or bad parser sync"));
662646
}
663647
}
@@ -693,7 +677,7 @@ pub fn read_mp4<T: Read>(f: &mut T, context: &mut MediaContext) -> Result<()> {
693677
BoxType::FileTypeBox => {
694678
let ftyp = read_ftyp(&mut b)?;
695679
found_ftyp = true;
696-
log!("{:?}", ftyp);
680+
debug!("{:?}", ftyp);
697681
}
698682
BoxType::MovieBox => {
699683
read_moov(&mut b, context)?;
@@ -703,7 +687,7 @@ pub fn read_mp4<T: Read>(f: &mut T, context: &mut MediaContext) -> Result<()> {
703687
};
704688
check_parser_state!(b.content);
705689
if found_moov {
706-
log!("found moov {}, could stop pure 'moov' parser now", if found_ftyp {
690+
debug!("found moov {}, could stop pure 'moov' parser now", if found_ftyp {
707691
"and ftyp"
708692
} else {
709693
"but no ftyp"
@@ -737,7 +721,7 @@ fn read_moov<T: Read>(f: &mut BMFFBox<T>, context: &mut MediaContext) -> Result<
737721
BoxType::MovieHeaderBox => {
738722
let (mvhd, timescale) = parse_mvhd(&mut b)?;
739723
context.timescale = timescale;
740-
log!("{:?}", mvhd);
724+
debug!("{:?}", mvhd);
741725
}
742726
BoxType::TrackBox => {
743727
let mut track = Track::new(context.tracks.len());
@@ -746,12 +730,12 @@ fn read_moov<T: Read>(f: &mut BMFFBox<T>, context: &mut MediaContext) -> Result<
746730
}
747731
BoxType::MovieExtendsBox => {
748732
let mvex = read_mvex(&mut b)?;
749-
log!("{:?}", mvex);
733+
debug!("{:?}", mvex);
750734
context.mvex = Some(mvex);
751735
}
752736
BoxType::ProtectionSystemSpecificHeaderBox => {
753737
let pssh = read_pssh(&mut b)?;
754-
log!("{:?}", pssh);
738+
debug!("{:?}", pssh);
755739
vec_push(&mut context.psshs, pssh)?;
756740
}
757741
_ => skip_box_content(&mut b)?,
@@ -834,7 +818,7 @@ fn read_trak<T: Read>(f: &mut BMFFBox<T>, track: &mut Track) -> Result<()> {
834818
let tkhd = read_tkhd(&mut b)?;
835819
track.track_id = Some(tkhd.track_id);
836820
track.tkhd = Some(tkhd.clone());
837-
log!("{:?}", tkhd);
821+
debug!("{:?}", tkhd);
838822
}
839823
BoxType::EditBox => read_edts(&mut b, track)?,
840824
BoxType::MediaBox => read_mdia(&mut b, track)?,
@@ -869,7 +853,7 @@ fn read_edts<T: Read>(f: &mut BMFFBox<T>, track: &mut Track) -> Result<()> {
869853
}
870854
track.media_time = Some(TrackScaledTime::<u64>(elst.edits[idx].media_time as u64,
871855
track.id));
872-
log!("{:?}", elst);
856+
debug!("{:?}", elst);
873857
}
874858
_ => skip_box_content(&mut b)?,
875859
};
@@ -899,7 +883,7 @@ fn read_mdia<T: Read>(f: &mut BMFFBox<T>, track: &mut Track) -> Result<()> {
899883
let (mdhd, duration, timescale) = parse_mdhd(&mut b, track)?;
900884
track.duration = duration;
901885
track.timescale = timescale;
902-
log!("{:?}", mdhd);
886+
debug!("{:?}", mdhd);
903887
}
904888
BoxType::HandlerBox => {
905889
let hdlr = read_hdlr(&mut b)?;
@@ -909,7 +893,7 @@ fn read_mdia<T: Read>(f: &mut BMFFBox<T>, track: &mut Track) -> Result<()> {
909893
"soun" => track.track_type = TrackType::Audio,
910894
_ => (),
911895
}
912-
log!("{:?}", hdlr);
896+
debug!("{:?}", hdlr);
913897
}
914898
BoxType::MediaInformationBox => read_minf(&mut b, track)?,
915899
_ => skip_box_content(&mut b)?,
@@ -937,41 +921,41 @@ fn read_stbl<T: Read>(f: &mut BMFFBox<T>, track: &mut Track) -> Result<()> {
937921
match b.head.name {
938922
BoxType::SampleDescriptionBox => {
939923
let stsd = read_stsd(&mut b, track)?;
940-
log!("{:?}", stsd);
924+
debug!("{:?}", stsd);
941925
}
942926
BoxType::TimeToSampleBox => {
943927
let stts = read_stts(&mut b)?;
944-
log!("{:?}", stts);
928+
debug!("{:?}", stts);
945929
track.stts = Some(stts);
946930
}
947931
BoxType::SampleToChunkBox => {
948932
let stsc = read_stsc(&mut b)?;
949-
log!("{:?}", stsc);
933+
debug!("{:?}", stsc);
950934
track.stsc = Some(stsc);
951935
}
952936
BoxType::SampleSizeBox => {
953937
let stsz = read_stsz(&mut b)?;
954-
log!("{:?}", stsz);
938+
debug!("{:?}", stsz);
955939
track.stsz = Some(stsz);
956940
}
957941
BoxType::ChunkOffsetBox => {
958942
let stco = read_stco(&mut b)?;
959-
log!("{:?}", stco);
943+
debug!("{:?}", stco);
960944
track.stco = Some(stco);
961945
}
962946
BoxType::ChunkLargeOffsetBox => {
963947
let co64 = read_co64(&mut b)?;
964-
log!("{:?}", co64);
948+
debug!("{:?}", co64);
965949
track.stco = Some(co64);
966950
}
967951
BoxType::SyncSampleBox => {
968952
let stss = read_stss(&mut b)?;
969-
log!("{:?}", stss);
953+
debug!("{:?}", stss);
970954
track.stss = Some(stss);
971955
}
972956
BoxType::CompositionOffsetBox => {
973957
let ctts = read_ctts(&mut b)?;
974-
log!("{:?}", ctts);
958+
debug!("{:?}", ctts);
975959
track.ctts = Some(ctts);
976960
}
977961
_ => skip_box_content(&mut b)?,
@@ -1418,7 +1402,7 @@ fn find_descriptor(data: &[u8], esds: &mut ES_Descriptor) -> Result<()> {
14181402
read_ds_descriptor(descriptor, esds)?;
14191403
},
14201404
_ => {
1421-
log!("Unsupported descriptor, tag {}", tag);
1405+
debug!("Unsupported descriptor, tag {}", tag);
14221406
},
14231407
}
14241408

@@ -1471,7 +1455,7 @@ fn read_ds_descriptor(data: &[u8], esds: &mut ES_Descriptor) -> Result<()> {
14711455
// When channel_counts is 0, we need to parse the program_config_element
14721456
// to calculate the channel counts.
14731457
if channel_counts == 0 {
1474-
log!("Parsing program_config_element for channel counts");
1458+
debug!("Parsing program_config_element for channel counts");
14751459

14761460
bit_reader.skip(4)?; // element_instance_tag
14771461
bit_reader.skip(2)?; // object_type
@@ -1750,7 +1734,7 @@ fn read_video_sample_entry<T: Read>(src: &mut BMFFBox<T>) -> Result<(CodecType,
17501734
BoxType::VP9SampleEntry => CodecType::VP9,
17511735
BoxType::ProtectedVisualSampleEntry => CodecType::EncryptedVideo,
17521736
_ => {
1753-
log!("Unsupported video codec, box {:?} found", name);
1737+
debug!("Unsupported video codec, box {:?} found", name);
17541738
CodecType::Unknown
17551739
}
17561740
};
@@ -1784,7 +1768,7 @@ fn read_video_sample_entry<T: Read>(src: &mut BMFFBox<T>) -> Result<(CodecType,
17841768
}
17851769
let avcc_size = b.head.size - b.head.offset;
17861770
let avcc = read_buf(&mut b.content, avcc_size as usize)?;
1787-
log!("{:?} (avcc)", avcc);
1771+
debug!("{:?} (avcc)", avcc);
17881772
// TODO(kinetik): Parse avcC box? For now we just stash the data.
17891773
codec_specific = Some(VideoCodecSpecific::AVCConfig(avcc));
17901774
}
@@ -1812,11 +1796,11 @@ fn read_video_sample_entry<T: Read>(src: &mut BMFFBox<T>) -> Result<(CodecType,
18121796
return Err(Error::InvalidData("malformed video sample entry"));
18131797
}
18141798
let sinf = read_sinf(&mut b)?;
1815-
log!("{:?} (sinf)", sinf);
1799+
debug!("{:?} (sinf)", sinf);
18161800
vec_push(&mut protection_info, sinf)?;
18171801
}
18181802
_ => {
1819-
log!("Unsupported video codec, box {:?} found", b.head.name);
1803+
debug!("Unsupported video codec, box {:?} found", b.head.name);
18201804
skip_box_content(&mut b)?;
18211805
}
18221806
}
@@ -1950,12 +1934,12 @@ fn read_audio_sample_entry<T: Read>(src: &mut BMFFBox<T>) -> Result<(CodecType,
19501934
return Err(Error::InvalidData("malformed audio sample entry"));
19511935
}
19521936
let sinf = read_sinf(&mut b)?;
1953-
log!("{:?} (sinf)", sinf);
1937+
debug!("{:?} (sinf)", sinf);
19541938
codec_type = CodecType::EncryptedAudio;
19551939
vec_push(&mut protection_info, sinf)?;
19561940
}
19571941
_ => {
1958-
log!("Unsupported audio codec, box {:?} found", b.head.name);
1942+
debug!("Unsupported audio codec, box {:?} found", b.head.name);
19591943
skip_box_content(&mut b)?;
19601944
}
19611945
}
@@ -2008,7 +1992,7 @@ fn read_stsd<T: Read>(src: &mut BMFFBox<T>, track: &mut Track) -> Result<SampleD
20081992
if track.data.is_none() {
20091993
track.data = Some(description.clone());
20101994
} else {
2011-
log!("** don't know how to handle multiple descriptions **");
1995+
debug!("** don't know how to handle multiple descriptions **");
20121996
}
20131997
vec_push(&mut descriptions, description)?;
20141998
check_parser_state!(b.content);

mp4parse_capi/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ travis-ci = { repository = "https://github.com/mozilla/mp4parse-rust" }
2323

2424
[dependencies]
2525
byteorder = "1.0.0"
26+
env_logger = "0.5.3"
27+
log = "0.4"
2628

2729
# To enable fallible memory allocation, add 'features = ["mp4parse_fallible"]'
2830
# in mp4parse brace.

mp4parse_capi/examples/dump.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
extern crate mp4parse;
22
extern crate mp4parse_capi;
33

4+
#[macro_use]
5+
extern crate log;
6+
7+
extern crate env_logger;
8+
49
use std::env;
510
use std::fs::File;
611
use std::io::Read;
@@ -15,7 +20,7 @@ extern fn buf_read(buf: *mut u8, size: usize, userdata: *mut std::os::raw::c_voi
1520
}
1621
}
1722

18-
fn dump_file(filename: &str, verbose: bool) {
23+
fn dump_file(filename: &str) {
1924
let mut file = File::open(filename).expect("Unknown file");
2025
let io = Mp4parseIo {
2126
read: Some(buf_read),
@@ -25,10 +30,6 @@ fn dump_file(filename: &str, verbose: bool) {
2530
unsafe {
2631
let parser = mp4parse_new(&io);
2732

28-
if verbose {
29-
mp4parse_log(true);
30-
}
31-
3233
match mp4parse_read(parser) {
3334
Mp4parseStatus::Ok => (),
3435
_ => {
@@ -122,18 +123,23 @@ fn main() {
122123
if args.len() < 2 {
123124
return;
124125
}
126+
127+
// Initialize logging, setting the log level if requested.
125128
let (skip, verbose) = if args[1] == "-v" {
126129
(2, true)
127130
} else {
128131
(1, false)
129132
};
133+
let env = env_logger::Env::default();
134+
let mut logger = env_logger::Builder::from_env(env);
135+
if verbose {
136+
logger.filter(None, log::LevelFilter::Debug);
137+
}
138+
logger.init();
139+
130140
for filename in args.iter().skip(skip) {
131-
if verbose {
132-
println!("-- dump of '{}' --", filename);
133-
}
134-
dump_file(filename, verbose);
135-
if verbose {
136-
println!("-- end of '{}' --", filename);
137-
}
141+
info!("-- dump of '{}' --", filename);
142+
dump_file(filename);
143+
info!("-- end of '{}' --", filename);
138144
}
139145
}

mp4parse_capi/examples/test.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,13 @@ int main(int argc, char* argv[])
234234
std::remove_if(args.begin(), args.end(), [](std::string& arg){
235235
if (!arg.compare("-v")) {
236236
fprintf(stderr, "Enabling debug logging.\n");
237-
mp4parse_log(true);
237+
const char* LOG_ENV = "RUST_LOG";
238+
auto logger = std::string(getenv(LOG_ENV));
239+
if (!logger.empty()) {
240+
logger.append(",");
241+
}
242+
logger.append("debug");
243+
setenv(LOG_ENV, logger.c_str(), 1);
238244
return true;
239245
}
240246
return false;

mp4parse_capi/src/lib.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,6 @@ pub unsafe extern fn mp4parse_free(parser: *mut Mp4parseParser) {
298298
let _ = Box::from_raw(parser);
299299
}
300300

301-
/// Enable `mp4_parser` log.
302-
#[no_mangle]
303-
pub unsafe extern fn mp4parse_log(enable: bool) {
304-
mp4parse::set_debug_mode(enable);
305-
}
306-
307301
/// Run the `Mp4parseParser*` allocated by `mp4parse_new()` until EOF or error.
308302
#[no_mangle]
309303
pub unsafe extern fn mp4parse_read(parser: *mut Mp4parseParser) -> Mp4parseStatus {

0 commit comments

Comments
 (0)