veilid/veilid-server/src/veilid_logs.rs

75 lines
2.6 KiB
Rust
Raw Normal View History

2022-03-11 00:00:59 +00:00
use crate::log_safe_channel::*;
2022-01-15 23:24:37 +00:00
use crate::settings::*;
use simplelog::*;
use std::fs::OpenOptions;
use std::path::Path;
pub struct VeilidLogs {
2022-03-11 00:00:59 +00:00
pub client_log_channel: Option<LogSafeChannel>,
pub client_log_channel_closer: Option<LogSafeChannelCloser>,
2022-01-15 23:24:37 +00:00
}
impl VeilidLogs {
pub fn setup_normal_logs(settings: Settings) -> Result<VeilidLogs, String> {
let settingsr = settings.read();
// Set up loggers
let mut logs: Vec<Box<dyn SharedLogger>> = Vec::new();
2022-03-11 00:00:59 +00:00
let mut client_log_channel: Option<LogSafeChannel> = None;
let mut client_log_channel_closer: Option<LogSafeChannelCloser> = None;
2022-01-15 23:24:37 +00:00
let mut cb = ConfigBuilder::new();
2022-02-01 03:47:17 +00:00
for ig in veilid_core::DEFAULT_LOG_IGNORE_LIST {
cb.add_filter_ignore_str(ig);
}
2022-01-15 23:24:37 +00:00
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 log_path = Path::new(&settingsr.logging.file.path);
2022-03-13 16:45:36 +00:00
let logfile = if settingsr.logging.file.append {
OpenOptions::new()
2022-01-15 23:24:37 +00:00
.create(true)
.append(true)
.open(log_path)
.map_err(|e| format!("failed to open log file: {}", e))?
} else {
2022-03-13 16:45:36 +00:00
OpenOptions::new()
2022-01-15 23:24:37 +00:00
.create(true)
.truncate(true)
.write(true)
.open(log_path)
.map_err(|e| format!("failed to open log file: {}", e))?
2022-03-13 16:45:36 +00:00
};
2022-01-15 23:24:37 +00:00
logs.push(WriteLogger::new(
convert_loglevel(settingsr.logging.file.level),
cb.build(),
logfile,
))
}
if settingsr.logging.client.enabled {
2022-03-11 00:00:59 +00:00
let (clog, clogwriter, clogcloser) = LogSafeChannel::new();
2022-01-15 23:24:37 +00:00
client_log_channel = Some(clog);
client_log_channel_closer = Some(clogcloser);
logs.push(WriteLogger::new(
convert_loglevel(settingsr.logging.client.level),
cb.build(),
clogwriter,
))
}
CombinedLogger::init(logs).map_err(|e| format!("failed to init logs: {}", e))?;
Ok(VeilidLogs {
client_log_channel,
client_log_channel_closer,
})
}
}