Struct Logger

Source
pub struct Logger<T: Write> {
    pub options: Options,
    /* private fields */
}
Expand description

A modular and powerful logger

§Example

use rusty_logger::Logger;
use rusty_logger::types;

let mut logger = Logger::new("EXAMPLE", std::io::stdout());

logger.options.time_unit = types::TimeUnit::Nanoseconds;

logger.info("We will be timing a for loop");

logger.timer_start("for loop");

for i in 0..1000 {
    // Do something...
}

logger.timer_log_and_stop("for loop");

logger.info("The for loop ended successfully");

Fields§

§options: Options

Implementations§

Source§

impl<T: Write> Logger<T>

Source

pub fn new(s: &str, output: T) -> Self

Creates a new Logger with default values and the given name

§Example
use rusty_logger::Logger;

let mut logger = Logger::new("name", std::io::stdout());

logger.info("This is a new logger named 'name' !");
Source

pub fn homemade( output: T, options: Options, modules: Vec<Box<dyn Module>>, ) -> Logger<T>

Creates a new Logger with custom options and modules

§Example
use rusty_logger::{
    Logger,
    Module,
    Options,
    modules,
};

let options = Options::new("homemade logger");

let modules: Vec<Box<dyn Module>> = vec!(
    Box::new(modules::Name{}),
    Box::new(modules::Time{}),
    Box::new(modules::Type{})
);

let mut logger = Logger::homemade(std::io::stdout(), options, modules);

logger.info("This is a new logger named 'name' which has 'options' as its options !");
Source

pub fn static_log<D: Display>(msg: D, msg_type: LogType, output: T)

Static implementation for the log function

Mainly used to print internal error messages but can also be used by an end user

§Example
use rusty_logger::{Logger, types::LogType};

Logger::static_log("Printing on the fly", LogType::Info, std::io::stdout());
Source

pub fn info<D: Display>(&mut self, msg: D)

Logs the given message to the output as an information

§Example
use rusty_logger::Logger;

let mut logger = Logger::new("name", std::io::stdout());

logger.info("Here is some information I want to log");
Source

pub fn warn<D: Display>(&mut self, msg: D)

Logs the given message to the output as a warning

§Example
use rusty_logger::Logger;

let mut logger = Logger::new("name", std::io::stdout());

logger.warn("Here is a warning");
Source

pub fn error<D: Display>(&mut self, msg: D)

Logs the given message to the output as an error

§Example
use rusty_logger::Logger;

let mut logger = Logger::new("name", std::io::stdout());

logger.info("Some error happened");
Source

pub fn timer_start(&mut self, msg: &str)

Starts a new timer with the given name and the actual time

Source

pub fn timer_get(&self, msg: &str) -> Option<Duration>

Gets the duration from when the timer with the given name was started

Source

pub fn timer_stop(&mut self, msg: &str) -> Option<Duration>

Stops the timer with the given name and returns it as a duration

Source

pub fn timer_reset(&mut self, msg: &str) -> Option<Duration>

Resets the timer with the given name and returns the duration of the timer before being reset as a duration

Source

pub fn timer_pause(&mut self, msg: &str) -> Option<Duration>

Pauses the timer with the given name and returns the duration of the timer before pausing it as a duration

Source

pub fn timer_resume(&mut self, msg: &str) -> Option<Duration>

Resumes the timer with the given name

Source

pub fn timer_log(&mut self, msg: &str) -> Option<Duration>

Logs the time elapsed between the start/reset of the timer and now without reseting it

§Example
use rusty_logger::Logger;

let mut logger = Logger::new("name", std::io::stdout());

logger.timer_start("new_timer");

std::thread::sleep(std::time::Duration::from_millis(1));

logger.timer_log("new_timer");
Source

pub fn timer_log_and_stop(&mut self, msg: &str) -> Option<Duration>

Logs the time elapsed between the start/reset of the timer and now and then stops it

§Example
use rusty_logger::Logger;

let mut logger = Logger::new("name", std::io::stdout());

logger.timer_start("new_timer");

std::thread::sleep(std::time::Duration::from_millis(1));

logger.timer_log_and_stop("new_timer");
Source

pub fn timer_log_and_reset(&mut self, msg: &str) -> Option<Duration>

Logs the time elapsed between the start/reset of the timer and now and then resets it

§Example
use rusty_logger::Logger;

let mut logger = Logger::new("name", std::io::stdout());

logger.timer_start("new_timer");

std::thread::sleep(std::time::Duration::from_millis(1));

logger.timer_log_and_reset("new_timer");

std::thread::sleep(std::time::Duration::from_millis(1));

logger.timer_log_and_stop("new_timer");

Auto Trait Implementations§

§

impl<T> Freeze for Logger<T>
where T: Freeze,

§

impl<T> !RefUnwindSafe for Logger<T>

§

impl<T> !Send for Logger<T>

§

impl<T> !Sync for Logger<T>

§

impl<T> Unpin for Logger<T>
where T: Unpin,

§

impl<T> !UnwindSafe for Logger<T>

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.