Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Dec 9, 2024
1 parent 367f837 commit 735203e
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 7 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ eyre = "0.6"
fork = "0.2.0"
indexmap = { version = "2", features = ["serde"] }
interprocess = { version = "2", features = ["tokio"] }
itertools = "0.13.0"
log = "0.4"
notify-debouncer-mini = "0.5.0"
once_cell = "1"
psutil = "3"
rev_lines = "0.3.0"
rmp-serde = "1.3.0"
serde = { version = "1", features = ["derive"] }
serde_json = { version = "1", features = ["indexmap"] }
Expand Down
1 change: 1 addition & 0 deletions pitchfork.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[daemons]
1 change: 0 additions & 1 deletion src/cli/daemon/stop.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::cli::daemon::kill_or_stop;
use crate::state_file::StateFile;
use crate::{env, Result};
use duct::cmd;

/// Stops the internal pitchfork daemon running in the background
#[derive(Debug, clap::Args)]
Expand Down
66 changes: 66 additions & 0 deletions src/cli/logs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use crate::{env, Result};
use itertools::Itertools;
use std::collections::{BTreeMap, HashSet};

/// Displays logs for daemon(s)
#[derive(Debug, clap::Args)]
#[clap()]
pub struct Logs {
/// Show only logs for the specified daemon(s)
name: Vec<String>,

/// Show N lines of logs
///
/// Set to 0 to show all logs
#[clap(short, default_value = "10")]
n: usize,

/// Show logs in real-time
#[clap(short, long)]
tail: bool,
}

impl Logs {
pub async fn run(&self) -> Result<()> {
let names = self.name.iter().collect::<HashSet<_>>();
let log_files = xx::file::ls(&*env::PITCHFORK_LOGS_DIR)?
.into_iter()
.filter(|d| !d.starts_with("."))
.filter(|d| d.is_dir())
.filter_map(|d| d.file_name().map(|f| f.to_string_lossy().to_string()))
.filter(|n| names.is_empty() || names.contains(n))
.map(|n| {
(
n.clone(),
env::PITCHFORK_LOGS_DIR.join(&n).join(format!("{n}.log")),
)
})
.filter(|(_, f)| f.exists())
.collect::<BTreeMap<_, _>>();

let log_lines = log_files
.iter()
.flat_map(|(name, path)| {
let rev = match xx::file::open(path) {
Ok(f) => rev_lines::RevLines::new(f),
Err(e) => {
error!("{}: {}", path.display(), e);
return vec![];
}
};
let lines = rev.into_iter()
.filter_map(Result::ok)
.map(|l| (name, l));
if self.n == 0 {
lines.collect()
} else {
lines.take(self.n).collect()
}
})
.rev()
.collect_vec();

dbg!(&log_lines);
Ok(())
}
}
3 changes: 3 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use clap::Parser;
mod daemon;
mod run;
mod start;
mod logs;

#[derive(Debug, clap::Parser)]
struct Cli {
Expand All @@ -14,6 +15,7 @@ struct Cli {
#[derive(Debug, clap::Subcommand)]
enum Commands {
Daemon(daemon::Daemon),
Logs(logs::Logs),
Run(run::Run),
Start(start::Start),
}
Expand All @@ -23,6 +25,7 @@ pub async fn run() -> Result<()> {
let args = Cli::parse();
match args.command {
Commands::Daemon(daemon) => daemon.run().await,
Commands::Logs(logs) => logs.run().await,
Commands::Run(run) => run.run().await,
Commands::Start(start) => start.run().await,
}
Expand Down
11 changes: 5 additions & 6 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ pub static PITCHFORK_LOG: Lazy<log::LevelFilter> =
Lazy::new(|| var_log_level("PITCHFORK_LOG").unwrap_or(log::LevelFilter::Info));
pub static PITCHFORK_LOG_FILE_LEVEL: Lazy<log::LevelFilter> =
Lazy::new(|| var_log_level("PITCHFORK_LOG_FILE_LEVEL").unwrap_or(*PITCHFORK_LOG));
pub static PITCHFORK_LOGS_DIR: Lazy<PathBuf> =
Lazy::new(|| var_path("PITCHFORK_LOGS_DIR").unwrap_or(PITCHFORK_STATE_DIR.join("logs")));
pub static PITCHFORK_LOG_FILE: Lazy<PathBuf> = Lazy::new(|| {
var_path("PITCHFORK_LOG_FILE").unwrap_or(
PITCHFORK_STATE_DIR
.join("logs")
.join("pitchfork")
.join("pitchfork.log"),
)
PITCHFORK_LOGS_DIR
.join("pitchfork")
.join("pitchfork.log")
});
pub static PITCHFORK_EXEC: Lazy<bool> = Lazy::new(|| var_true("PITCHFORK_EXEC"));

Expand Down

0 comments on commit 735203e

Please sign in to comment.