Struct bp3d_logger::LogMsg

source ·
#[repr(C)]
pub struct LogMsg { /* private fields */ }
Expand description

A log message.

This structure uses a large 1K buffer which stores the entire log message to improve performance.

The repr(C) is used to force the control fields (msg_len, level and target_len) to be before the message buffer and avoid large movs when setting control fields.

§Examples

use bp3d_logger::{Level, Location, LogMsg};
use std::fmt::Write;
let mut msg = LogMsg::new(Location::new("test", "file.c", 1), Level::Info);
let _ = write!(msg, "This is a formatted message {}", 42);
assert_eq!(msg.msg(), "This is a formatted message 42");

Implementations§

source§

impl LogMsg

source

pub fn new(location: Location, level: Level) -> LogMsg

Creates a new instance of log message buffer.

§Arguments
  • location: the location this message comes from.
  • level: the Level of the log message.

returns: LogMsg

§Examples
use bp3d_logger::{Level, Location, LogMsg};
let msg = LogMsg::new(Location::new("test", "file.c", 1), Level::Info);
assert_eq!(msg.location().module_path(), "test");
assert_eq!(msg.level(), Level::Info);
source

pub fn with_time( location: Location, time: OffsetDateTime, level: Level, ) -> LogMsg

Creates a new instance of log message buffer.

§Arguments
  • location: the location this message comes from.
  • level: the Level of the log message.

returns: LogMsg

§Examples
use time::macros::datetime;
use bp3d_logger::{Level, Location, LogMsg};
let msg = LogMsg::with_time(Location::new("test", "file.c", 1), datetime!(1999-1-1 0:0 UTC), Level::Info);
assert_eq!(msg.location().module_path(), "test");
assert_eq!(msg.level(), Level::Info);
source

pub fn clear(&mut self)

Clears the log message but keep the target and the level.

§Examples
use bp3d_logger::{Level, Location, LogMsg};
let mut msg = LogMsg::from_msg(Location::new("test", "file.c", 1), Level::Info, "this is a test");
msg.clear();
assert_eq!(msg.msg(), "");
assert_eq!(msg.location().module_path(), "test");
assert_eq!(msg.level(), Level::Info);
source

pub fn set_time(&mut self, time: OffsetDateTime)

Replaces the time contained in this log message.

§Arguments

returns: ()

source

pub fn from_msg(location: Location, level: Level, msg: &str) -> LogMsg

Auto-creates a new log message with a pre-defined string message.

This function is the same as calling write after new.

§Arguments
  • target: the target name this log comes from.
  • level: the Level of the log message.
  • msg: the message string.

returns: LogMsg

§Examples
use bp3d_logger::{LogMsg, Level, Location};
let mut msg = LogMsg::from_msg(Location::new("test", "file.c", 1), Level::Info, "this is a test");
assert_eq!(msg.location().module_path(), "test");
assert_eq!(msg.level(), Level::Info);
assert_eq!(msg.msg(), "this is a test");
source

pub unsafe fn write(&mut self, buf: &[u8]) -> usize

Appends a raw byte buffer at the end of the message buffer.

Returns the number of bytes written.

§Arguments
  • buf: the raw byte buffer to append.

returns: usize

§Safety
  • LogMsg contains only valid UTF-8 strings so buf must contain only valid UTF-8 bytes.
  • If buf contains invalid UTF-8 bytes, further operations on the log message buffer may result in UB.
source

pub fn location(&self) -> &Location

Returns the location the log message comes from.

source

pub fn time(&self) -> &OffsetDateTime

Returns the time of this log message.

source

pub fn msg(&self) -> &str

Returns the log message as a string.

source

pub fn level(&self) -> Level

Returns the level of this log message.

Trait Implementations§

source§

impl Clone for LogMsg

source§

fn clone(&self) -> LogMsg

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Write for LogMsg

source§

fn write_str(&mut self, s: &str) -> Result<(), Error>

Writes a string slice into this writer, returning whether the write succeeded. Read more
1.1.0 · source§

fn write_char(&mut self, c: char) -> Result<(), Error>

Writes a char into this writer, returning whether the write succeeded. Read more
1.0.0 · source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Glue for usage of the write! macro with implementors of this trait. Read more

Auto Trait Implementations§

§

impl Freeze for LogMsg

§

impl RefUnwindSafe for LogMsg

§

impl Send for LogMsg

§

impl Sync for LogMsg

§

impl Unpin for LogMsg

§

impl UnwindSafe for LogMsg

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> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.