mirror of
https://github.com/pinnacle-comp/pinnacle.git
synced 2025-02-06 20:46:37 +01:00
Add file logging
This commit is contained in:
parent
b2b23a88c9
commit
7fb3d798b9
3 changed files with 67 additions and 18 deletions
|
@ -31,6 +31,8 @@ clap = { version = "4.4.2", features = ["derive"] }
|
||||||
xkbcommon = "0.6.0"
|
xkbcommon = "0.6.0"
|
||||||
xdg = "2.5.2"
|
xdg = "2.5.2"
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
|
tracing-appender = "0.2.2"
|
||||||
|
walkdir = "2.4.0"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["egl", "winit", "udev", "xwayland"]
|
default = ["egl", "winit", "udev", "xwayland"]
|
||||||
|
|
65
src/main.rs
65
src/main.rs
|
@ -11,7 +11,12 @@
|
||||||
// #![deny(unused_imports)] // gonna force myself to keep stuff clean
|
// #![deny(unused_imports)] // gonna force myself to keep stuff clean
|
||||||
#![warn(clippy::unwrap_used)]
|
#![warn(clippy::unwrap_used)]
|
||||||
|
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
use tracing_subscriber::{fmt::writer::MakeWriterExt, EnvFilter};
|
||||||
|
use walkdir::WalkDir;
|
||||||
|
use xdg::BaseDirectories;
|
||||||
|
|
||||||
mod api;
|
mod api;
|
||||||
mod backend;
|
mod backend;
|
||||||
|
@ -29,6 +34,11 @@ mod state;
|
||||||
mod tag;
|
mod tag;
|
||||||
mod window;
|
mod window;
|
||||||
|
|
||||||
|
lazy_static::lazy_static! {
|
||||||
|
pub static ref XDG_BASE_DIRS: BaseDirectories =
|
||||||
|
BaseDirectories::with_prefix("pinnacle").expect("couldn't create xdg BaseDirectories");
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(clap::Args, Debug)]
|
#[derive(clap::Args, Debug)]
|
||||||
#[group(id = "backend", required = false, multiple = false)]
|
#[group(id = "backend", required = false, multiple = false)]
|
||||||
struct Backends {
|
struct Backends {
|
||||||
|
@ -53,18 +63,24 @@ struct Args {
|
||||||
force: bool,
|
force: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const PINNACLE_LOG_PREFIX: &str = "pinnacle.log";
|
||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
match tracing_subscriber::EnvFilter::try_from_default_env() {
|
let xdg_state_dir = XDG_BASE_DIRS.get_state_home();
|
||||||
Ok(env_filter) => {
|
|
||||||
|
trim_logs(&xdg_state_dir);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
let env_filter = EnvFilter::try_from_default_env().unwrap_or(EnvFilter::new("debug"));
|
||||||
|
|
||||||
tracing_subscriber::fmt()
|
tracing_subscriber::fmt()
|
||||||
.compact()
|
.compact()
|
||||||
.with_env_filter(env_filter)
|
.with_env_filter(env_filter)
|
||||||
|
.with_writer(writer)
|
||||||
.init();
|
.init();
|
||||||
}
|
|
||||||
Err(_) => {
|
|
||||||
tracing_subscriber::fmt().compact().init();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
|
|
||||||
|
@ -124,3 +140,38 @@ fn main() -> anyhow::Result<()> {
|
||||||
|
|
||||||
Ok(())
|
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)
|
||||||
|
})
|
||||||
|
.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 3 logs, delete the oldest ones
|
||||||
|
let num_to_delete = logs.len().saturating_sub(3);
|
||||||
|
|
||||||
|
for entry in logs.into_iter().take(num_to_delete) {
|
||||||
|
std::fs::remove_file(entry.path()).expect("failed to remove oldest log file");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
10
src/state.rs
10
src/state.rs
|
@ -65,15 +65,9 @@ use smithay::{
|
||||||
},
|
},
|
||||||
xwayland::{X11Surface, X11Wm, XWayland, XWaylandEvent},
|
xwayland::{X11Surface, X11Wm, XWayland, XWaylandEvent},
|
||||||
};
|
};
|
||||||
use xdg::BaseDirectories;
|
|
||||||
|
|
||||||
use crate::input::InputState;
|
use crate::input::InputState;
|
||||||
|
|
||||||
lazy_static::lazy_static! {
|
|
||||||
static ref XDG_BASE_DIRS: BaseDirectories =
|
|
||||||
BaseDirectories::with_prefix("pinnacle").expect("couldn't create xdg BaseDirectories");
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum Backend {
|
pub enum Backend {
|
||||||
Winit(Winit),
|
Winit(Winit),
|
||||||
Udev(Udev),
|
Udev(Udev),
|
||||||
|
@ -412,7 +406,7 @@ fn get_config_dir() -> PathBuf {
|
||||||
.ok()
|
.ok()
|
||||||
.and_then(|s| Some(PathBuf::from(shellexpand::full(&s).ok()?.to_string())));
|
.and_then(|s| Some(PathBuf::from(shellexpand::full(&s).ok()?.to_string())));
|
||||||
|
|
||||||
config_dir.unwrap_or(XDG_BASE_DIRS.get_config_home())
|
config_dir.unwrap_or(crate::XDG_BASE_DIRS.get_config_home())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This should be called *after* you have created the [`PinnacleSocketSource`] to ensure
|
/// This should be called *after* you have created the [`PinnacleSocketSource`] to ensure
|
||||||
|
@ -460,6 +454,8 @@ fn start_config(metaconfig: Metaconfig, config_dir: &Path) -> anyhow::Result<Con
|
||||||
.args(command)
|
.args(command)
|
||||||
.envs(envs)
|
.envs(envs)
|
||||||
.current_dir(config_dir)
|
.current_dir(config_dir)
|
||||||
|
.stdout(async_process::Stdio::inherit())
|
||||||
|
.stderr(async_process::Stdio::inherit())
|
||||||
.spawn()
|
.spawn()
|
||||||
.expect("failed to spawn config");
|
.expect("failed to spawn config");
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue