pub struct ToastLoggerBuilder { /* private fields */ }Expand description
Implementations§
Source§impl ToastLoggerBuilder
impl ToastLoggerBuilder
Sourcepub fn init(&mut self) -> Result<()>
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?
More examples
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}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}pub fn init_logger(&mut self) -> Result<()>
init() insteadSourcepub fn build(&mut self) -> Result<ToastLogger>
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.
Sourcepub fn max_level(&mut self, level: LevelFilter) -> &mut Self
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?
More examples
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}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}Sourcepub fn auto_flush(&mut self, is_auto_flush: bool) -> &mut Self
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?
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}Sourcepub fn application_id(&mut self, application_id: &str) -> &mut Self
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”.
Sourcepub fn format<F>(&mut self, formatter: F) -> &mut Self
pub fn format<F>(&mut self, formatter: F) -> &mut Self
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()?;Sourcepub fn create_notification<F>(&mut self, create: F) -> &mut Self
pub fn create_notification<F>(&mut self, create: F) -> &mut Self
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?
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}