global: snapshot

This commit is contained in:
nym21
2025-11-11 17:41:12 +01:00
parent 2dcbd8df99
commit 81da73bc53
17 changed files with 811 additions and 427 deletions
+24 -5
View File
@@ -3,7 +3,7 @@
use std::{
fmt::Display,
fs::{self, OpenOptions},
io::{self, Write},
io::{self, BufWriter, Write},
path::Path,
sync::OnceLock,
};
@@ -13,19 +13,23 @@ use jiff::{Timestamp, tz};
pub use owo_colors::OwoColorize;
use parking_lot::Mutex;
static LOG_FILE: OnceLock<Mutex<fs::File>> = OnceLock::new();
// Type alias for the hook function
type LogHook = Box<dyn Fn(&str) + Send + Sync>;
static LOG_HOOK: OnceLock<LogHook> = OnceLock::new();
static LOG_FILE: OnceLock<Mutex<BufWriter<fs::File>>> = OnceLock::new();
#[inline]
pub fn init(path: Option<&Path>) -> io::Result<()> {
if let Some(path) = path {
let _ = fs::remove_file(path);
let file = OpenOptions::new().create(true).append(true).open(path)?;
LOG_FILE.set(Mutex::new(file)).ok();
LOG_FILE.set(Mutex::new(BufWriter::new(file))).ok();
}
Builder::from_env(Env::default().default_filter_or(
"info,bitcoin=off,bitcoincore-rpc=off,fjall=off,lsm-tree=off,rolldown=off,rolldown=off,rmcp=off,brk_rmcp=off,tracing=off,aide=off,brk_aide=off",
// "debug,fjall=trace,bitcoin=off,bitcoincore-rpc=off,rolldown=off,rolldown=off,rmcp=off,brk_rmcp=off,tracing=off,aide=off,brk_aide=off",
"debug,bitcoin=off,bitcoincore-rpc=off,fjall=off,brk_fjall=off,lsm-tree=off,rolldown=off,rmcp=off,brk_rmcp=off,tracing=off,aide=off",
// "debug,fjall=trace,bitcoin=off,bitcoincore-rpc=off,rolldown=off,rmcp=off,brk_rmcp=off,tracing=off,aide=off",
))
.format(move |buf, record| {
let date_time = Timestamp::now()
@@ -38,6 +42,10 @@ pub fn init(path: Option<&Path>) -> io::Result<()> {
let dash = "-";
let args = record.args();
if let Some(hook) = LOG_HOOK.get() {
hook(&args.to_string());
}
if let Some(file) = LOG_FILE.get() {
let _ = write(&mut *file.lock(), &date_time, target, &level, dash, args);
}
@@ -67,6 +75,17 @@ pub fn init(path: Option<&Path>) -> io::Result<()> {
Ok(())
}
/// Register a hook that gets called for every log message
/// Can only be called once
pub fn register_hook<F>(hook: F) -> Result<(), &'static str>
where
F: Fn(&str) + Send + Sync + 'static,
{
LOG_HOOK
.set(Box::new(hook))
.map_err(|_| "Hook already registered")
}
fn write(
mut buf: impl Write,
date_time: impl Display,