My apologizes for this seemingly simple question. I must be having a brain fault and cannot figure this out.
I am getting the following compile error on my closure in this function:
error[E0619]: the type of this value must be known in this context
--> src/utils/logger.rs:37:22
|
37 | writeln!(buf, "{:>5}|{:<30}|{:>35}:{:<4}| {}", record.level(), record.target(), record.location().file(), record.location().line(), record.args())
| ^^^
here's the code:
use std::env;
use std::sync::{Once, ONCE_INIT};
use env_logger::Builder;
use log::{Record, Level, Metadata, Log, LevelFilter};
static LOGGER_INIT: Once = ONCE_INIT;
pub fn init_log() {
LOGGER_INIT.call_once(|| {
let format = |buf, record: &Record| {
writeln!(buf, "{:>5}|{:<30}|{:>35?}:{:<4?}| {}", record.level(), record.target(), record.file(), record.line(), record.args())
};
let mut builder = Builder::new();
builder.format(format).filter(None, LevelFilter::Off);
if env::var("RUST_LOG").is_ok() {
builder.parse(&env::var("RUST_LOG").unwrap());
}
builder.init();
});
}
Type checker needs to know the type of buf there, which I believe is &mut env_logger::fmt::Formatter. In addition, you need to use std::io::Write so the writeln! can call write_fmt on it. So make the closure
let format = |buf: &mut env_logger::fmt::Formatter, record: &Record| { ... };
Alternatively, put the closure inline with builder.format(...) and let type inference figure out buf there:
builder.format(|buf, record| ...) // buf and record will both be inferred