2021-07-04 19:32:58 +00:00
|
|
|
use crate::utils;
|
2020-09-28 20:38:50 +00:00
|
|
|
use log::{Level, LevelFilter, Metadata, Record};
|
2022-09-04 16:44:54 +00:00
|
|
|
use nu_ansi_term::Color;
|
2020-10-27 18:00:53 +00:00
|
|
|
use once_cell::sync::OnceCell;
|
2020-09-28 20:38:50 +00:00
|
|
|
use std::{
|
|
|
|
collections::HashSet,
|
|
|
|
env,
|
|
|
|
fs::{self, File, OpenOptions},
|
|
|
|
io::Write,
|
|
|
|
path::PathBuf,
|
2020-10-27 18:00:53 +00:00
|
|
|
sync::Mutex,
|
2020-09-28 20:38:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pub struct StarshipLogger {
|
2020-10-27 18:00:53 +00:00
|
|
|
log_file: OnceCell<Mutex<File>>,
|
|
|
|
log_file_path: PathBuf,
|
|
|
|
log_file_content: HashSet<String>,
|
2020-09-28 20:38:50 +00:00
|
|
|
log_level: Level,
|
|
|
|
}
|
|
|
|
|
2021-01-16 12:25:58 +00:00
|
|
|
impl Default for StarshipLogger {
|
|
|
|
fn default() -> Self {
|
2020-09-28 20:38:50 +00:00
|
|
|
let log_dir = env::var_os("STARSHIP_CACHE")
|
|
|
|
.map(PathBuf::from)
|
|
|
|
.unwrap_or_else(|| {
|
2021-07-04 19:32:58 +00:00
|
|
|
utils::home_dir()
|
2020-09-28 20:38:50 +00:00
|
|
|
.expect("Unable to find home directory")
|
|
|
|
.join(".cache/starship")
|
|
|
|
});
|
|
|
|
|
2020-10-14 16:13:44 +00:00
|
|
|
fs::create_dir_all(&log_dir)
|
2022-11-05 11:40:46 +00:00
|
|
|
.unwrap_or_else(|err| panic!("Unable to create log dir {log_dir:?}: {err:?}!"));
|
2020-09-28 20:38:50 +00:00
|
|
|
let session_log_file = log_dir.join(format!(
|
|
|
|
"session_{}.log",
|
|
|
|
env::var("STARSHIP_SESSION_KEY").unwrap_or_default()
|
|
|
|
));
|
|
|
|
|
|
|
|
Self {
|
2020-10-27 18:00:53 +00:00
|
|
|
log_file_content: fs::read_to_string(&session_log_file)
|
|
|
|
.unwrap_or_default()
|
|
|
|
.lines()
|
2021-07-29 18:27:46 +00:00
|
|
|
.map(std::string::ToString::to_string)
|
2020-10-27 18:00:53 +00:00
|
|
|
.collect(),
|
|
|
|
log_file: OnceCell::new(),
|
|
|
|
log_file_path: session_log_file,
|
2020-09-28 20:38:50 +00:00
|
|
|
log_level: env::var("STARSHIP_LOG")
|
|
|
|
.map(|level| match level.to_lowercase().as_str() {
|
|
|
|
"trace" => Level::Trace,
|
|
|
|
"debug" => Level::Debug,
|
|
|
|
"info" => Level::Info,
|
|
|
|
"warn" => Level::Warn,
|
|
|
|
"error" => Level::Error,
|
|
|
|
_ => Level::Warn,
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|_| Level::Warn),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 12:25:58 +00:00
|
|
|
impl StarshipLogger {
|
|
|
|
/// Override the minimum log level
|
|
|
|
pub fn set_log_level(&mut self, level: log::Level) {
|
|
|
|
self.log_level = level;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Override the log level path
|
|
|
|
/// This won't change anything if a log file was already opened
|
|
|
|
pub fn set_log_file_path(&mut self, path: PathBuf) {
|
|
|
|
self.log_file_path = path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 20:38:50 +00:00
|
|
|
impl log::Log for StarshipLogger {
|
|
|
|
fn enabled(&self, metadata: &Metadata) -> bool {
|
|
|
|
metadata.level() <= self.log_level
|
|
|
|
}
|
|
|
|
|
|
|
|
fn log(&self, record: &Record) {
|
|
|
|
let to_print = format!(
|
|
|
|
"[{}] - ({}): {}",
|
|
|
|
record.level(),
|
|
|
|
record.module_path().unwrap_or_default(),
|
|
|
|
record.args()
|
|
|
|
);
|
|
|
|
|
|
|
|
if record.metadata().level() <= Level::Warn {
|
|
|
|
self.log_file
|
2020-10-27 18:00:53 +00:00
|
|
|
.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!(
|
2022-11-05 11:40:46 +00:00
|
|
|
"Unable to open session log file {:?}: {err:?}!",
|
|
|
|
self.log_file_path
|
2020-10-27 18:00:53 +00:00
|
|
|
)
|
|
|
|
})
|
2020-09-28 20:38:50 +00:00
|
|
|
.lock()
|
2022-11-05 11:40:46 +00:00
|
|
|
.map(|mut file| writeln!(file, "{to_print}"))
|
2020-09-28 20:38:50 +00:00
|
|
|
.expect("Log file writer mutex was poisoned!")
|
|
|
|
.expect("Unable to write to the log file!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.enabled(record.metadata()) && !self.log_file_content.contains(to_print.as_str()) {
|
|
|
|
eprintln!(
|
|
|
|
"[{}] - ({}): {}",
|
|
|
|
match record.level() {
|
2021-02-13 13:47:07 +00:00
|
|
|
Level::Trace => Color::Blue.dimmed().paint(format!("{}", record.level())),
|
|
|
|
Level::Debug => Color::Cyan.paint(format!("{}", record.level())),
|
2020-09-28 20:38:50 +00:00
|
|
|
Level::Info => Color::White.paint(format!("{}", record.level())),
|
|
|
|
Level::Warn => Color::Yellow.paint(format!("{}", record.level())),
|
|
|
|
Level::Error => Color::Red.paint(format!("{}", record.level())),
|
|
|
|
},
|
|
|
|
record.module_path().unwrap_or_default(),
|
|
|
|
record.args()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&self) {
|
2020-10-27 18:00:53 +00:00
|
|
|
if let Some(m) = self.log_file.get() {
|
|
|
|
m.lock()
|
|
|
|
.map(|mut writer| writer.flush())
|
|
|
|
.expect("Log file writer mutex was poisoned!")
|
|
|
|
.expect("Unable to flush the log file!");
|
|
|
|
}
|
2020-09-28 20:38:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn init() {
|
2022-12-17 17:01:27 +00:00
|
|
|
log::set_boxed_logger(Box::<StarshipLogger>::default()).unwrap();
|
2020-09-28 20:38:50 +00:00
|
|
|
log::set_max_level(LevelFilter::Trace);
|
|
|
|
}
|