Skip to content

Early logging initialization #390

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions native-pkcs11/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ license.workspace = true
custom-function-list = []

[dependencies]
ctor = { version = "0.2" }
cached = { version = "~0.54", default-features = false }
native-pkcs11-core = { version = "^0.2.24", path = "../native-pkcs11-core" }
native-pkcs11-traits = { version = "0.2.0", path = "../native-pkcs11-traits" }
Expand Down
64 changes: 34 additions & 30 deletions native-pkcs11/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#![allow(clippy::missing_safety_doc)]
#![deny(unsafe_op_in_unsafe_fn)]

use ctor::ctor;
pub use native_pkcs11_core::Error;
use native_pkcs11_traits::backend;
use tracing::metadata::LevelFilter;
Expand All @@ -28,10 +29,7 @@ mod utils;
use std::{
cmp,
slice,
sync::{
atomic::{AtomicBool, Ordering},
Once,
},
sync::atomic::{AtomicBool, Ordering},
};

use native_pkcs11_core::{
Expand Down Expand Up @@ -209,35 +207,41 @@ pub static mut FUNC_LIST: CK_FUNCTION_LIST = CK_FUNCTION_LIST {
C_WaitForSlotEvent: Some(C_WaitForSlotEvent),
};

static TRACING_INIT: Once = Once::new();
#[ctor]
fn init_tracing() {
let env_filter = EnvFilter::builder()
.with_default_directive(LevelFilter::WARN.into())
.from_env_lossy();
let force_stderr = std::env::var("NATIVE_PKCS11_LOG_STDERR").is_ok();
if !force_stderr {
if let Ok(journald_layer) = tracing_journald::layer() {
let subscriber = Registry::default()
.with(journald_layer.with_syslog_identifier("native-pkcs11".into()))
.with(env_filter)
.with(ErrorLayer::default());
if let Err(e) = tracing::subscriber::set_global_default(subscriber) {
eprintln!("failed to initialize logging: {e}");
}
return;
}
}

let subscriber = Registry::default()
.with(
tracing_subscriber::fmt::layer()
.with_writer(std::io::stderr)
.with_span_events(FmtSpan::ENTER),
)
.with(env_filter)
.with(ErrorLayer::default());

if let Err(e) = tracing::subscriber::set_global_default(subscriber) {
eprintln!("failed to initialize logging: {e}");
}
}

cryptoki_fn!(
fn C_Initialize(pInitArgs: CK_VOID_PTR) {
TRACING_INIT.call_once(|| {
let env_filter = EnvFilter::builder()
.with_default_directive(LevelFilter::WARN.into())
.from_env_lossy();
let force_stderr = std::env::var("NATIVE_PKCS11_LOG_STDERR").is_ok();
if !force_stderr {
if let Ok(journald_layer) = tracing_journald::layer() {
_ = Registry::default()
.with(journald_layer.with_syslog_identifier("native-pkcs11".into()))
.with(env_filter)
.with(ErrorLayer::default())
.try_init();
return;
}
}
_ = Registry::default()
.with(
tracing_subscriber::fmt::layer()
.with_writer(std::io::stderr)
.with_span_events(FmtSpan::ENTER),
)
.with(env_filter)
.with(ErrorLayer::default())
.try_init();
});
if !pInitArgs.is_null() {
let args = unsafe { *(pInitArgs as CK_C_INITIALIZE_ARGS_PTR) };
if !args.pReserved.is_null() {
Expand Down