Skip to content

Commit

Permalink
add redirect_stderr_to_file to logger
Browse files Browse the repository at this point in the history
  • Loading branch information
lijunwangs committed Mar 3, 2025
1 parent 4b1ade7 commit ef1ed88
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 3 deletions.
25 changes: 23 additions & 2 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 @@ -161,6 +161,7 @@ indicatif = "0.17.9"
itertools = "0.12.1"
js-sys = "0.3.77"
lazy_static = "1.5.0"
libc = "0.2.170"
libsecp256k1 = { version = "0.6.0", default-features = false, features = [
"std",
"static-context",
Expand Down Expand Up @@ -192,6 +193,7 @@ serde_with = { version = "3.12.0", default-features = false }
serial_test = "2.0.0"
sha2 = "0.10.8"
sha3 = "0.10.8"
signal-hook = "0.3.17"
siphasher = "0.3.11"
solana-account = { path = "account", version = "2.2.1" }
solana-account-info = { path = "account-info", version = "2.2.1" }
Expand Down
2 changes: 2 additions & 0 deletions logger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ edition = { workspace = true }
[dependencies]
env_logger = { workspace = true }
lazy_static = { workspace = true }
libc = { workspace = true }
log = { workspace = true }
signal-hook = { workspace = true }

[lib]
name = "solana_logger"
Expand Down
69 changes: 68 additions & 1 deletion logger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
use {
lazy_static::lazy_static,
std::sync::{Arc, RwLock},
std::{
env,
sync::{Arc, RwLock},
thread::JoinHandle,
},
};

lazy_static! {
Expand Down Expand Up @@ -75,3 +79,66 @@ pub fn setup_file_with_default(logfile: &str, filter: &str) {
.build();
replace_logger(logger);
}

#[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 => {
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);
});

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");
setup_file_with_default(&logfile, solana_logger::DEFAULT_FILTER);
None
}
}
}
}

0 comments on commit ef1ed88

Please sign in to comment.