Skip to content

Commit

Permalink
Improved logging functions
Browse files Browse the repository at this point in the history
  • Loading branch information
olszomal committed Dec 19, 2024
1 parent 9c982d8 commit a29103e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/eng_back.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,12 @@ void ctx_log(ENGINE_CTX *ctx, int level, const char *format, ...)
{
va_list args;

if (!ctx)
return;

va_start(args, format);
if (ctx->vlog) {
if (!ctx) {
vfprintf(stderr, format, args);
} else if (ctx->vlog) {
/* Log messages through a custom logging function */
const char *prefix = "libp11: ";
const char *prefix = "pkcs11: ";
char *vlog_format = OPENSSL_malloc(strlen(prefix) + strlen(format) + 1);

if (!vlog_format) {
Expand All @@ -100,6 +99,7 @@ void ctx_log(ENGINE_CTX *ctx, int level, const char *format, ...)
vprintf(format, args);
}
}

va_end(args);
}

Expand Down
4 changes: 2 additions & 2 deletions src/eng_front.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,11 @@ static void exit_callback(void)
static int bind_fn(ENGINE *e, const char *id)
{
if (id && (strcmp(id, PKCS11_ENGINE_ID) != 0)) {
fprintf(stderr, "bad engine id\n");
ctx_log(NULL, LOG_ERR, "bad engine id\n");
return 0;
}
if (!bind_helper(e)) {
fprintf(stderr, "bind failed\n");
ctx_log(NULL, LOG_ERR, "bind failed\n");
return 0;
}
atexit(exit_callback);
Expand Down
2 changes: 2 additions & 0 deletions src/libp11-int.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ extern char *pkcs11_strdup(char *, size_t);
#define EVP_PKEY_get0_EC_KEY(key) ((key)->pkey.ec)
#endif

extern void pkcs11_log(PKCS11_CTX_private *pctx, int level, const char *format, ...);

/* Reinitializing the module after fork (if detected) */
extern unsigned int get_forkid();
extern int check_fork(PKCS11_CTX_private *ctx);
Expand Down
8 changes: 4 additions & 4 deletions src/p11_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ int pkcs11_atomic_add(int *value, int amount, pthread_mutex_t *lock)
#endif
}

void pkcs11_log(PKCS11_CTX *pctx, int level, const char *format, ...)
void pkcs11_log(PKCS11_CTX_private *pctx, int level, const char *format, ...)
{
va_list args;

va_start(args, format);
if (pctx && PRIVCTX(pctx)->vlog_a) {
if (pctx && pctx->vlog_a) {
/* Log messages through a custom logging function */
const char *prefix = "libp11: ";
char *vlog_format = OPENSSL_malloc(strlen(prefix) + strlen(format) + 1);
Expand All @@ -78,9 +78,9 @@ void pkcs11_log(PKCS11_CTX *pctx, int level, const char *format, ...)
strcpy(vlog_format, prefix);
strcat(vlog_format, format);

PRIVCTX(pctx)->vlog_a(level, (const char *)vlog_format, args);
pctx->vlog_a(level, (const char *)vlog_format, args);
OPENSSL_free(vlog_format);
} else if (level <= 3) { /* LOG_ERR */
} else if (level <= 4) { /* LOG_WARNING */
vfprintf(stderr, format, args);
} else if (level >= 7) { /* LOG_DEBUG */
#ifdef DEBUG
Expand Down

0 comments on commit a29103e

Please sign in to comment.