From d3270133be3d3c5f58c93dfa65028e657192c796 Mon Sep 17 00:00:00 2001 From: Zhiming Wang Date: Mon, 3 Aug 2020 18:46:49 +0800 Subject: [PATCH] Add log::util module --- src/util/log/flag.rs | 9 ++++++++ src/util/log/level.rs | 54 +++++++++++++++++++++++++++++++++++++++++++ src/util/log/mod.rs | 24 +++++++++++++++++++ src/util/mod.rs | 1 + 4 files changed, 88 insertions(+) create mode 100644 src/util/log/flag.rs create mode 100644 src/util/log/level.rs create mode 100644 src/util/log/mod.rs diff --git a/src/util/log/flag.rs b/src/util/log/flag.rs new file mode 100644 index 0000000..fa26506 --- /dev/null +++ b/src/util/log/flag.rs @@ -0,0 +1,9 @@ +use ffi::*; +use libc::c_int; + +bitflags! { + pub struct Flags: c_int { + const SKIP_REPEATED = AV_LOG_SKIP_REPEATED; + const PRINT_LEVEL = AV_LOG_PRINT_LEVEL; + } +} diff --git a/src/util/log/level.rs b/src/util/log/level.rs new file mode 100644 index 0000000..c79aad7 --- /dev/null +++ b/src/util/log/level.rs @@ -0,0 +1,54 @@ +use std::convert::TryFrom; + +use ffi::*; +use libc::c_int; + +#[derive(Eq, PartialEq, Clone, Copy, Debug)] +pub enum Level { + Quiet, + Panic, + Fatal, + Error, + Warning, + Info, + Verbose, + Debug, + Trace, +} + +pub struct LevelError; + +impl TryFrom for Level { + type Error = &'static str; + + fn try_from(value: c_int) -> Result { + match value { + AV_LOG_QUIET => Ok(Level::Quiet), + AV_LOG_PANIC => Ok(Level::Panic), + AV_LOG_FATAL => Ok(Level::Fatal), + AV_LOG_ERROR => Ok(Level::Error), + AV_LOG_WARNING => Ok(Level::Warning), + AV_LOG_INFO => Ok(Level::Info), + AV_LOG_VERBOSE => Ok(Level::Verbose), + AV_LOG_DEBUG => Ok(Level::Debug), + AV_LOG_TRACE => Ok(Level::Trace), + _ => Err("illegal log level"), + } + } +} + +impl Into for Level { + fn into(self) -> c_int { + match self { + Level::Quiet => AV_LOG_QUIET, + Level::Panic => AV_LOG_PANIC, + Level::Fatal => AV_LOG_FATAL, + Level::Error => AV_LOG_ERROR, + Level::Warning => AV_LOG_WARNING, + Level::Info => AV_LOG_INFO, + Level::Verbose => AV_LOG_VERBOSE, + Level::Debug => AV_LOG_DEBUG, + Level::Trace => AV_LOG_TRACE, + } + } +} diff --git a/src/util/log/mod.rs b/src/util/log/mod.rs new file mode 100644 index 0000000..69bc4c7 --- /dev/null +++ b/src/util/log/mod.rs @@ -0,0 +1,24 @@ +pub mod level; +pub use self::level::Level; + +pub mod flag; +pub use self::flag::Flags; + +use ffi::*; +use std::convert::TryInto; + +pub fn set_level(value: Level) { + unsafe { av_log_set_level(value.into()) } +} + +pub fn get_level() -> Result { + unsafe { av_log_get_level().try_into() } +} + +pub fn set_flags(value: Flags) { + unsafe { av_log_set_flags(value.bits()) } +} + +pub fn get_flags() -> Flags { + unsafe { Flags::from_bits_truncate(av_log_get_flags()) } +} diff --git a/src/util/mod.rs b/src/util/mod.rs index 1a50077..1df872d 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -7,6 +7,7 @@ pub mod error; pub mod format; pub mod frame; pub mod interrupt; +pub mod log; pub mod mathematics; pub mod media; pub mod option;