File: meta-logging.rs

package info (click to toggle)
rust-fern 0.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 652 kB
  • sloc: makefile: 2
file content (32 lines) | stat: -rw-r--r-- 988 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//! This is an example to test the "meta-logging-in-format" fern cargo features.
//!
//! The example will hang if the feature is disabled, and will produce cohesive
//! logs if it's enabled.
use std::fmt;

use log::{debug, info};

fn main() {
    fern::Dispatch::new()
        .chain(std::io::stdout())
        .chain(std::io::stderr())
        .chain(fern::log_file("hello.txt").unwrap())
        .format(move |out, message, record| {
            out.finish(format_args!("[{}] {}", record.level(), message))
        })
        .apply()
        .unwrap();

    // in order to actually trigger the situation that deadlocks, we need a custom
    // Display implementation which performs logging:
    struct Thing<'a>(&'a str);

    impl<'a> fmt::Display for Thing<'a> {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            debug!("formatting Thing wrapping ({})", self.0);
            f.write_str(self.0)
        }
    }

    info!("I'm logging {}!", Thing("aha"));
}