forked from solana-labs/solana
-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dabf02e
commit 4a04f34
Showing
3 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ log-*.txt | |
log-*/ | ||
!log-collector/ | ||
!log-analyzer/* | ||
!log-utils/ | ||
|
||
# intellij files | ||
.idea/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |