-
Notifications
You must be signed in to change notification settings - Fork 32
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
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ef1ed88
add redirect_stderr_to_file to logger
lijunwangs 4635a4d
bump the solana-logger version to 2.2.2
lijunwangs 836118b
ignore nits check in logger/src/lib.rs
lijunwangs 24ab763
do not add changes for wasm32
lijunwangs 058feb8
do not add changes for wasm32
lijunwangs cdfdd78
addressed some feedback from Jon
lijunwangs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -2,7 +2,11 @@ | |||||||
|
||||||||
use { | ||||||||
lazy_static::lazy_static, | ||||||||
std::sync::{Arc, RwLock}, | ||||||||
std::{ | ||||||||
env, | ||||||||
sync::{Arc, RwLock}, | ||||||||
thread::JoinHandle, | ||||||||
}, | ||||||||
}; | ||||||||
|
||||||||
lazy_static! { | ||||||||
|
@@ -75,3 +79,68 @@ pub fn setup_file_with_default(logfile: &str, filter: &str) { | |||||||
.build(); | ||||||||
replace_logger(logger); | ||||||||
} | ||||||||
|
||||||||
#[cfg(unix)] | ||||||||
#[cfg(not(target_arch = "wasm32"))] | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: these can get condensed into one
Suggested change
|
||||||||
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 | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't do the version bump here, the release job will take care of that automatically as mentioned at https://github.com/anza-xyz/solana-sdk?tab=readme-ov-file#publishing-a-crate-from-this-repository