Depend on tracing git

This commit is contained in:
Ottatop 2023-09-14 01:34:20 -05:00
parent 3ebb0cd133
commit cd689fd7a6
2 changed files with 10 additions and 48 deletions

View file

@ -10,8 +10,9 @@ repository = "https://github.com/pinnacle-comp/pinnacle/"
keywords = ["wayland", "compositor", "smithay", "lua"]
[dependencies]
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
tracing = { git = "https://github.com/tokio-rs/tracing", rev = "b8c45cc" }
tracing-subscriber = { git = "https://github.com/tokio-rs/tracing", rev = "b8c45cc", features = ["env-filter"] }
tracing-appender = { git = "https://github.com/tokio-rs/tracing", rev = "b8c45cc" }
smithay = { git = "https://github.com/Smithay/smithay", rev = "1a61e1c", features = ["desktop", "wayland_frontend"] }
smithay-drm-extras = { git = "https://github.com/Smithay/smithay", optional = true }
thiserror = "1.0.48"
@ -31,7 +32,6 @@ clap = { version = "4.4.2", features = ["derive"] }
xkbcommon = "0.6.0"
xdg = "2.5.2"
lazy_static = "1.4.0"
tracing-appender = "0.2.2"
walkdir = "2.4.0"
sysinfo = "0.29.10"

View file

@ -11,11 +11,9 @@
// #![deny(unused_imports)] // gonna force myself to keep stuff clean
#![warn(clippy::unwrap_used)]
use std::path::Path;
use clap::Parser;
use tracing_appender::rolling::Rotation;
use tracing_subscriber::{fmt::writer::MakeWriterExt, EnvFilter};
use walkdir::WalkDir;
use xdg::BaseDirectories;
mod api;
@ -63,14 +61,16 @@ struct Args {
force: bool,
}
const PINNACLE_LOG_PREFIX: &str = "pinnacle.log";
fn main() -> anyhow::Result<()> {
let xdg_state_dir = XDG_BASE_DIRS.get_state_home();
trim_logs(&xdg_state_dir);
let appender = tracing_appender::rolling::Builder::new()
.rotation(Rotation::HOURLY)
.filename_suffix("pinnacle.log")
.max_log_files(8)
.build(xdg_state_dir)
.expect("failed to build file logger");
let appender = tracing_appender::rolling::hourly(&xdg_state_dir, PINNACLE_LOG_PREFIX);
let (appender, _guard) = tracing_appender::non_blocking(appender);
let writer = appender.and(std::io::stdout);
@ -140,41 +140,3 @@ fn main() -> anyhow::Result<()> {
Ok(())
}
fn trim_logs(log_path: impl AsRef<Path>) {
let logs = WalkDir::new(log_path)
.sort_by(|a, b| {
let a_creation_time = a
.metadata()
.expect("failed to get log metadata")
.created()
.expect("failed to get log creation time");
let b_creation_time = b
.metadata()
.expect("failed to get log metadata")
.created()
.expect("failed to get log creation time");
a_creation_time.cmp(&b_creation_time)
})
.contents_first(true)
.into_iter()
.filter_entry(|entry| {
entry.file_type().is_file()
&& entry
.file_name()
.to_string_lossy()
.starts_with(PINNACLE_LOG_PREFIX)
})
.filter_map(|dir| dir.ok())
.collect::<Vec<_>>();
// If there are more than 4 logs, delete all but 3
if logs.len() > 4 {
let num_to_delete = logs.len().saturating_sub(3);
for entry in logs.into_iter().take(num_to_delete) {
tracing::info!("Deleting {:?}", entry.path());
std::fs::remove_file(entry.path()).expect("failed to remove oldest log file");
}
}
}