logging via tracing

This commit is contained in:
raf 2025-07-22 01:21:42 +03:00
commit 9bd20270ba
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
7 changed files with 233 additions and 28 deletions

View file

@ -7,5 +7,8 @@ authors.workspace = true
rust-version.workspace = true
[dependencies]
clap.workspace = true
regex.workspace = true
clap.workspace = true
regex.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
yansi.workspace = true

View file

@ -1,6 +1,8 @@
use crate::command::{NixCommand, StdIoInterceptor};
use crate::util::{HashExtractor, NixErrorClassifier, NixFileFixer};
use std::io::Write;
use tracing::{info, warn};
use yansi::Paint;
pub fn handle_nix_build(
args: &[String],
@ -28,7 +30,7 @@ pub fn handle_nix_build(
if let Some(new_hash) = hash_extractor.extract_hash(&stderr) {
if fixer.fix_hash_in_files(&new_hash) {
eprintln!("\x1b[32m✔ Fixed hash mismatch, retrying...\x1b[0m");
info!("{}", Paint::green("✔ Fixed hash mismatch, retrying..."));
let retry_status = NixCommand::new("build")
.print_build_logs(true)
.args(args.iter().cloned())
@ -40,8 +42,9 @@ pub fn handle_nix_build(
if classifier.should_retry(&stderr) {
if stderr.contains("unfree") {
eprintln!(
"\x1b[33m⚠ Unfree package detected, retrying with NIXPKGS_ALLOW_UNFREE=1...\x1b[0m"
warn!(
"{}",
Paint::yellow("⚠ Unfree package detected, retrying with NIXPKGS_ALLOW_UNFREE=1...")
);
let retry_status = NixCommand::new("build")
.print_build_logs(true)
@ -53,8 +56,11 @@ pub fn handle_nix_build(
std::process::exit(retry_status.code().unwrap_or(1));
}
if stderr.contains("insecure") {
eprintln!(
"\x1b[33m⚠ Insecure package detected, retrying with NIXPKGS_ALLOW_INSECURE=1...\x1b[0m"
warn!(
"{}",
Paint::yellow(
"⚠ Insecure package detected, retrying with NIXPKGS_ALLOW_INSECURE=1..."
)
);
let retry_status = NixCommand::new("build")
.print_build_logs(true)
@ -66,8 +72,9 @@ pub fn handle_nix_build(
std::process::exit(retry_status.code().unwrap_or(1));
}
if stderr.contains("broken") {
eprintln!(
"\x1b[33m⚠ Broken package detected, retrying with NIXPKGS_ALLOW_BROKEN=1...\x1b[0m"
warn!(
"{}",
Paint::yellow("⚠ Broken package detected, retrying with NIXPKGS_ALLOW_BROKEN=1...")
);
let retry_status = NixCommand::new("build")
.print_build_logs(true)

View file

@ -36,6 +36,14 @@ enum Command {
}
fn main() {
let format = tracing_subscriber::fmt::format()
.with_level(true) // don't include levels in formatted output
.with_target(true) // don't include targets
.with_thread_ids(false) // include the thread ID of the current thread
.with_thread_names(false) // include the name of the current thread
.compact(); // use the `Compact` formatting style.
tracing_subscriber::fmt().event_format(format).init();
let mut args = env::args();
let bin = args.next().unwrap_or_else(|| "eh".to_string());
let app_name = Path::new(&bin)
@ -43,6 +51,7 @@ fn main() {
.and_then(|name| name.to_str())
.unwrap_or("eh");
// If invoked as nr/ns/nb, dispatch directly and exit
match app_name {
"nr" => {
let rest: Vec<String> = args.collect();
@ -52,6 +61,7 @@ fn main() {
run::handle_nix_run(&rest, &hash_extractor, &fixer, &classifier);
return;
}
"ns" => {
let rest: Vec<String> = args.collect();
let hash_extractor = util::RegexHashExtractor;
@ -60,6 +70,7 @@ fn main() {
shell::handle_nix_shell(&rest, &hash_extractor, &fixer, &classifier);
return;
}
"nb" => {
let rest: Vec<String> = args.collect();
let hash_extractor = util::RegexHashExtractor;
@ -81,13 +92,16 @@ fn main() {
Some(Command::Run { args }) => {
run::handle_nix_run(&args, &hash_extractor, &fixer, &classifier);
}
Some(Command::Shell { args }) => {
shell::handle_nix_shell(&args, &hash_extractor, &fixer, &classifier);
}
Some(Command::Build { args }) => {
build::handle_nix_build(&args, &hash_extractor, &fixer, &classifier);
}
None => {
_ => {
Cli::command().print_help().unwrap();
println!();
std::process::exit(0);

View file

@ -1,6 +1,8 @@
use crate::command::{NixCommand, StdIoInterceptor};
use crate::util::{HashExtractor, NixErrorClassifier, NixFileFixer};
use std::io::Write;
use tracing::{info, warn};
use yansi::Paint;
pub fn handle_nix_run(
args: &[String],
@ -28,7 +30,7 @@ pub fn handle_nix_run(
if let Some(new_hash) = hash_extractor.extract_hash(&stderr) {
if fixer.fix_hash_in_files(&new_hash) {
eprintln!("\x1b[32m✔ Fixed hash mismatch, retrying...\x1b[0m");
info!("{}", Paint::green("✔ Fixed hash mismatch, retrying..."));
let retry_status = NixCommand::new("run")
.print_build_logs(true)
.args(args.iter().cloned())
@ -40,8 +42,9 @@ pub fn handle_nix_run(
if classifier.should_retry(&stderr) {
if stderr.contains("unfree") {
eprintln!(
"\x1b[33m⚠ Unfree package detected, retrying with NIXPKGS_ALLOW_UNFREE=1...\x1b[0m"
warn!(
"{}",
Paint::yellow("⚠ Unfree package detected, retrying with NIXPKGS_ALLOW_UNFREE=1...")
);
let retry_status = NixCommand::new("run")
.print_build_logs(true)
@ -53,8 +56,11 @@ pub fn handle_nix_run(
std::process::exit(retry_status.code().unwrap_or(1));
}
if stderr.contains("insecure") {
eprintln!(
"\x1b[33m⚠ Insecure package detected, retrying with NIXPKGS_ALLOW_INSECURE=1...\x1b[0m"
warn!(
"{}",
Paint::yellow(
"⚠ Insecure package detected, retrying with NIXPKGS_ALLOW_INSECURE=1..."
)
);
let retry_status = NixCommand::new("run")
.print_build_logs(true)
@ -66,8 +72,9 @@ pub fn handle_nix_run(
std::process::exit(retry_status.code().unwrap_or(1));
}
if stderr.contains("broken") {
eprintln!(
"\x1b[33m⚠ Broken package detected, retrying with NIXPKGS_ALLOW_BROKEN=1...\x1b[0m"
warn!(
"{}",
Paint::yellow("⚠ Broken package detected, retrying with NIXPKGS_ALLOW_BROKEN=1...")
);
let retry_status = NixCommand::new("run")
.print_build_logs(true)

View file

@ -1,6 +1,8 @@
use crate::command::{NixCommand, StdIoInterceptor};
use crate::util::{HashExtractor, NixErrorClassifier, NixFileFixer};
use std::io::Write;
use tracing::{info, warn};
use yansi::Paint;
pub fn handle_nix_shell(
args: &[String],
@ -28,7 +30,7 @@ pub fn handle_nix_shell(
if let Some(new_hash) = hash_extractor.extract_hash(&stderr) {
if fixer.fix_hash_in_files(&new_hash) {
eprintln!("\x1b[32m✔ Fixed hash mismatch, retrying...\x1b[0m");
info!("{}", Paint::green("✔ Fixed hash mismatch, retrying..."));
let retry_status = NixCommand::new("shell")
.print_build_logs(true)
.args(args.iter().cloned())
@ -40,8 +42,9 @@ pub fn handle_nix_shell(
if classifier.should_retry(&stderr) {
if stderr.contains("unfree") {
eprintln!(
"\x1b[33m⚠ Unfree package detected, retrying with NIXPKGS_ALLOW_UNFREE=1...\x1b[0m"
warn!(
"{}",
Paint::yellow("⚠ Unfree package detected, retrying with NIXPKGS_ALLOW_UNFREE=1...")
);
let retry_status = NixCommand::new("shell")
.print_build_logs(true)
@ -53,8 +56,11 @@ pub fn handle_nix_shell(
std::process::exit(retry_status.code().unwrap_or(1));
}
if stderr.contains("insecure") {
eprintln!(
"\x1b[33m⚠ Insecure package detected, retrying with NIXPKGS_ALLOW_INSECURE=1...\x1b[0m"
warn!(
"{}",
Paint::yellow(
"⚠ Insecure package detected, retrying with NIXPKGS_ALLOW_INSECURE=1..."
)
);
let retry_status = NixCommand::new("shell")
.print_build_logs(true)
@ -66,8 +72,9 @@ pub fn handle_nix_shell(
std::process::exit(retry_status.code().unwrap_or(1));
}
if stderr.contains("broken") {
eprintln!(
"\x1b[33m⚠ Broken package detected, retrying with NIXPKGS_ALLOW_BROKEN=1...\x1b[0m"
warn!(
"{}",
Paint::yellow("⚠ Broken package detected, retrying with NIXPKGS_ALLOW_BROKEN=1...")
);
let retry_status = NixCommand::new("shell")
.print_build_logs(true)