Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor redirect_stderr_to_file to solana-logger #64

Merged
merged 6 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 4 additions & 0 deletions logger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ env_logger = { workspace = true }
lazy_static = { workspace = true }
log = { workspace = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
libc = { workspace = true }
signal-hook = { workspace = true }

[lib]
name = "solana_logger"

Expand Down
70 changes: 69 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,67 @@ pub fn setup_file_with_default(logfile: &str, filter: &str) {
.build();
replace_logger(logger);
}

#[cfg(all(unix, not(target_arch = "wasm32")))]
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.
#[cfg(not(target_arch = "wasm32"))]
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
}
}
}
}
1 change: 1 addition & 0 deletions scripts/check-nits.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ declare prints=(
# Parts of the tree that are expected to be print free
declare print_free_tree=(
':**.rs'
':^logger/src/lib.rs'
':^msg/src/lib.rs'
':^program-option/src/lib.rs'
':^pubkey/src/lib.rs'
Expand Down