Skip to content

Commit

Permalink
missing library files
Browse files Browse the repository at this point in the history
  • Loading branch information
lijunwangs committed Feb 27, 2025
1 parent 11357c4 commit 3201fed
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ log-*.txt
log-*/
!log-collector/
!log-analyzer/*
!log-utils/

# intellij files
.idea/
Expand Down
25 changes: 25 additions & 0 deletions log-utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "solana-log-utils"
description = "Solana Logging Utilitites"
documentation = "https://docs.rs/solana-log-utils"
publish = false
version = { workspace = true }
authors = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
edition = { workspace = true }

[dependencies]
clap = { workspace = true }
libc = { workspace = true }
log = { workspace = true }
signal-hook = { workspace = true }
solana-logger = { workspace = true }

[lib]
crate-type = ["lib"]
name = "solana_log_utils"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
64 changes: 64 additions & 0 deletions log-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use std::{env, thread::JoinHandle};

#[cfg(unix)]
fn redirect_stderr(filename: &str) {
use std::{fs::OpenOptions, os::unix::io::AsRawFd};
match OpenOptions::new().create(true).append(true).open(filename) {
Ok(file) => unsafe {
libc::dup2(file.as_raw_fd(), libc::STDERR_FILENO);
},
Err(err) => eprintln!("Unable to open {filename}: {err}"),
}
}

// Redirect stderr to a file with support for logrotate by sending a SIGUSR1 to the process.
//
// Upon success, future `log` macros and `eprintln!()` can be found in the specified log file.
pub fn redirect_stderr_to_file(logfile: Option<String>) -> Option<JoinHandle<()>> {
// Default to RUST_BACKTRACE=1 for more informative validator logs
if env::var_os("RUST_BACKTRACE").is_none() {
env::set_var("RUST_BACKTRACE", "1")
}

match logfile {
None => {
solana_logger::setup_with_default_filter();
None
}
Some(logfile) => {
#[cfg(unix)]
{
use log::info;
let mut signals =
signal_hook::iterator::Signals::new([signal_hook::consts::SIGUSR1])
.unwrap_or_else(|err| {
eprintln!("Unable to register SIGUSR1 handler: {err:?}");
std::process::exit(1);
});

solana_logger::setup_with_default_filter();
redirect_stderr(&logfile);
Some(
std::thread::Builder::new()
.name("solSigUsr1".into())
.spawn(move || {
for signal in signals.forever() {
info!(
"received SIGUSR1 ({}), reopening log file: {:?}",
signal, logfile
);
redirect_stderr(&logfile);
}
})
.unwrap(),
)
}
#[cfg(not(unix))]
{
println!("logrotate is not supported on this platform");
solana_logger::setup_file_with_default(&logfile, solana_logger::DEFAULT_FILTER);
None
}
}
}
}

0 comments on commit 3201fed

Please sign in to comment.