Struct bp3d_logger::Builder
source · pub struct Builder { /* private fields */ }Expand description
The base logger builder/initializer.
§Examples
The following example shows basic initialization of this logger.
use bp3d_logger::{Builder, Level, LogMsg, Location};
fn main() {
let logger = Builder::new().add_stdout().add_file("my-app").start();
logger.log(&LogMsg::from_msg(Location::new("bp3d-logger", "test.c", 1), Level::Info, "Example message"));
}The following example shows initialization of this logger and use of the log buffer.
use bp3d_logger::{Builder, Level, LogMsg, Location, LevelFilter, handler::{LogQueue, LogQueueHandler}};
fn main() {
let queue = LogQueue::default();
let logger = Builder::new().add_stdout().add_handler(LogQueueHandler::new(queue.clone())).start();
//... application code with log redirect pump.///
logger.log(&LogMsg::from_msg(Location::new("bp3d-logger", "test.c", 1), Level::Info, "Example message"));
logger.set_filter(LevelFilter::None);
logger.log(&LogMsg::from_msg(Location::new("bp3d-logger", "test.c", 1), Level::Info, "Dropped message"));
logger.raw_log(&LogMsg::from_msg(Location::new("bp3d-logger", "test.c", 1), Level::Info, "Example message 1"));
logger.set_filter(LevelFilter::Info);
logger.flush();
let l = queue.pop().unwrap(); // Capture the last log message.
// We can't test for equality because log messages contains a timestamp...
assert_eq!(l.msg(), "Example message");
let l = queue.pop().unwrap();
assert_eq!(l.msg(), "Example message 1");
//... application code without log redirect pump.
}Implementations§
source§impl Builder
impl Builder
sourcepub fn colors(self, state: Colors) -> Self
pub fn colors(self, state: Colors) -> Self
Sets the colors state when logging to stdout/stderr.
The default behavior is to disable colors.
sourcepub fn filter(self, filter: LevelFilter) -> Self
pub fn filter(self, filter: LevelFilter) -> Self
Sets the default level filter when initializing the logger.
The default is Info.
sourcepub fn smart_stderr(self, flag: bool) -> Self
pub fn smart_stderr(self, flag: bool) -> Self
Enables or disables automatic redirection of error logs to stderr.
The default for this flag is true.
sourcepub fn buffer_size(self, buf_size: usize) -> Self
pub fn buffer_size(self, buf_size: usize) -> Self
sourcepub fn add_handler<T: Handler + 'static>(self, handler: T) -> Self
pub fn add_handler<T: Handler + 'static>(self, handler: T) -> Self
sourcepub fn add_stdout(self) -> Self
pub fn add_stdout(self) -> Self
Enables stdout logging.
sourcepub fn add_file<T: GetLogs>(self, app: T) -> Self
pub fn add_file<T: GetLogs>(self, app: T) -> Self
Enables file logging to the given application.
The application is given as a reference to GetLogs to allow obtaining a log directory from various sources.
If the log directory could not be found the function prints an error to stderr.
sourcepub fn start(self) -> Logger
pub fn start(self) -> Logger
Initializes the log implementation with this current configuration.
NOTE: This returns an instance of Logger which is the main entry point for all logging based operations. This instance also acts as a guard to flush all log buffers before returning. It is necessary to flush log buffers because this implementation uses threads to avoid blocking the main thread when issuing logs.
NOTE 2: There are no safety concerns with running twice this function in the same application, only that calling this function may be slow due to thread management.