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,

source

pub fn new(inner: D, log: LogFunc) -> Self

Creates a new custom LogDiskit

Creates a new LogDiskit with the given inner diskit and logging function.

source

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 LogDiskits.

source

pub fn set_log_func(&mut self, log: LogFunc)

Sets the logging function

Changes the logging function to the provided one.

source

pub fn set_log_func_raw(&mut self, log: Arc<Mutex<LogFunc>>)

Sets the logging function with a reference to a logging function

Changes the logging function to the provided one allowing to share one function between multiple LogDiskits.

Trait Implementations§

source§

impl<D> Clone for LogDiskit<D>
where D: Diskit + Clone,

source§

fn clone(&self) -> LogDiskit<D>

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<D> Default for LogDiskit<D>
where D: Diskit,

source§

fn default() -> Self

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

impl<D> Diskit for LogDiskit<D>
where D: Diskit,

source§

fn set_pwd_inner(&self, path: &Path) -> Result<(), Error>

Changes the current working directory Read more
source§

fn get_pwd(&self) -> Result<PathBuf, Error>

Retrieves the current working directory Read more
source§

fn open_inner(&self, path: &Path) -> Result<File<Self>, Error>

Opens an exisitent file Read more
source§

fn create_inner(&self, path: &Path) -> Result<File<Self>, Error>

Creates a new file or truncates it Read more
source§

fn open_with_options_inner( &self, path: &Path, options: OpenOptions ) -> Result<File<Self>, Error>

Opens a file with custom options Read more
source§

fn read_inner(&self, file: &FileInner, buf: &mut [u8]) -> Result<usize, Error>

Reads as much as possible into the provided buffer Read more
source§

fn read_to_end_inner( &self, file: &mut FileInner, buf: &mut Vec<u8> ) -> Result<usize, Error>

Reads the complete file into the provided buffer Read more
source§

fn read_to_string_inner( &self, file: &mut FileInner, buf: &mut String ) -> Result<usize, Error>

Reads the complete file into the provided string Read more
source§

fn write_inner(&self, file: &mut FileInner, buf: &[u8]) -> Result<usize, Error>

Writes as much as possible from the provided buffer Read more
source§

fn write_all_inner(&self, file: &mut FileInner, buf: &[u8]) -> Result<(), Error>

Writes the complete provided buufer into the file Read more
source§

fn flush_inner(&self, file: &mut FileInner) -> Result<(), Error>

Flushes all writes of the file Read more
source§

fn metadata_inner(&self, file: &FileInner) -> Result<Metadata, Error>

Retrieves the metadata of the file Read more
source§

fn seek_inner(&self, file: &mut FileInner, pos: SeekFrom) -> Result<u64, Error>

Repositions the file’s cursor Read more
source§

fn create_dir_inner(&self, path: &Path) -> Result<(), Error>

Creates a directory Read more
source§

fn create_dir_all_inner(&self, path: &Path) -> Result<(), Error>

Creates a directory and all above if necessary Read more
source§

fn close_inner(&self, file: FileInner) -> Result<(), Error>

Closes the file Read more
source§

fn walkdir_inner(&self, path: &Path) -> WalkDir<Self>

Creates a Walkdir from a path Read more
source§

fn into_walkdir_iterator(&self, walkdir: WalkDir<Self>) -> WalkdirIterator<Self>

Converts a walkdir builder to an iterator Read more
source§

fn walkdir_next_inner( &self, inner: &mut WalkdirIteratorInner ) -> Option<Result<DirEntry, Error>>

Gets the next file in a WalkDir Read more

Auto Trait Implementations§

§

impl<D> RefUnwindSafe for LogDiskit<D>
where D: RefUnwindSafe,

§

impl<D> Send for LogDiskit<D>
where D: Send,

§

impl<D> Sync for LogDiskit<D>
where D: Sync,

§

impl<D> Unpin for LogDiskit<D>
where D: Unpin,

§

impl<D> UnwindSafe for LogDiskit<D>
where D: UnwindSafe,

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> DiskitExt for T
where T: Diskit,

source§

fn set_pwd<P: AsRef<Path>>(&self, path: P) -> Result<(), Error>

Changes the current working directory Read more
source§

fn open<P: AsRef<Path>>(&self, path: P) -> Result<File<Self>, Error>

Changes the current working directory Read more
source§

fn create<P: AsRef<Path>>(&self, path: P) -> Result<File<Self>, Error>

Creates a new file or truncates it Read more
source§

fn open_with_options<P: AsRef<Path>>( &self, path: P, options: OpenOptions ) -> Result<File<Self>, Error>

Opens a file with custom options Read more
source§

fn create_dir<P: AsRef<Path>>(&self, path: P) -> Result<(), Error>

Creates a directory Read more
source§

fn create_dir_all<P: AsRef<Path>>(&self, path: P) -> Result<(), Error>

Creates a directory and all above if necessary Read more
source§

fn walkdir<P: AsRef<Path>>(&self, path: P) -> WalkDir<Self>

Creates a Walkdir from a path Read more
source§

fn read_to_end<P: AsRef<Path>>(&self, path: P) -> Result<Vec<u8>, Error>

Reads file to end Read more
source§

fn read_to_string<P: AsRef<Path>>(&self, path: P) -> Result<String, Error>

Reads file to end in string 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.