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

source

pub fn new() -> Builder

Creates a new instance of a logger builder.

source

pub fn colors(self, state: Colors) -> Self

Sets the colors state when logging to stdout/stderr.

The default behavior is to disable colors.

source

pub fn filter(self, filter: LevelFilter) -> Self

Sets the default level filter when initializing the logger.

The default is Info.

source

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.

source

pub fn buffer_size(self, buf_size: usize) -> Self

Sets the buffer size.

§Arguments
  • buf_size: the buffer size.

returns: Builder

source

pub fn add_handler<T: Handler + 'static>(self, handler: T) -> Self

Adds a new log Handler.

§Arguments
  • handler: the new handler implementation to add.

returns: Builder

source

pub fn add_stdout(self) -> Self

Enables stdout logging.

source

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.

source

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.

Trait Implementations§

source§

impl Default for Builder

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.