1
0
Fork 0
forked from wry/wry

Add clean-logs-older-than option

This commit is contained in:
khyperia 2026-03-27 07:44:34 +01:00 committed by Julian Orth
parent 4c7d108e09
commit 880c98ecfb
17 changed files with 360 additions and 10 deletions

View file

@ -523,6 +523,7 @@ pub struct Config {
pub keymaps: Vec<ConfigKeymap>,
pub auto_reload: Option<bool>,
pub log_level: Option<LogLevel>,
pub clean_logs_older_than: Option<Duration>,
pub theme: Theme,
pub egui: Egui,
pub gfx_api: Option<GfxApi>,

View file

@ -9,6 +9,7 @@ use {
pub mod action;
mod actions;
mod capabilities;
mod clean_logs_older_than;
mod client_match;
mod client_rule;
mod color;

View file

@ -0,0 +1,57 @@
use {
crate::{
config::{
context::Context,
extractor::{Extractor, ExtractorError, fltorint, opt},
parser::{DataType, ParseResult, Parser, UnexpectedDataType},
},
toml::{
toml_span::{DespanExt, Span, Spanned, SpannedExt},
toml_value::Value,
},
},
indexmap::IndexMap,
std::time::{Duration, TryFromFloatSecsError},
thiserror::Error,
};
#[derive(Debug, Error)]
pub enum CleanLogsOlderThanParserError {
#[error(transparent)]
Expected(#[from] UnexpectedDataType),
#[error(transparent)]
Extract(#[from] ExtractorError),
#[error("At least one of the `weeks` or `days` fields must be specified")]
WeeksOrDays,
#[error("Duration is invalid")]
InvalidDuration(#[source] TryFromFloatSecsError),
}
pub struct CleanLogsOlderThanParser<'a>(pub &'a Context<'a>);
impl Parser for CleanLogsOlderThanParser<'_> {
type Value = Duration;
type Error = CleanLogsOlderThanParserError;
const EXPECTED: &'static [DataType] = &[DataType::Table];
fn parse_table(
&mut self,
span: Span,
table: &IndexMap<Spanned<String>, Spanned<Value>>,
) -> ParseResult<Self> {
let mut ext = Extractor::new(self.0, span, table);
let (weeks, days) = ext.extract((opt(fltorint("weeks")), opt(fltorint("days"))))?;
if weeks.is_none() && days.is_none() {
return Err(CleanLogsOlderThanParserError::WeeksOrDays.spanned(span));
}
const SECONDS_IN_WEEK: f64 = 604800.0;
const SECONDS_IN_DAY: f64 = 86400.0;
let duration = Duration::try_from_secs_f64(
weeks.despan().unwrap_or_default() * SECONDS_IN_WEEK
+ days.despan().unwrap_or_default() * SECONDS_IN_DAY,
)
.map_err(CleanLogsOlderThanParserError::InvalidDuration)
.map_err(|e| e.spanned(span))?;
Ok(duration)
}
}

View file

@ -8,6 +8,7 @@ use {
parsers::{
action::ActionParser,
actions::ActionsParser,
clean_logs_older_than::CleanLogsOlderThanParser,
client_rule::ClientRulesParser,
color_management::ColorManagementParser,
connector::ConnectorsParser,
@ -152,6 +153,7 @@ impl Parser for ConfigParser<'_> {
show_titles,
fallback_output_mode_val,
egui_val,
clean_logs_older_than_val,
),
) = ext.extract((
(
@ -211,6 +213,7 @@ impl Parser for ConfigParser<'_> {
recover(opt(bol("show-titles"))),
opt(val("fallback-output-mode")),
opt(val("egui")),
opt(val("clean-logs-older-than")),
),
))?;
let mut keymap = None;
@ -307,6 +310,17 @@ impl Parser for ConfigParser<'_> {
}
}
}
let mut clean_logs_older_than = None;
if let Some(value) = clean_logs_older_than_val {
match value.parse(&mut CleanLogsOlderThanParser(self.0)) {
Ok(v) => {
clean_logs_older_than = Some(v);
}
Err(e) => {
log::warn!("Could not parse clean-logs-older-than: {}", self.0.error(e));
}
}
}
let mut theme = Theme::default();
if let Some(value) = theme_val {
match value.parse(&mut ThemeParser(self.0)) {
@ -567,6 +581,7 @@ impl Parser for ConfigParser<'_> {
keymaps,
auto_reload: auto_reload.despan(),
log_level,
clean_logs_older_than,
theme,
egui,
gfx_api,

View file

@ -35,7 +35,7 @@ use {
io::Async,
is_reload,
keyboard::Keymap,
logging::set_log_level,
logging::{clean_logs_older_than, set_log_level},
on_devices_enumerated, on_idle, on_unload, open_control_center, quit, reload,
set_color_management_enabled, set_default_workspace_capture, set_explicit_sync_enabled,
set_float_above_fullscreen, set_idle, set_idle_grace_period,
@ -67,7 +67,7 @@ use {
os::{fd::AsRawFd, unix::ffi::OsStrExt},
path::{Path, PathBuf},
rc::Rc,
time::Duration,
time::{Duration, SystemTime, UNIX_EPOCH},
},
uapi::{
Errno,
@ -1494,6 +1494,11 @@ fn load_config(initial_load: bool, auto_reload: bool, persistent: &Rc<Persistent
if let Some(level) = config.log_level {
set_log_level(level);
}
if let Some(duration) = config.clean_logs_older_than {
let now = SystemTime::now();
let in_the_past = now.checked_sub(duration).unwrap_or(UNIX_EPOCH);
clean_logs_older_than(in_the_past);
}
if let Some(idle) = config.idle {
set_idle(Some(idle));
}