pub struct FileStream { /* private fields */ }Expand description
Used to output logs to a file.
§Examples
Writing a log to a file:
// Required by `FileStream` for parsing logs
let mut formatter = LogFormatter::default();
let mut file_output = FileStream::default();
// Set the log file path **first**
file_output.set_log_file_path(&path)
.expect("Failed to set the log file path!");
// Enable the output
file_output.enable()
.expect("Failed to enable the output!");
// Write to the log file buffer
file_output.out(&LogStruct::debug("Hello from file!"), &mut formatter)
.expect("Failed to write to the buffer!");
// Flush the logs from the buffer to the log file
file_output.flush();Implementations§
Source§impl FileStream
impl FileStream
Sourcepub fn set_log_file_path(&mut self, path: &str) -> Result<(), Error>
pub fn set_log_file_path(&mut self, path: &str) -> Result<(), Error>
Sets the log file path.
§Examples
// Set the log file path **first**
file_output.set_log_file_path(&path)
.expect("Failed to set the log file path!");
// And then enable the output
file_output.enable()
.expect("Failed to enable the output!");Sourcepub fn out(
&mut self,
log: &LogStruct,
formatter: &mut LogFormatter,
) -> Result<(), Error>
pub fn out( &mut self, log: &LogStruct, formatter: &mut LogFormatter, ) -> Result<(), Error>
Formats the given log using a formatter and stores it in a buffer until it is flushed.
§Examples
// Set the log file path **first**
file_output.set_log_file_path(&path)
.expect("Failed to set the log file path!");
// And then enable the output
file_output.enable()
.expect("Failed to enable the output!");
// Write to the buffer 100 times
for i in 0..100 {
file_output.out(&LogStruct::debug(&format!("Log number {}", i)),
&mut formatter).expect("Failed to write to the buffer!");
}
// Write the log buffer contents to the log file
file_output.flush();Sourcepub fn flush(&mut self) -> Result<(), Error>
pub fn flush(&mut self) -> Result<(), Error>
Flush the contents of the log buffer to the log file.
§Examples
// Set the log file path **first**
file_output.set_log_file_path(&path)
.expect("Failed to set the log file path!");
// And then enable the output
file_output.enable()
.expect("Failed to enable the output!");
file_output.out(&LogStruct::debug(&format!("Hello from file!")),
&mut formatter).expect("Failed to write to the buffer!");
// Write the log buffer contents to the log file
file_output.flush();Sourcepub fn set_max_buffer_size<I: Into<Option<usize>>>(&mut self, size: I)
pub fn set_max_buffer_size<I: Into<Option<usize>>>(&mut self, size: I)
Sets the maximum size of the log buffer.
When the buffer exceeds this size, its contents are written to a file and then cleared.
§Examples
// Set the log file path **first**
file_output.set_log_file_path(&path)
.expect("Failed to set the log file path!");
// And then enable the output
file_output.enable()
.expect("Failed to enable the output!");
// Define the maximum buffer size
let max_size = 128;
file_output.set_max_buffer_size(Some(max_size));
for i in 0..max_size {
// Write to the buffer
file_output.out(&LogStruct::debug(&format!("Log number {}", i)),
&mut formatter).expect("Failed to write to the buffer!");
}
// Here the buffer will be flushed to the log file.Sourcepub fn enable(&mut self) -> Result<(), Error>
pub fn enable(&mut self) -> Result<(), Error>
Enables the output.
Returns an error if the log file is not writable.
§Examples
// Set the log file path **first**
file_output.set_log_file_path(&path)
.expect("Failed to set the log file path!");
// And then enable the output
file_output.enable()
.expect("Failed to enable the output!");Sourcepub fn set_on_drop_policy<I: Into<OnDropPolicy>>(&mut self, policy: I)
pub fn set_on_drop_policy<I: Into<OnDropPolicy>>(&mut self, policy: I)
Sets the policy for handling the log buffer lock when the stream is dropped.
Sourcepub fn unlock_file(&mut self)
pub fn unlock_file(&mut self)
Unlocks the log file, allowing the stream to write to it.
Sourcepub fn is_enabled(&self) -> &bool
pub fn is_enabled(&self) -> &bool
Returns whether the output is enabled.
Trait Implementations§
Source§impl Clone for FileStream
impl Clone for FileStream
Source§fn clone(&self) -> FileStream
fn clone(&self) -> FileStream
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for FileStream
impl Debug for FileStream
Source§impl Default for FileStream
impl Default for FileStream
Source§impl<'de> Deserialize<'de> for FileStream
impl<'de> Deserialize<'de> for FileStream
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl Drop for FileStream
impl Drop for FileStream
Source§impl Hash for FileStream
impl Hash for FileStream
Source§impl Ord for FileStream
impl Ord for FileStream
Source§fn cmp(&self, other: &FileStream) -> Ordering
fn cmp(&self, other: &FileStream) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl PartialEq for FileStream
impl PartialEq for FileStream
Source§impl PartialOrd for FileStream
impl PartialOrd for FileStream
Source§impl Serialize for FileStream
impl Serialize for FileStream
impl Eq for FileStream
impl StructuralPartialEq for FileStream
Auto Trait Implementations§
impl Freeze for FileStream
impl RefUnwindSafe for FileStream
impl Send for FileStream
impl Sync for FileStream
impl Unpin for FileStream
impl UnwindSafe for FileStream
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
Mutably borrows from an owned value. Read more