refactor checkpoint
This commit is contained in:
+100
-135
@@ -1,162 +1,127 @@
|
||||
use crate::log_safe_channel::*;
|
||||
use crate::settings::*;
|
||||
use cfg_if::*;
|
||||
use log::*;
|
||||
use simplelog::*;
|
||||
use std::fs::OpenOptions;
|
||||
use std::path::Path;
|
||||
use std::path::*;
|
||||
use tracing::*;
|
||||
use tracing_appender::*;
|
||||
use tracing_subscriber::prelude::*;
|
||||
use tracing_subscriber::*;
|
||||
|
||||
pub struct VeilidLogs {
|
||||
pub client_log_channel: Option<LogSafeChannel>,
|
||||
pub client_log_channel_closer: Option<LogSafeChannelCloser>,
|
||||
pub guard: Option<non_blocking::WorkerGuard>,
|
||||
}
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(target_os = "linux")] {
|
||||
use systemd_journal_logger::JournalLog;
|
||||
pub struct SystemLogger {
|
||||
level_filter: LevelFilter,
|
||||
config: Config,
|
||||
journal_log: JournalLog<String,String>,
|
||||
}
|
||||
|
||||
impl SystemLogger {
|
||||
pub fn new(level_filter: LevelFilter, config: Config) -> Box<Self> {
|
||||
Box::new(Self {
|
||||
level_filter,
|
||||
config,
|
||||
journal_log: JournalLog::with_extra_fields(Vec::new())
|
||||
})
|
||||
}
|
||||
|
||||
pub fn should_skip(record: &Record<'_>) -> bool {
|
||||
// // If a module path and allowed list are available
|
||||
// match (record.target(), &*config.filter_allow) {
|
||||
// (path, allowed) if !allowed.is_empty() => {
|
||||
// // Check that the module path matches at least one allow filter
|
||||
// if !allowed.iter().any(|v| path.starts_with(&**v)) {
|
||||
// // If not, skip any further writing
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
// _ => {}
|
||||
// }
|
||||
|
||||
// If a module path and ignore list are available
|
||||
match (record.target(), &veilid_core::DEFAULT_LOG_IGNORE_LIST) {
|
||||
(path, ignore) if !ignore.is_empty() => {
|
||||
// Check that the module path does not match any ignore filters
|
||||
if ignore.iter().any(|v| path.starts_with(&**v)) {
|
||||
// If not, skip any further writing
|
||||
return true;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl Log for SystemLogger {
|
||||
fn enabled(&self, metadata: &Metadata<'_>) -> bool {
|
||||
metadata.level() <= self.level_filter
|
||||
}
|
||||
|
||||
fn log(&self, record: &Record<'_>) {
|
||||
if self.enabled(record.metadata()) && ! Self::should_skip(record) {
|
||||
self.journal_log.log(record);
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&self) {
|
||||
self.journal_log.flush();
|
||||
}
|
||||
}
|
||||
|
||||
impl SharedLogger for SystemLogger {
|
||||
fn level(&self) -> LevelFilter {
|
||||
self.level_filter
|
||||
}
|
||||
fn config(&self) -> Option<&Config> {
|
||||
Some(&self.config)
|
||||
}
|
||||
fn as_log(self: Box<Self>) -> Box<dyn Log> {
|
||||
Box::new(*self)
|
||||
}
|
||||
fn logfilter<T: AsRef<str>, V: AsRef<[T]>>(
|
||||
metadata: &Metadata,
|
||||
max_level: veilid_core::VeilidLogLevel,
|
||||
ignore_list: V,
|
||||
) -> bool {
|
||||
// Skip filtered targets
|
||||
!match (metadata.target(), ignore_list.as_ref()) {
|
||||
(path, ignore) if !ignore.is_empty() => {
|
||||
// Check that the module path does not match any ignore filters
|
||||
ignore.iter().any(|v| path.starts_with(v.as_ref()))
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
impl VeilidLogs {
|
||||
pub fn setup_normal_logs(settings: Settings) -> Result<VeilidLogs, String> {
|
||||
pub fn setup(settings: Settings) -> Result<VeilidLogs, String> {
|
||||
let settingsr = settings.read();
|
||||
|
||||
// Set up loggers
|
||||
let mut logs: Vec<Box<dyn SharedLogger>> = Vec::new();
|
||||
let mut client_log_channel: Option<LogSafeChannel> = None;
|
||||
let mut client_log_channel_closer: Option<LogSafeChannelCloser> = None;
|
||||
let mut cb = ConfigBuilder::new();
|
||||
// Set up subscriber and layers
|
||||
|
||||
let mut ignore_list = Vec::<String>::new();
|
||||
for ig in veilid_core::DEFAULT_LOG_IGNORE_LIST {
|
||||
cb.add_filter_ignore_str(ig);
|
||||
ignore_list.push(ig.to_owned());
|
||||
}
|
||||
|
||||
if settingsr.logging.terminal.enabled {
|
||||
logs.push(TermLogger::new(
|
||||
convert_loglevel(settingsr.logging.terminal.level),
|
||||
cb.build(),
|
||||
TerminalMode::Mixed,
|
||||
ColorChoice::Auto,
|
||||
))
|
||||
}
|
||||
if settingsr.logging.file.enabled {
|
||||
let subscriber = Registry::default();
|
||||
|
||||
let subscriber = subscriber.with(
|
||||
EnvFilter::builder()
|
||||
.with_default_directive(level_filters::LevelFilter::INFO.into())
|
||||
.from_env_lossy(),
|
||||
);
|
||||
|
||||
let subscriber = subscriber.with(if settingsr.logging.terminal.enabled {
|
||||
let terminal_max_log_level = convert_loglevel(settingsr.logging.terminal.level);
|
||||
Some(
|
||||
fmt::Layer::new()
|
||||
.compact()
|
||||
.with_writer(std::io::stdout)
|
||||
.with_filter(filter::FilterFn::new(move |metadata| {
|
||||
logfilter(metadata, terminal_max_log_level, &ignore_list)
|
||||
})),
|
||||
)
|
||||
} else {
|
||||
None
|
||||
});
|
||||
|
||||
let mut guard = None;
|
||||
let subscriber = subscriber.with(if settingsr.logging.file.enabled {
|
||||
let file_max_log_level = convert_loglevel(settingsr.logging.file.level);
|
||||
|
||||
let log_path = Path::new(&settingsr.logging.file.path);
|
||||
let full_path = std::env::current_dir()
|
||||
.unwrap_or(PathBuf::from(MAIN_SEPARATOR.to_string()))
|
||||
.join(log_path);
|
||||
let log_parent = full_path
|
||||
.parent()
|
||||
.unwrap_or(Path::new(&MAIN_SEPARATOR.to_string()))
|
||||
.canonicalize()
|
||||
.map_err(|e| {
|
||||
format!(
|
||||
"File log path parent does not exist: {} ({})",
|
||||
settingsr.logging.file.path, e
|
||||
)
|
||||
})?;
|
||||
let log_filename = full_path.file_name().ok_or(format!(
|
||||
"File log filename not specified in path: {}",
|
||||
settingsr.logging.file.path
|
||||
))?;
|
||||
|
||||
let logfile = if settingsr.logging.file.append {
|
||||
OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(log_path)
|
||||
.map_err(|e| format!("failed to open log file: {}", e))?
|
||||
} else {
|
||||
OpenOptions::new()
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.write(true)
|
||||
.open(log_path)
|
||||
.map_err(|e| format!("failed to open log file: {}", e))?
|
||||
};
|
||||
logs.push(WriteLogger::new(
|
||||
convert_loglevel(settingsr.logging.file.level),
|
||||
cb.build(),
|
||||
logfile,
|
||||
))
|
||||
}
|
||||
if settingsr.logging.client.enabled {
|
||||
let (clog, clogwriter, clogcloser) = LogSafeChannel::new();
|
||||
client_log_channel = Some(clog);
|
||||
client_log_channel_closer = Some(clogcloser);
|
||||
logs.push(WriteLogger::new(
|
||||
convert_loglevel(settingsr.logging.client.level),
|
||||
cb.build(),
|
||||
clogwriter,
|
||||
))
|
||||
}
|
||||
let appender = tracing_appender::rolling::never(log_parent, Path::new(log_filename));
|
||||
let (non_blocking_appender, non_blocking_guard) =
|
||||
tracing_appender::non_blocking(appender);
|
||||
guard = Some(non_blocking_guard);
|
||||
|
||||
Some(
|
||||
fmt::Layer::new()
|
||||
.compact()
|
||||
.with_writer(non_blocking_appender)
|
||||
.with_filter(filter::FilterFn::new(|metadata| {
|
||||
logfilter(metadata, file_max_log_level, &ignore_list)
|
||||
})),
|
||||
)
|
||||
} else {
|
||||
None
|
||||
});
|
||||
|
||||
let subscriber = subscriber.with(if settingsr.logging.api.enabled {
|
||||
// Get layer from veilid core, filtering is done by ApiTracingLayer automatically
|
||||
Some(veilid_core::ApiTracingLayer::get())
|
||||
} else {
|
||||
None
|
||||
});
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(target_os = "linux")] {
|
||||
if settingsr.logging.system.enabled {
|
||||
logs.push(SystemLogger::new(convert_loglevel(settingsr.logging.system.level), cb.build()))
|
||||
}
|
||||
let subscriber = subscriber.with(if settingsr.logging.system.enabled {
|
||||
let system_max_log_level = convert_loglevel(settingsr.logging.system.level);
|
||||
Some(tracing_journald::layer().map_err(|e| format!("failed to set up journald logging: {}", e))?.with_filter(filter::FilterFn::new(|metadata| {
|
||||
logfilter(metadata, system_max_log_level, &ignore_list)
|
||||
})))
|
||||
} else {
|
||||
None
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
CombinedLogger::init(logs).map_err(|e| format!("failed to init logs: {}", e))?;
|
||||
subscriber
|
||||
.try_init()
|
||||
.map_err(|e| format!("failed to initialize logging: {}", e))?;
|
||||
|
||||
Ok(VeilidLogs {
|
||||
client_log_channel,
|
||||
client_log_channel_closer,
|
||||
})
|
||||
Ok(VeilidLogs { guard })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user