SimpleLogger API improvements, printing to stdout, timestamps (#1109)

* log

* fix

* a

* rev

* remove

* 2 logger

* cfg std

* more

* more

* cf

* no_std

* features

* optional

* rename

* current_time()
This commit is contained in:
Dongjia "toka" Zhang 2023-03-02 22:07:46 +09:00 committed by GitHub
parent 2a3f1d68f5
commit 5b4ae61cdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 91 additions and 56 deletions

View File

@ -13,7 +13,7 @@ use libafl::{
bolts::{ bolts::{
llmp::{self, Tag}, llmp::{self, Tag},
shmem::{ShMemProvider, StdShMemProvider}, shmem::{ShMemProvider, StdShMemProvider},
ClientId, SimpleStdErrLogger, ClientId, SimpleStderrLogger,
}, },
Error, Error,
}; };
@ -34,7 +34,7 @@ const BROKER_TIMEOUT: Duration = Duration::from_secs(10);
const SLEEP_BETWEEN_FORWARDS: Duration = Duration::from_millis(5); const SLEEP_BETWEEN_FORWARDS: Duration = Duration::from_millis(5);
#[cfg(feature = "std")] #[cfg(feature = "std")]
static LOGGER: SimpleStdErrLogger = SimpleStdErrLogger::debug(); static LOGGER: SimpleStderrLogger = SimpleStderrLogger::new();
#[cfg(feature = "std")] #[cfg(feature = "std")]
fn adder_loop(port: u16) -> Result<(), Box<dyn std::error::Error>> { fn adder_loop(port: u16) -> Result<(), Box<dyn std::error::Error>> {

View File

@ -36,6 +36,9 @@ use std::time::{SystemTime, UNIX_EPOCH};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[cfg(feature = "std")]
use crate::Error;
/// The client ID == the sender id. /// The client ID == the sender id.
#[repr(transparent)] #[repr(transparent)]
#[derive( #[derive(
@ -44,7 +47,7 @@ use serde::{Deserialize, Serialize};
pub struct ClientId(pub u32); pub struct ClientId(pub u32);
#[cfg(feature = "std")] #[cfg(feature = "std")]
use log::{Level, Metadata, Record}; use log::{Metadata, Record};
/// Can be converted to a slice /// Can be converted to a slice
pub trait AsSlice { pub trait AsSlice {
@ -239,73 +242,105 @@ where
} }
} }
/// Stderr logger
#[cfg(feature = "std")]
pub static LIBAFL_STDERR_LOGGER: SimpleStderrLogger = SimpleStderrLogger::new();
/// Stdout logger
#[cfg(feature = "std")]
pub static LIBAFL_STDOUT_LOGGER: SimpleStdoutLogger = SimpleStdoutLogger::new();
/// A simple logger struct that logs to stderr when used with [`log::set_logger`]. /// A simple logger struct that logs to stderr when used with [`log::set_logger`].
#[derive(Debug)] #[derive(Debug)]
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub struct SimpleStdErrLogger { pub struct SimpleStdoutLogger {}
/// The min log level for which this logger will write messages.
pub log_level: Level,
}
#[cfg(feature = "std")] #[cfg(feature = "std")]
impl SimpleStdErrLogger { impl Default for SimpleStdoutLogger {
/// Create a new [`log::Log`] logger that will log [`Level::Trace`] and above fn default() -> Self {
#[must_use] Self::new()
pub const fn trace() -> Self {
Self {
log_level: Level::Trace,
}
}
/// Create a new [`log::Log`] logger that will log [`Level::Debug`] and above
#[must_use]
pub const fn debug() -> Self {
Self {
log_level: Level::Debug,
}
}
/// Create a new [`log::Log`] logger that will log [`Level::Info`] and above
#[must_use]
pub const fn info() -> Self {
Self {
log_level: Level::Info,
}
}
/// Create a new [`log::Log`] logger that will log [`Level::Warn`] and above
#[must_use]
pub const fn warn() -> Self {
Self {
log_level: Level::Warn,
}
}
/// Create a new [`log::Log`] logger that will log [`Level::Error`]
#[must_use]
pub const fn error() -> Self {
Self {
log_level: Level::Error,
}
} }
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
impl log::Log for SimpleStdErrLogger { impl SimpleStdoutLogger {
/// Create a new [`log::Log`] logger that will wrte log to stdout
#[must_use]
pub const fn new() -> Self {
Self {}
}
/// register stdout logger
pub fn set_logger() -> Result<(), Error> {
log::set_logger(&LIBAFL_STDOUT_LOGGER)
.map_err(|_| Error::unknown("Failed to register logger"))
}
}
#[cfg(feature = "std")]
impl log::Log for SimpleStdoutLogger {
#[inline] #[inline]
fn enabled(&self, metadata: &Metadata) -> bool { fn enabled(&self, _metadata: &Metadata) -> bool {
metadata.level() <= self.log_level true
} }
fn log(&self, record: &Record) { fn log(&self, record: &Record) {
if self.enabled(record.metadata()) { println!(
eprintln!("{}: {}", record.level(), record.args()); "[{:?}] {}: {}",
} current_time(),
record.level(),
record.args()
);
} }
fn flush(&self) {} fn flush(&self) {}
} }
/// A simple logger struct that logs to stderr when used with [`log::set_logger`].
#[derive(Debug)]
#[cfg(feature = "std")]
pub struct SimpleStderrLogger {}
#[cfg(feature = "std")]
impl Default for SimpleStderrLogger {
fn default() -> Self {
Self::new()
}
}
#[cfg(feature = "std")]
impl SimpleStderrLogger {
/// Create a new [`log::Log`] logger that will wrte log to stdout
#[must_use]
pub const fn new() -> Self {
Self {}
}
/// register stderr logger
pub fn set_logger() -> Result<(), Error> {
log::set_logger(&LIBAFL_STDERR_LOGGER)
.map_err(|_| Error::unknown("Failed to register logger"))
}
}
#[cfg(feature = "std")]
impl log::Log for SimpleStderrLogger {
#[inline]
fn enabled(&self, _metadata: &Metadata) -> bool {
true
}
fn log(&self, record: &Record) {
eprintln!(
"[{:?}] {}: {}",
current_time(),
record.level(),
record.args()
);
}
fn flush(&self) {}
}
/// The purpose of this module is to alleviate imports of the bolts by adding a glob import. /// The purpose of this module is to alleviate imports of the bolts by adding a glob import.
#[cfg(feature = "prelude")] #[cfg(feature = "prelude")]
pub mod bolts_prelude { pub mod bolts_prelude {

View File

@ -168,7 +168,7 @@ pub trait CompilerWrapper {
let args = self.command()?; let args = self.command()?;
if !self.is_silent() { if !self.is_silent() {
log::info!("{args:?}"); dbg!("{args:?}");
} }
if args.is_empty() { if args.is_empty() {
return Err(Error::InvalidArguments( return Err(Error::InvalidArguments(
@ -180,7 +180,7 @@ pub trait CompilerWrapper {
Err(e) => return Err(Error::Io(e)), Err(e) => return Err(Error::Io(e)),
}; };
if !self.is_silent() { if !self.is_silent() {
log::info!("{status:?}"); dbg!("{status:?}");
} }
Ok(status.code()) Ok(status.code())
} }

View File

@ -34,7 +34,7 @@ mod clone {
cmd.arg("checkout").arg(commit).current_dir(path); cmd.arg("checkout").arg(commit).current_dir(path);
let output = cmd.output().expect("failed to execute git checkout"); let output = cmd.output().expect("failed to execute git checkout");
if !output.status.success() { if !output.status.success() {
log::error!("failed to checkout symcc git repository commit:"); eprintln!("failed to checkout symcc git repository commit:");
let mut stdout = stdout(); let mut stdout = stdout();
stdout stdout
.write_all(&output.stderr) .write_all(&output.stderr)
@ -42,7 +42,7 @@ mod clone {
panic!(); panic!();
} }
} else { } else {
log::error!("failed to clone symcc git repository:"); eprintln!("failed to clone symcc git repository:");
let mut stdout = stdout(); let mut stdout = stdout();
stdout stdout
.write_all(&output.stderr) .write_all(&output.stderr)