ToastLoggerBuilder

Struct ToastLoggerBuilder 

Source
pub struct ToastLoggerBuilder { /* private fields */ }
Expand description

Builder for ToastLogger.

§Examples

ToastLogger::builder().init()?;

Implementations§

Source§

impl ToastLoggerBuilder

Source

pub fn init(&mut self) -> Result<()>

Initialize the log crate to use the ToastLogger with the configurations set to this builder.

Examples found in repository?
examples/multi.rs (line 8)
5pub fn main() -> Result<()> {
6    ToastLogger::builder()
7        .max_level(log::LevelFilter::Info)
8        .init()?;
9
10    for arg in env::args().skip(1) {
11        log::info!("{}", arg);
12    }
13    Ok(())
14}
More examples
Hide additional examples
examples/echo.rs (line 8)
5pub fn main() -> Result<()> {
6    ToastLogger::builder()
7        .max_level(log::LevelFilter::Info)
8        .init()?;
9
10    let args: Vec<String> = env::args().skip(1).collect();
11    log::info!("{}", args.join("\n"));
12    Ok(())
13}
examples/auto_flush.rs (line 9)
5pub fn main() -> Result<()> {
6    ToastLogger::builder()
7        .max_level(log::LevelFilter::Info)
8        .auto_flush(false)
9        .init()?;
10
11    for arg in env::args().skip(1) {
12        log::info!("{}", arg);
13    }
14    ToastLogger::flush()?;
15    Ok(())
16}
examples/expire.rs (line 25)
5pub fn main() -> anyhow::Result<()> {
6    let args: Vec<String> = env::args().skip(1).collect();
7    let duration = if args.is_empty() {
8        Duration::ZERO
9    } else {
10        Duration::from_secs(args[0].parse()?)
11    };
12
13    let mut builder = ToastLogger::builder();
14    let message = if duration.is_zero() {
15        "This message shouldn't expire".into()
16    } else {
17        let message = format!("This message should expire in {duration:?}.");
18        builder.create_notification(move |records| {
19            let mut notification = Notification::new_with_records(records)?;
20            notification.expires_in(duration)?;
21            Ok(notification)
22        });
23        message
24    };
25    builder.max_level(log::LevelFilter::Info).init()?;
26
27    log::info!("{}", message);
28    Ok(())
29}
Source

pub fn init_logger(&mut self) -> Result<()>

👎Deprecated since 0.2.0: Use init() instead
Source

pub fn build(&mut self) -> Result<ToastLogger>

Build a ToastLogger.

The returned logger implements the Log trait and can be installed manually or nested within another logger.

Source

pub fn max_level(&mut self, level: LevelFilter) -> &mut Self

Set the maximum level of logs to be displayed. Logs above the specified level are discarded.

Examples found in repository?
examples/multi.rs (line 7)
5pub fn main() -> Result<()> {
6    ToastLogger::builder()
7        .max_level(log::LevelFilter::Info)
8        .init()?;
9
10    for arg in env::args().skip(1) {
11        log::info!("{}", arg);
12    }
13    Ok(())
14}
More examples
Hide additional examples
examples/echo.rs (line 7)
5pub fn main() -> Result<()> {
6    ToastLogger::builder()
7        .max_level(log::LevelFilter::Info)
8        .init()?;
9
10    let args: Vec<String> = env::args().skip(1).collect();
11    log::info!("{}", args.join("\n"));
12    Ok(())
13}
examples/auto_flush.rs (line 7)
5pub fn main() -> Result<()> {
6    ToastLogger::builder()
7        .max_level(log::LevelFilter::Info)
8        .auto_flush(false)
9        .init()?;
10
11    for arg in env::args().skip(1) {
12        log::info!("{}", arg);
13    }
14    ToastLogger::flush()?;
15    Ok(())
16}
examples/expire.rs (line 25)
5pub fn main() -> anyhow::Result<()> {
6    let args: Vec<String> = env::args().skip(1).collect();
7    let duration = if args.is_empty() {
8        Duration::ZERO
9    } else {
10        Duration::from_secs(args[0].parse()?)
11    };
12
13    let mut builder = ToastLogger::builder();
14    let message = if duration.is_zero() {
15        "This message shouldn't expire".into()
16    } else {
17        let message = format!("This message should expire in {duration:?}.");
18        builder.create_notification(move |records| {
19            let mut notification = Notification::new_with_records(records)?;
20            notification.expires_in(duration)?;
21            Ok(notification)
22        });
23        message
24    };
25    builder.max_level(log::LevelFilter::Info).init()?;
26
27    log::info!("{}", message);
28    Ok(())
29}
Source

pub fn auto_flush(&mut self, is_auto_flush: bool) -> &mut Self

Set whether to show a toast notification on each logging, or only when explicitly specified. When this is set to false, logs are appended to an internal buffer without being shown, until ToastLogger::flush() is called.

The default value is true, which shows a toast notification on each logging.

§Examples
ToastLogger::builder()
    .max_level(log::LevelFilter::Info)
    .auto_flush(false)
    .init()?;
log::info!("Test info log");
log::info!("Test info log 2");
ToastLogger::flush()?;  // Shows only one notification with both logs.
Examples found in repository?
examples/auto_flush.rs (line 8)
5pub fn main() -> Result<()> {
6    ToastLogger::builder()
7        .max_level(log::LevelFilter::Info)
8        .auto_flush(false)
9        .init()?;
10
11    for arg in env::args().skip(1) {
12        log::info!("{}", arg);
13    }
14    ToastLogger::flush()?;
15    Ok(())
16}
Source

pub fn application_id(&mut self, application_id: &str) -> &mut Self

Set the application ID for the Toast Notification.

This is the application ID passed to the Windows CreateToastNotifier API. Please also see the Application User Model ID, and the “Find the Application User Model ID of an installed app”.

Source

pub fn format<F>(&mut self, formatter: F) -> &mut Self
where F: Fn(&mut dyn Write, &Record<'_>) -> Result + Send + Sync + 'static,

Set a custom formatter function that writes log::Record to fmt::Write.

The default formatter writes the logs with their levels as prefixes.

§Examples
ToastLogger::builder()
    .format(|buf: &mut dyn fmt::Write, record: &log::Record| {
        match record.level() {
            log::Level::Info => buf.write_fmt(*record.args()),
            _ => write!(buf, "{}: {}", record.level(), record.args()),
        }
    })
    .init()?;
Source

pub fn create_notification<F>(&mut self, create: F) -> &mut Self
where F: Fn(&[BufferedRecord]) -> Result<Notification> + Send + Sync + 'static,

Set a custom function to create the Notification.

§Examples
let builder = ToastLogger::builder()
    .create_notification(|records: &[BufferedRecord]| {
        let notification = Notification::new_with_records(records);
        // Change properties of `notification` as needed.
        notification
    });
Examples found in repository?
examples/expire.rs (lines 18-22)
5pub fn main() -> anyhow::Result<()> {
6    let args: Vec<String> = env::args().skip(1).collect();
7    let duration = if args.is_empty() {
8        Duration::ZERO
9    } else {
10        Duration::from_secs(args[0].parse()?)
11    };
12
13    let mut builder = ToastLogger::builder();
14    let message = if duration.is_zero() {
15        "This message shouldn't expire".into()
16    } else {
17        let message = format!("This message should expire in {duration:?}.");
18        builder.create_notification(move |records| {
19            let mut notification = Notification::new_with_records(records)?;
20            notification.expires_in(duration)?;
21            Ok(notification)
22        });
23        message
24    };
25    builder.max_level(log::LevelFilter::Info).init()?;
26
27    log::info!("{}", message);
28    Ok(())
29}

Trait Implementations§

Source§

impl Default for ToastLoggerBuilder

Source§

fn default() -> ToastLoggerBuilder

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

Auto Trait Implementations§

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.
Source§

impl<T> ErasedDestructor for T
where T: 'static,