Struct diskit::log_diskit::LogDiskit
source · pub struct LogDiskit<D>where
D: Diskit,{
pub inner: D,
pub log: Arc<Mutex<LogFunc>>,
}
Expand description
Logging diskit
This diskit extensively logs all requests it gets and passes them
to an other diskit (like StdDiskit
or
VirtualDiskit
) to satisfy.
Every request is logged before an operation is performed and after
– in a style depending on whether it was successful or not – it
completed by passing a String
to the logging
function (the default logging
function prints them to
stdout
).
use diskit::{diskit_extend::DiskitExt, log_diskit, LogDiskit, VirtualDiskit};
use std::{
io::Write,
sync::{Arc, Mutex},
};
let output = Arc::new(Mutex::new(String::new()));
let diskit = LogDiskit::new(
VirtualDiskit::default(),
log_diskit::get_log_in_buf_func(output.clone()),
);
let mut file = diskit.create("test.txt")?;
file.write_all(b"Hello, World!")?;
file.close()?;
assert_eq!(*output.lock().unwrap(),
"Creating \"test.txt\".
Created \"test.txt\": FileInner { file: None, val: 0 }.
Writing all of [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33] into FileInner { file: None, val: 0 }.
Wrote all of [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33] in FileInner { file: None, val: 0 }.
Closing FileInner { file: None, val: 0 }.
Closed a file.
");
You can write your own logging function or use one of the two
predefined ones: get_standard_log_func
and
get_log_in_buf_func
.
Fields§
§inner: D
§log: Arc<Mutex<LogFunc>>
Implementations§
source§impl<D> LogDiskit<D>where
D: Diskit,
impl<D> LogDiskit<D>where
D: Diskit,
sourcepub fn new_raw(inner: D, log: Arc<Mutex<LogFunc>>) -> Self
pub fn new_raw(inner: D, log: Arc<Mutex<LogFunc>>) -> Self
Creates a new custom LogDiskit
with a reference to a logging function
Creates a new LogDiskit
with the given inner diskit and an
Arc<Mutex<_>>
of the logging function. This is useful if
you want to share one function between two LogDiskit
s.
sourcepub fn set_log_func(&mut self, log: LogFunc)
pub fn set_log_func(&mut self, log: LogFunc)
Sets the logging function
Changes the logging function to the provided one.