1 use anyhow
::{bail, Result}
;
2 use log
::{Level, Metadata, Record}
;
3 use std
::{fs::File, io::Write, sync::Mutex, sync::OnceLock}
;
5 pub struct AutoInstLogger
;
6 static LOGFILE
: OnceLock
<Mutex
<File
>> = OnceLock
::new();
9 pub fn init(path
: &str) -> Result
<()> {
10 let f
= File
::create(path
)?
;
11 if LOGFILE
.set(Mutex
::new(f
)).is_err() {
12 bail
!("Cannot set LOGFILE")
18 impl log
::Log
for AutoInstLogger
{
19 fn enabled(&self, metadata
: &Metadata
) -> bool
{
20 metadata
.level() <= Level
::Info
23 /// Logs to both, stderr and into a log file
24 fn log(&self, record
: &Record
) {
25 if self.enabled(record
.metadata()) {
26 eprintln
!("{}: {}", record
.level(), record
.args());
27 let mut file
= LOGFILE
29 .expect("could not get LOGFILE")
31 .expect("could not get mutex for LOGFILE");
32 write
!(file
, "{}: {}\n", record
.level(), record
.args())
33 .expect("could not write to LOGFILE");
40 .expect("could not get LOGFILE")
42 .expect("could not get mutex for LOGFILE")
44 .expect("could not flush LOGFILE");