Skip to content

Commit b28c40a

Browse files
committed
Perform logging initialisation during function entry
haproxy doesn't call `OPENSSL_init_ssl` (see their bug 2448) in the version ubuntu 24.04 packages, and it is not actually documented as being required anyway. As a result of this, avoid activating the `env_logger` default (where error-level logs are printed to stderr) which is noisy and interferes with tests.
1 parent 98b057e commit b28c40a

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

src/entry.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use core::{mem, ptr};
77
use std::io::{self, Read};
88
use std::os::raw::{c_char, c_int, c_long, c_uchar, c_uint, c_void};
99
use std::sync::Arc;
10+
use std::sync::OnceLock;
1011
use std::{fs, path::PathBuf};
1112

1213
use openssl_sys::{
@@ -48,6 +49,8 @@ macro_rules! entry {
4849
#[no_mangle]
4950
pub extern "C" fn $name($($aname: $aty),*) {
5051
ffi_panic_boundary! {
52+
one_time_init();
53+
5154
#[cfg(debug_assertions)]
5255
log::trace!("-> {}{:?}", &stringify!($name)[1..], &($($aname,)*));
5356
$body
@@ -60,6 +63,8 @@ macro_rules! entry {
6063
#[no_mangle]
6164
pub extern "C" fn $name($($aname: $aty),*) -> $ret {
6265
ffi_panic_boundary! {
66+
one_time_init();
67+
6368
#[cfg(debug_assertions)]
6469
log::trace!("-> {}{:?}", &stringify!($name)[1..], &($($aname,)*));
6570
let r = $body;
@@ -71,15 +76,24 @@ macro_rules! entry {
7176
};
7277
}
7378

79+
fn one_time_init() {
80+
static CELL: OnceLock<()> = OnceLock::new();
81+
82+
CELL.get_or_init(|| {
83+
const VERSION: &str = env!("CARGO_PKG_VERSION");
84+
85+
// logging is off unless `RUST_LOG` environment variable is set.
86+
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("off")).init();
87+
88+
log::trace!("Initialized rustls-libssl {VERSION}");
89+
});
90+
}
91+
7492
pub struct OpenSslInitSettings;
7593
type OPENSSL_INIT_SETTINGS = OpenSslInitSettings;
7694

7795
entry! {
7896
pub fn _OPENSSL_init_ssl(_opts: u64, _settings: *const OPENSSL_INIT_SETTINGS) -> c_int {
79-
const VERSION: &str = env!("CARGO_PKG_VERSION");
80-
81-
env_logger::init();
82-
log::trace!("OPENSSL_init_ssl in rustls-libssl {VERSION}");
8397
C_INT_SUCCESS
8498
}
8599
}
@@ -2088,6 +2102,7 @@ macro_rules! entry_stub {
20882102
#[no_mangle]
20892103
pub extern "C" fn $name($($aname: $aty),*) {
20902104
ffi_panic_boundary! {
2105+
one_time_init();
20912106
#[cfg(debug_assertions)]
20922107
log::trace!("!! {}{:?}", &stringify!($name)[1..], &($($aname,)*));
20932108
Error::not_supported(stringify!($name)).raise().into()
@@ -2098,6 +2113,7 @@ macro_rules! entry_stub {
20982113
#[no_mangle]
20992114
pub extern "C" fn $name($($aname: $aty),*) -> $ret {
21002115
ffi_panic_boundary! {
2116+
one_time_init();
21012117
#[cfg(debug_assertions)]
21022118
log::trace!("!! {}{:?}", &stringify!($name)[1..], &($($aname,)*));
21032119
Error::not_supported(stringify!($name)).raise().into()

0 commit comments

Comments
 (0)