From 1362405054ef0d399dc448ba6e78ffd8622ca06e Mon Sep 17 00:00:00 2001 From: Andrew Lilley Brinker Date: Wed, 28 Aug 2024 16:39:19 -0700 Subject: [PATCH] chore: Create new 'init' top-level module Over time we've accrued a number of pieces of logic for pieces of global state that needs to be initialized right at the start of execution. These pieces all init-related but scattered around modules in a way that meant more clutter at the top-level of the crate, and a general lack of clarity about how init was done. This refactoring groups all init-related logic under an `init` function in the `init` module, which is just called right at the top of the `main` function. Signed-off-by: Andrew Lilley Brinker --- hipcheck/src/{ => init}/git2_log_shim.rs | 0 .../src/{ => init}/git2_rustls_transport.rs | 0 .../indicatif_log_bridge.rs} | 3 +- hipcheck/src/init/mod.rs | 49 +++++++++++++++++++ hipcheck/src/main.rs | 33 +------------ 5 files changed, 52 insertions(+), 33 deletions(-) rename hipcheck/src/{ => init}/git2_log_shim.rs (100%) rename hipcheck/src/{ => init}/git2_rustls_transport.rs (100%) rename hipcheck/src/{log_bridge.rs => init/indicatif_log_bridge.rs} (99%) create mode 100644 hipcheck/src/init/mod.rs diff --git a/hipcheck/src/git2_log_shim.rs b/hipcheck/src/init/git2_log_shim.rs similarity index 100% rename from hipcheck/src/git2_log_shim.rs rename to hipcheck/src/init/git2_log_shim.rs diff --git a/hipcheck/src/git2_rustls_transport.rs b/hipcheck/src/init/git2_rustls_transport.rs similarity index 100% rename from hipcheck/src/git2_rustls_transport.rs rename to hipcheck/src/init/git2_rustls_transport.rs diff --git a/hipcheck/src/log_bridge.rs b/hipcheck/src/init/indicatif_log_bridge.rs similarity index 99% rename from hipcheck/src/log_bridge.rs rename to hipcheck/src/init/indicatif_log_bridge.rs index ca7156ed..aaf4a27b 100644 --- a/hipcheck/src/log_bridge.rs +++ b/hipcheck/src/init/indicatif_log_bridge.rs @@ -1,10 +1,9 @@ //! The `indicatif-log-bridge` crate discards filtering information when using env_logger, so I'm writting my own. +use crate::shell::Shell; use env_logger::Logger; use log::SetLoggerError; -use crate::shell::Shell; - pub struct LogWrapper(pub Logger); impl log::Log for LogWrapper { diff --git a/hipcheck/src/init/mod.rs b/hipcheck/src/init/mod.rs new file mode 100644 index 00000000..e8cb797c --- /dev/null +++ b/hipcheck/src/init/mod.rs @@ -0,0 +1,49 @@ +mod git2_log_shim; +mod git2_rustls_transport; +mod indicatif_log_bridge; + +use crate::shell::verbosity::Verbosity; +use crate::shell::Shell; +use env_logger::Env; +use rustls::crypto::ring; +use rustls::crypto::CryptoProvider; + +/// Initialize global state for the program. +/// +/// **NOTE:** The order in which these operations are done is precise, and +/// should not be changed! +pub fn init() { + init_shell(); + init_logging(); + init_libgit2(); + init_cryptography(); +} + +fn init_shell() { + Shell::init(Verbosity::Normal); +} + +fn init_logging() { + let env = Env::new().filter("HC_LOG").write_style("HC_LOG_STYLE"); + let logger = env_logger::Builder::from_env(env).build(); + indicatif_log_bridge::LogWrapper(logger) + .try_init() + .expect("logging initialization must succeed"); +} + +fn init_libgit2() { + // Tell the `git2` crate to pass its tracing messages to the log crate. + git2_log_shim::git2_set_trace_log_shim(); + + // Make libgit2 use a rustls + ureq based transport for executing the git + // protocol over http(s). I would normally just let libgit2 use its own + // implementation but have seen that this rustls/ureq transport is 2-3 times + // faster on my machine — enough of a performance bump to warrant using this. + git2_rustls_transport::register(); +} + +fn init_cryptography() { + // Install a process-wide default crypto provider. + CryptoProvider::install_default(ring::default_provider()) + .expect("installed process-wide default crypto provider"); +} diff --git a/hipcheck/src/main.rs b/hipcheck/src/main.rs index 4c4fc861..7c72b1fd 100644 --- a/hipcheck/src/main.rs +++ b/hipcheck/src/main.rs @@ -12,9 +12,7 @@ mod context; mod data; mod engine; mod error; -mod git2_log_shim; -mod git2_rustls_transport; -mod log_bridge; +mod init; mod metric; #[allow(unused)] mod plugin; @@ -44,7 +42,6 @@ use crate::error::Result; use crate::plugin::{Plugin, PluginExecutor, PluginWithConfig}; use crate::session::Session; use crate::setup::{resolve_and_transform_source, SourceType}; -use crate::shell::verbosity::Verbosity; use crate::shell::Shell; use crate::util::iter::TryAny; use crate::util::iter::TryFilter; @@ -61,13 +58,10 @@ use command_util::DependentProgram; use config::WeightTreeNode; use config::WeightTreeProvider; use core::fmt; -use env_logger::Env; use indextree::Arena; use indextree::NodeId; use ordered_float::NotNan; use pathbuf::pathbuf; -use rustls::crypto::ring; -use rustls::crypto::CryptoProvider; use schemars::schema_for; use shell::color_choice::ColorChoice; use shell::spinner_phase::SpinnerPhase; @@ -88,24 +82,7 @@ use which::which; /// Entry point for Hipcheck. fn main() -> ExitCode { - // Initialize the global shell with normal verbosity by default. - Shell::init(Verbosity::Normal); - // Initialize logging. - // This must be done after shell initialization. - // Panic if this fails. - init_logging().unwrap(); - - // Tell the git2 crate to pass its tracing messages to the log crate. - git2_log_shim::git2_set_trace_log_shim(); - - // Make libgit2 use a rustls + ureq based transport for executing the git protocol over http(s). - // I would normally just let libgit2 use its own implementation but have seen that this rustls/ureq transport is - // 2-3 times faster on my machine -- enough of a performance bump to warrant using this. - git2_rustls_transport::register(); - - // Install a process-wide default crypto provider. - CryptoProvider::install_default(ring::default_provider()) - .expect("installed process-wide default crypto provider"); + init::init(); if cfg!(feature = "print-timings") { Shell::eprintln("[TIMINGS]: Timing information will be printed."); @@ -154,12 +131,6 @@ fn main() -> ExitCode { ExitCode::SUCCESS } -fn init_logging() -> std::result::Result<(), log::SetLoggerError> { - let env = Env::new().filter("HC_LOG").write_style("HC_LOG_STYLE"); - let logger = env_logger::Builder::from_env(env).build(); - log_bridge::LogWrapper(logger).try_init() -} - /// Run the `check` command. fn cmd_check(args: &CheckArgs, config: &CliConfig) -> ExitCode { let target = match args.to_target_seed() {