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
impl LogMsg
sourcepub fn new(location: Location, level: Level) -> LogMsg
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);sourcepub fn with_time(
location: Location,
time: OffsetDateTime,
level: Level,
) -> LogMsg
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);sourcepub fn clear(&mut self)
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);sourcepub fn set_time(&mut self, time: OffsetDateTime)
pub fn set_time(&mut self, time: OffsetDateTime)
Replaces the time contained in this log message.
§Arguments
time: the new OffsetDateTime.
returns: ()
sourcepub fn from_msg(location: Location, level: Level, msg: &str) -> LogMsg
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");sourcepub unsafe fn write(&mut self, buf: &[u8]) -> usize
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.
sourcepub fn time(&self) -> &OffsetDateTime
pub fn time(&self) -> &OffsetDateTime
Returns the time of this log message.
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)