use lief_ffi as ffi;
use std::convert::{From, Into};
use std::path::Path;
#[allow(non_camel_case_types)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Level {
OFF,
TRACE,
DEBUG,
INFO,
WARN,
ERR,
CRITICAL,
UNKNOWN(u32),
}
impl From<u32> for Level {
fn from(value: u32) -> Self {
match value {
0x00000000 => Level::OFF,
0x00000001 => Level::TRACE,
0x00000002 => Level::DEBUG,
0x00000003 => Level::INFO,
0x00000004 => Level::WARN,
0x00000005 => Level::ERR,
0x00000006 => Level::CRITICAL,
_ => Level::UNKNOWN(value),
}
}
}
impl From<Level> for u32 {
fn from(value: Level) -> Self {
match value {
Level::OFF => 0x00000000,
Level::TRACE => 0x00000001,
Level::DEBUG => 0x00000002,
Level::INFO => 0x00000003,
Level::WARN => 0x00000004,
Level::ERR => 0x00000005,
Level::CRITICAL => 0x00000006,
Level::UNKNOWN(_) => 0x00000003, }
}
}
pub fn reset() {
ffi::LIEF_Logging::reset()
}
pub fn disable() {
ffi::LIEF_Logging::disable()
}
pub fn enable() {
ffi::LIEF_Logging::enable()
}
pub fn set_level(level: Level) {
ffi::LIEF_Logging::set_level(level.into())
}
pub fn set_path<P: AsRef<Path>>(path: P) {
cxx::let_cxx_string!(p = path.as_ref().to_str().expect("Can't convert into string"));
ffi::LIEF_Logging::set_path(&p);
}
pub fn log(level: Level, message: &str) {
cxx::let_cxx_string!(msg = message);
ffi::LIEF_Logging::log(level.into(), &msg);
}
pub fn get_level() -> Level {
Level::from(ffi::LIEF_Logging::get_level())
}
pub struct Scoped {
inner: cxx::UniquePtr<ffi::LIEF_Logging_Scoped>,
}
impl Scoped {
pub fn new(level: Level) -> Self {
Self {
inner: ffi::LIEF_Logging_Scoped::create(level.into()),
}
}
pub fn set_level(&self, level: Level) {
self.inner.set_level(level.into())
}
pub fn reset(&mut self) {
self.inner.pin_mut().reset()
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! __lief_log {
($level: expr) => {
lief::logging::log($level, "")
};
($level: expr, $($arg:tt)*) => {{
lief::logging::log($level, &format!($($arg)*));
}};
}
#[macro_export]
macro_rules! log_dbg {
($($args:tt)*) => {
$crate::__lief_log!(lief::logging::Level::DEBUG, $($args)*)
};
}
#[macro_export]
macro_rules! log_info {
($($args:tt)*) => {
$crate::__lief_log!(lief::logging::Level::INFO, $($args)*)
};
}
#[macro_export]
macro_rules! log_warn {
($($args:tt)*) => {
$crate::__lief_log!(lief::logging::Level::WARN, $($args)*)
};
}
#[macro_export]
macro_rules! log_err {
($($args:tt)*) => {
$crate::__lief_log!(lief::logging::Level::ERR, $($args)*)
};
}