Skip to content

Commit

Permalink
chore: Create new 'init' top-level module
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
alilleybrinker authored and mchernicoff committed Aug 29, 2024
1 parent 7a23a93 commit 1362405
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 33 deletions.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
49 changes: 49 additions & 0 deletions hipcheck/src/init/mod.rs
Original file line number Diff line number Diff line change
@@ -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");
}
33 changes: 2 additions & 31 deletions hipcheck/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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.");
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 1362405

Please sign in to comment.