Browse Source

logger: Update functions to take PathBuf/Path (#8726)

Using these types is more proper than the current use of String and &str
in the arguments
steviez 3 weeks ago
parent
commit
8d1b74edf6

+ 5 - 4
logger/src/lib.rs

@@ -2,6 +2,7 @@
 #![cfg(feature = "agave-unstable-api")]
 #![cfg(feature = "agave-unstable-api")]
 use std::{
 use std::{
     env,
     env,
+    path::{Path, PathBuf},
     sync::{Arc, LazyLock, RwLock},
     sync::{Arc, LazyLock, RwLock},
     thread::JoinHandle,
     thread::JoinHandle,
 };
 };
@@ -61,7 +62,7 @@ pub fn setup() {
 }
 }
 
 
 // Configures file logging with a default filter if RUST_LOG is not set
 // Configures file logging with a default filter if RUST_LOG is not set
-pub fn setup_file_with_default(logfile: &str, filter: &str) {
+pub fn setup_file_with_default(logfile: &Path, filter: &str) {
     use std::fs::OpenOptions;
     use std::fs::OpenOptions;
     let file = OpenOptions::new()
     let file = OpenOptions::new()
         .create(true)
         .create(true)
@@ -76,20 +77,20 @@ pub fn setup_file_with_default(logfile: &str, filter: &str) {
 }
 }
 
 
 #[cfg(unix)]
 #[cfg(unix)]
-fn redirect_stderr(filename: &str) {
+fn redirect_stderr(filename: &Path) {
     use std::{fs::OpenOptions, os::unix::io::AsRawFd};
     use std::{fs::OpenOptions, os::unix::io::AsRawFd};
     match OpenOptions::new().create(true).append(true).open(filename) {
     match OpenOptions::new().create(true).append(true).open(filename) {
         Ok(file) => unsafe {
         Ok(file) => unsafe {
             libc::dup2(file.as_raw_fd(), libc::STDERR_FILENO);
             libc::dup2(file.as_raw_fd(), libc::STDERR_FILENO);
         },
         },
-        Err(err) => eprintln!("Unable to open {filename}: {err}"),
+        Err(err) => eprintln!("Unable to open {}: {err}", filename.display()),
     }
     }
 }
 }
 
 
 // Redirect stderr to a file with support for logrotate by sending a SIGUSR1 to the process.
 // 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.
 // 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<()>> {
+pub fn redirect_stderr_to_file(logfile: Option<PathBuf>) -> Option<JoinHandle<()>> {
     // Default to RUST_BACKTRACE=1 for more informative validator logs
     // Default to RUST_BACKTRACE=1 for more informative validator logs
     if env::var_os("RUST_BACKTRACE").is_none() {
     if env::var_os("RUST_BACKTRACE").is_none() {
         env::set_var("RUST_BACKTRACE", "1")
         env::set_var("RUST_BACKTRACE", "1")

+ 1 - 7
validator/src/bin/solana-test-validator.rs

@@ -118,13 +118,7 @@ fn main() {
         let _ = fs::remove_file(&validator_log_symlink);
         let _ = fs::remove_file(&validator_log_symlink);
         symlink::symlink_file(&validator_log_with_timestamp, &validator_log_symlink).unwrap();
         symlink::symlink_file(&validator_log_with_timestamp, &validator_log_symlink).unwrap();
 
 
-        Some(
-            ledger_path
-                .join(validator_log_with_timestamp)
-                .into_os_string()
-                .into_string()
-                .unwrap(),
-        )
+        Some(ledger_path.join(validator_log_with_timestamp))
     } else {
     } else {
         None
         None
     };
     };

+ 16 - 9
validator/src/commands/run/args.rs

@@ -67,7 +67,7 @@ pub mod send_transaction_config;
 pub struct RunArgs {
 pub struct RunArgs {
     pub identity_keypair: Keypair,
     pub identity_keypair: Keypair,
     pub ledger_path: PathBuf,
     pub ledger_path: PathBuf,
-    pub logfile: String,
+    pub logfile: Option<PathBuf>,
     pub entrypoints: Vec<SocketAddr>,
     pub entrypoints: Vec<SocketAddr>,
     pub known_validators: Option<HashSet<Pubkey>>,
     pub known_validators: Option<HashSet<Pubkey>>,
     pub socket_addr_space: SocketAddrSpace,
     pub socket_addr_space: SocketAddrSpace,
@@ -103,8 +103,13 @@ impl FromClapArgMatches for RunArgs {
 
 
         let logfile = matches
         let logfile = matches
             .value_of("logfile")
             .value_of("logfile")
-            .map(|s| s.into())
+            .map(String::from)
             .unwrap_or_else(|| format!("agave-validator-{}.log", identity_keypair.pubkey()));
             .unwrap_or_else(|| format!("agave-validator-{}.log", identity_keypair.pubkey()));
+        let logfile = if logfile == "-" {
+            None
+        } else {
+            Some(PathBuf::from(logfile))
+        };
 
 
         let mut entrypoints = values_t!(matches, "entrypoint", String).unwrap_or_default();
         let mut entrypoints = values_t!(matches, "entrypoint", String).unwrap_or_default();
         // sort() + dedup() to yield a vector of unique elements
         // sort() + dedup() to yield a vector of unique elements
@@ -1419,7 +1424,8 @@ mod tests {
         fn default() -> Self {
         fn default() -> Self {
             let identity_keypair = Keypair::new();
             let identity_keypair = Keypair::new();
             let ledger_path = absolute(PathBuf::from("ledger")).unwrap();
             let ledger_path = absolute(PathBuf::from("ledger")).unwrap();
-            let logfile = format!("agave-validator-{}.log", identity_keypair.pubkey());
+            let logfile =
+                PathBuf::from(format!("agave-validator-{}.log", identity_keypair.pubkey()));
             let entrypoints = vec![];
             let entrypoints = vec![];
             let known_validators = None;
             let known_validators = None;
 
 
@@ -1429,7 +1435,7 @@ mod tests {
             RunArgs {
             RunArgs {
                 identity_keypair,
                 identity_keypair,
                 ledger_path,
                 ledger_path,
-                logfile,
+                logfile: Some(logfile),
                 entrypoints,
                 entrypoints,
                 known_validators,
                 known_validators,
                 socket_addr_space: SocketAddrSpace::Global,
                 socket_addr_space: SocketAddrSpace::Global,
@@ -1652,9 +1658,10 @@ mod tests {
         // default
         // default
         {
         {
             let expected_args = RunArgs {
             let expected_args = RunArgs {
-                logfile: "agave-validator-".to_string()
-                    + &default_run_args.identity_keypair.pubkey().to_string()
-                    + ".log",
+                logfile: Some(PathBuf::from(format!(
+                    "agave-validator-{}.log",
+                    default_run_args.identity_keypair.pubkey()
+                ))),
                 ..default_run_args.clone()
                 ..default_run_args.clone()
             };
             };
             verify_args_struct_by_command_run_with_identity_setup(
             verify_args_struct_by_command_run_with_identity_setup(
@@ -1667,7 +1674,7 @@ mod tests {
         // short arg
         // short arg
         {
         {
             let expected_args = RunArgs {
             let expected_args = RunArgs {
-                logfile: "-".to_string(),
+                logfile: None,
                 ..default_run_args.clone()
                 ..default_run_args.clone()
             };
             };
             verify_args_struct_by_command_run_with_identity_setup(
             verify_args_struct_by_command_run_with_identity_setup(
@@ -1680,7 +1687,7 @@ mod tests {
         // long arg
         // long arg
         {
         {
             let expected_args = RunArgs {
             let expected_args = RunArgs {
-                logfile: "custom_log.log".to_string(),
+                logfile: Some(PathBuf::from("custom_log.log")),
                 ..default_run_args.clone()
                 ..default_run_args.clone()
             };
             };
             verify_args_struct_by_command_run_with_identity_setup(
             verify_args_struct_by_command_run_with_identity_setup(

+ 3 - 6
validator/src/commands/run/execute.rs

@@ -116,12 +116,9 @@ pub fn execute(
     let identity_keypair = Arc::new(run_args.identity_keypair);
     let identity_keypair = Arc::new(run_args.identity_keypair);
 
 
     let logfile = run_args.logfile;
     let logfile = run_args.logfile;
-    let logfile = if logfile == "-" {
-        None
-    } else {
-        println!("log file: {logfile}");
-        Some(logfile)
-    };
+    if let Some(logfile) = logfile.as_ref() {
+        println!("log file: {}", logfile.display());
+    }
     let use_progress_bar = logfile.is_none();
     let use_progress_bar = logfile.is_none();
     let _logger_thread = redirect_stderr_to_file(logfile);
     let _logger_thread = redirect_stderr_to_file(logfile);
 
 

+ 2 - 1
vortexor/src/main.rs

@@ -23,6 +23,7 @@ use {
         collections::HashMap,
         collections::HashMap,
         env,
         env,
         net::{IpAddr, SocketAddr},
         net::{IpAddr, SocketAddr},
+        path::PathBuf,
         sync::{atomic::AtomicBool, Arc, RwLock},
         sync::{atomic::AtomicBool, Arc, RwLock},
     },
     },
     tokio_util::sync::CancellationToken,
     tokio_util::sync::CancellationToken,
@@ -54,7 +55,7 @@ pub fn main() {
             None
             None
         } else {
         } else {
             println!("log file: {logfile}");
             println!("log file: {logfile}");
-            Some(logfile)
+            Some(PathBuf::from(logfile))
         }
         }
     };
     };
     let _logger_thread = redirect_stderr_to_file(logfile);
     let _logger_thread = redirect_stderr_to_file(logfile);