mirror of
https://github.com/Llewellynvdm/starship.git
synced 2024-11-16 18:15:16 +00:00
feat(logger): don't create empty log files (#1836)
This commit is contained in:
parent
919d367dd4
commit
b9c096bd56
@ -1,17 +1,19 @@
|
|||||||
use ansi_term::Color;
|
use ansi_term::Color;
|
||||||
use log::{Level, LevelFilter, Metadata, Record};
|
use log::{Level, LevelFilter, Metadata, Record};
|
||||||
|
use once_cell::sync::OnceCell;
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashSet,
|
collections::HashSet,
|
||||||
env,
|
env,
|
||||||
fs::{self, File, OpenOptions},
|
fs::{self, File, OpenOptions},
|
||||||
io::Write,
|
io::Write,
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
sync::{Arc, Mutex},
|
sync::Mutex,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct StarshipLogger {
|
pub struct StarshipLogger {
|
||||||
log_file: Arc<Mutex<File>>,
|
log_file: OnceCell<Mutex<File>>,
|
||||||
log_file_content: Arc<HashSet<String>>,
|
log_file_path: PathBuf,
|
||||||
|
log_file_content: HashSet<String>,
|
||||||
log_level: Level,
|
log_level: Level,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,25 +35,13 @@ impl StarshipLogger {
|
|||||||
));
|
));
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
log_file_content: Arc::new(
|
log_file_content: fs::read_to_string(&session_log_file)
|
||||||
fs::read_to_string(&session_log_file)
|
.unwrap_or_default()
|
||||||
.unwrap_or_default()
|
.lines()
|
||||||
.lines()
|
.map(|line| line.to_string())
|
||||||
.map(|line| line.to_string())
|
.collect(),
|
||||||
.collect(),
|
log_file: OnceCell::new(),
|
||||||
),
|
log_file_path: session_log_file,
|
||||||
log_file: Arc::new(Mutex::new(
|
|
||||||
OpenOptions::new()
|
|
||||||
.create(true)
|
|
||||||
.append(true)
|
|
||||||
.open(session_log_file.clone())
|
|
||||||
.unwrap_or_else(|err| {
|
|
||||||
panic!(
|
|
||||||
"Unable to open session log file {:?}: {:?}!",
|
|
||||||
session_log_file, err
|
|
||||||
)
|
|
||||||
}),
|
|
||||||
)),
|
|
||||||
log_level: env::var("STARSHIP_LOG")
|
log_level: env::var("STARSHIP_LOG")
|
||||||
.map(|level| match level.to_lowercase().as_str() {
|
.map(|level| match level.to_lowercase().as_str() {
|
||||||
"trace" => Level::Trace,
|
"trace" => Level::Trace,
|
||||||
@ -81,6 +71,21 @@ impl log::Log for StarshipLogger {
|
|||||||
|
|
||||||
if record.metadata().level() <= Level::Warn {
|
if record.metadata().level() <= Level::Warn {
|
||||||
self.log_file
|
self.log_file
|
||||||
|
.get_or_try_init(|| {
|
||||||
|
let m = Mutex::new(
|
||||||
|
OpenOptions::new()
|
||||||
|
.create(true)
|
||||||
|
.append(true)
|
||||||
|
.open(&self.log_file_path)?,
|
||||||
|
);
|
||||||
|
Ok(m)
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|err: std::io::Error| {
|
||||||
|
panic!(
|
||||||
|
"Unable to open session log file {:?}: {:?}!",
|
||||||
|
self.log_file_path, err
|
||||||
|
)
|
||||||
|
})
|
||||||
.lock()
|
.lock()
|
||||||
.map(|mut file| writeln!(file, "{}", to_print))
|
.map(|mut file| writeln!(file, "{}", to_print))
|
||||||
.expect("Log file writer mutex was poisoned!")
|
.expect("Log file writer mutex was poisoned!")
|
||||||
@ -104,11 +109,12 @@ impl log::Log for StarshipLogger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn flush(&self) {
|
fn flush(&self) {
|
||||||
self.log_file
|
if let Some(m) = self.log_file.get() {
|
||||||
.lock()
|
m.lock()
|
||||||
.map(|mut writer| writer.flush())
|
.map(|mut writer| writer.flush())
|
||||||
.expect("Log file writer mutex was poisoned!")
|
.expect("Log file writer mutex was poisoned!")
|
||||||
.expect("Unable to flush the log file!");
|
.expect("Unable to flush the log file!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user