Skip to content

Commit

Permalink
Fix logfiles growing indefinitely (fixes #750, #689, #1000)
Browse files Browse the repository at this point in the history
  • Loading branch information
elkowar committed Feb 17, 2024
1 parent 2656e23 commit 5b3dabf
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ All notable changes to eww will be listed here, starting at changes since versio
- Add `eww shell-completions` command, generating completion scripts for different shells

### Fixes
- Fixed wrong values in `EWW_NET`
- Fix wrong values in `EWW_NET`
- Fix logfiles growing indefinitely

## [0.4.0] (04.09.2022)

Expand Down
24 changes: 16 additions & 8 deletions crates/eww/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use anyhow::{bail, Result};
#[derive(Debug, Clone)]
pub struct EwwPaths {
pub log_file: PathBuf,
pub log_dir: PathBuf,
pub ipc_socket_file: PathBuf,
pub config_dir: PathBuf,
}
Expand Down Expand Up @@ -43,14 +44,17 @@ impl EwwPaths {
log::warn!("The IPC socket file's absolute path exceeds 100 bytes, the socket may fail to create.");
}

Ok(EwwPaths {
config_dir,
log_file: std::env::var("XDG_CACHE_HOME")
.map(PathBuf::from)
.unwrap_or_else(|_| PathBuf::from(std::env::var("HOME").unwrap()).join(".cache"))
.join(format!("eww_{}.log", daemon_id)),
ipc_socket_file,
})
let log_dir = std::env::var("XDG_CACHE_HOME")
.map(PathBuf::from)
.unwrap_or_else(|_| PathBuf::from(std::env::var("HOME").unwrap()).join(".cache"))
.join("eww");

if !log_dir.exists() {
log::info!("Creating log dir");
std::fs::create_dir_all(&log_dir)?;
}

Ok(EwwPaths { config_dir, log_file: log_dir.join(format!("eww_{}.log", daemon_id)), log_dir, ipc_socket_file })
}

pub fn default() -> Result<Self> {
Expand All @@ -66,6 +70,10 @@ impl EwwPaths {
self.log_file.as_path()
}

pub fn get_log_dir(&self) -> &Path {
self.log_dir.as_path()
}

pub fn get_ipc_socket_file(&self) -> &Path {
self.ipc_socket_file.as_path()
}
Expand Down
46 changes: 46 additions & 0 deletions crates/eww/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use anyhow::{Context, Result};
use std::{
cell::RefCell,
collections::{HashMap, HashSet},
io::Write,
os::unix::io::AsRawFd,
path::Path,
rc::Rc,
Expand Down Expand Up @@ -41,6 +42,8 @@ pub fn initialize_server<B: DisplayBackend>(
}
};

cleanup_log_dir(paths.get_log_dir())?;

if should_daemonize {
let fork_result = do_detach(paths.get_log_file())?;

Expand Down Expand Up @@ -263,3 +266,46 @@ fn do_detach(log_file_path: impl AsRef<Path>) -> Result<ForkResult> {

Ok(ForkResult::Child)
}

/// Ensure the log directory never grows larger than 100MB by deleting files older than 7 days,
/// and truncating all other logfiles to 100MB.
fn cleanup_log_dir(log_dir: impl AsRef<Path>) -> Result<()> {
// Find all files named "eww_*.log" in the log directory
let log_files = std::fs::read_dir(&log_dir)?
.filter_map(|entry| {
let entry = entry.ok()?;
let path = entry.path();
if let Some(file_name) = path.file_name() {
if file_name.to_string_lossy().starts_with("eww_") && file_name.to_string_lossy().ends_with(".log") {
Some(path)
} else {
None
}
} else {
None
}
})
.collect::<Vec<_>>();

for log_file in log_files {
// if the file is older than a week, delete it
if let Ok(metadata) = log_file.metadata() {
if metadata.modified()?.elapsed()?.as_secs() > 60 * 60 * 24 * 7 {
log::info!("Deleting old log file: {}", log_file.display());
std::fs::remove_file(&log_file)?;
} else {
// If the file is larger than 200MB, delete the start of it until it's 100MB or less.
let mut file = std::fs::OpenOptions::new().append(true).open(&log_file)?;
let file_size = file.metadata()?.len();
if file_size > 200_000_000 {
let mut file_content = std::fs::read(&log_file)?;
let bytes_to_remove = file_content.len().saturating_sub(100_000_000);
file_content.drain(0..bytes_to_remove);
file.set_len(0)?;
file.write_all(&file_content)?;
}
}
}
}
Ok(())
}

0 comments on commit 5b3dabf

Please sign in to comment.