Skip to content

Commit

Permalink
Support runtime pluggable logging callback
Browse files Browse the repository at this point in the history
Allow setting up a callback function that receives logging messages, with
the current behavior of printing to stdout and stderr as a default.
  • Loading branch information
igorkh-fb committed Dec 20, 2023
1 parent ebff055 commit 0c5d6ea
Show file tree
Hide file tree
Showing 17 changed files with 95 additions and 31 deletions.
33 changes: 33 additions & 0 deletions include/logging/logging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

typedef enum {
COSIGNER_LOG_LEVEL_FATAL = 50000,
COSIGNER_LOG_LEVEL_ERROR = 40000,
COSIGNER_LOG_LEVEL_WARN = 30000,
COSIGNER_LOG_LEVEL_INFO = 20000,
COSIGNER_LOG_LEVEL_DEBUG = 10000,
COSIGNER_LOG_LEVEL_TRACE = 5000,
} COSIGNER_LOG_LEVEL;

typedef void (*cosigner_log_callback)(int level, const char* file, int line, const char* func, const char* message, void* userp);

#ifdef __cplusplus
extern "C" {
#endif //__cplusplus

void cosigner_log_init(cosigner_log_callback cb, void* userp);

void cosigner_log_msg(int level, const char* file, int line, const char* func, const char* message, ...)
__attribute__ ((format (printf, 5, 6)));

#ifdef __cplusplus
}
#endif //__cplusplus

#define LOG(level, message, ...) cosigner_log_msg((level), __FILE__, __LINE__, __func__, (message), ##__VA_ARGS__)
#define LOG_TRACE(message, ...) LOG(COSIGNER_LOG_LEVEL_TRACE, message, ##__VA_ARGS__)
#define LOG_DEBUG(message, ...) LOG(COSIGNER_LOG_LEVEL_DEBUG, message, ##__VA_ARGS__)
#define LOG_INFO(message, ...) LOG(COSIGNER_LOG_LEVEL_INFO, message, ##__VA_ARGS__)
#define LOG_WARN(message, ...) LOG(COSIGNER_LOG_LEVEL_WARN, message, ##__VA_ARGS__)
#define LOG_ERROR(message, ...) LOG(COSIGNER_LOG_LEVEL_ERROR, message, ##__VA_ARGS__)
#define LOG_FATAL(message, ...) LOG(COSIGNER_LOG_LEVEL_FATAL, message, ##__VA_ARGS__)
16 changes: 0 additions & 16 deletions include/logging/logging_t.h

This file was deleted.

6 changes: 3 additions & 3 deletions src/common/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ endif
COMMON_CFLAGS := $(COMMON_CFLAGS)

C_Files := crypto/shamir_secret_sharing/verifiable_secret_sharing.c crypto/paillier/paillier.c crypto/paillier/paillier_zkp.c crypto/GFp_curve_algebra/GFp_curve_algebra.c \
crypto/commitments/commitments.c crypto/zero_knowledge_proof/schnorr.c crypto/zero_knowledge_proof/range_proofs.c crypto/zero_knowledge_proof/diffie_hellman_log.c \
crypto/commitments/ring_pedersen.c crypto/ed25519_algebra/ed25519_algebra.c crypto/drng/drng.c crypto/keccak1600/keccak1600.c
crypto/commitments/commitments.c crypto/zero_knowledge_proof/schnorr.c crypto/zero_knowledge_proof/range_proofs.c crypto/zero_knowledge_proof/diffie_hellman_log.c \
crypto/commitments/ring_pedersen.c crypto/ed25519_algebra/ed25519_algebra.c crypto/drng/drng.c crypto/keccak1600/keccak1600.c logging/logging.c

C_Objects := $(C_Files:.c=.o)

Cpp_Files := cosigner/cosigner_exception.cpp cosigner/cmp_setup_service.cpp cosigner/cmp_offline_refresh_service.cpp cosigner/utils.cpp \
Expand Down
2 changes: 1 addition & 1 deletion src/common/cosigner/asymmetric_eddsa_cosigner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "cosigner/cmp_key_persistency.h"
#include "cosigner/cosigner_exception.h"
#include "cosigner/platform_service.h"
#include "logging/logging_t.h"
#include "logging/logging.h"

#include <openssl/sha.h>

Expand Down
2 changes: 1 addition & 1 deletion src/common/cosigner/asymmetric_eddsa_cosigner_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "cosigner/cosigner_exception.h"
#include "cosigner/platform_service.h"
#include "cosigner/mpc_globals.h"
#include "logging/logging_t.h"
#include "logging/logging.h"
#include "utils.h"

#include <openssl/sha.h>
Expand Down
2 changes: 1 addition & 1 deletion src/common/cosigner/asymmetric_eddsa_cosigner_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "cosigner/platform_service.h"
#include "cosigner/mpc_globals.h"
#include "utils.h"
#include "logging/logging_t.h"
#include "logging/logging.h"

#include <openssl/sha.h>

Expand Down
2 changes: 1 addition & 1 deletion src/common/cosigner/cmp_ecdsa_offline_signing_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "utils.h"
#include "crypto/GFp_curve_algebra/GFp_curve_algebra.h"
#include "crypto/zero_knowledge_proof/range_proofs.h"
#include "logging/logging_t.h"
#include "logging/logging.h"

namespace fireblocks
{
Expand Down
2 changes: 1 addition & 1 deletion src/common/cosigner/cmp_ecdsa_online_signing_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "crypto/GFp_curve_algebra/GFp_curve_algebra.h"
#include "crypto/zero_knowledge_proof/range_proofs.h"
#include "utils.h"
#include "logging/logging_t.h"
#include "logging/logging.h"

namespace fireblocks
{
Expand Down
2 changes: 1 addition & 1 deletion src/common/cosigner/cmp_ecdsa_signing_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "crypto/GFp_curve_algebra/GFp_curve_algebra.h"
#include "crypto/zero_knowledge_proof/diffie_hellman_log.h"
#include "crypto/zero_knowledge_proof/range_proofs.h"
#include "logging/logging_t.h"
#include "logging/logging.h"

#include <openssl/sha.h>

Expand Down
2 changes: 1 addition & 1 deletion src/common/cosigner/cmp_offline_refresh_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "cosigner/platform_service.h"
#include "cosigner/prf.h"
#include "crypto/elliptic_curve_algebra/elliptic_curve256_algebra.h"
#include "logging/logging_t.h"
#include "logging/logging.h"

namespace fireblocks
{
Expand Down
2 changes: 1 addition & 1 deletion src/common/cosigner/cmp_setup_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "cosigner/cosigner_exception.h"
#include "utils.h"
#include "crypto/zero_knowledge_proof/schnorr.h"
#include "logging/logging_t.h"
#include "logging/logging.h"

#include <openssl/sha.h>

Expand Down
2 changes: 1 addition & 1 deletion src/common/cosigner/cosigner_exception.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "cosigner/cosigner_exception.h"
#include "crypto/paillier/paillier.h"
#include "logging/logging_t.h"
#include "logging/logging.h"

namespace fireblocks
{
Expand Down
2 changes: 1 addition & 1 deletion src/common/cosigner/eddsa_online_signing_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "cosigner/mpc_globals.h"
#include "cosigner/platform_service.h"
#include "utils.h"
#include "logging/logging_t.h"
#include "logging/logging.h"

extern "C" int gettimeofday(struct timeval *tv, struct timezone *tz);

Expand Down
2 changes: 1 addition & 1 deletion src/common/cosigner/mta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "../crypto/paillier/paillier_internal.h"

#ifndef TEST_ONLY
#include "logging/logging_t.h"
#include "logging/logging.h"
#else
#define LOG_ERROR(message, ...) printf((message), ##__VA_ARGS__);putchar('\n')
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/common/cosigner/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "cosigner/cmp_key_persistency.h"
#include "cosigner/cosigner_exception.h"
#include "cosigner/platform_service.h"
#include "logging/logging_t.h"
#include "logging/logging.h"

namespace fireblocks
{
Expand Down
1 change: 1 addition & 0 deletions src/common/lib.lds
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ libcosigner.so
range_proof_*;
schnorr_zkp_*;
derive_*;
cosigner_log_*;
_ZN10fireblocks6common8cosigner17*;
_ZN10fireblocks6common8cosigner2*;
_ZN10fireblocks6common8cosigner32*;
Expand Down
46 changes: 46 additions & 0 deletions src/common/logging/logging.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "logging/logging.h"

#include <stdarg.h>
#include <stdio.h>

#define MAX_LOG_SIZE 4096

static void default_log_callback(int level, const char* file, int line, const char* func, const char* message, void* userp)
{
(void)file;
(void)line;
(void)func;
(void)userp;

if (level >= COSIGNER_LOG_LEVEL_ERROR)
fprintf(stderr, "%s\n", message);
else
printf("%s\n", message);
}

static cosigner_log_callback log_callback = default_log_callback;
static void* log_callback_user_data_pointer = NULL;

void cosigner_log_init(cosigner_log_callback cb, void* userp)
{
log_callback = cb;
log_callback_user_data_pointer = userp;
}

void cosigner_log_msg(int level, const char* file, int line, const char* func, const char* message, ...)
{
va_list args;
char buffer[MAX_LOG_SIZE] = { '\0' };

if (log_callback == NULL)
return;

if (message != NULL)
{
va_start(args, message);
vsnprintf(buffer, MAX_LOG_SIZE, message, args);
va_end(args);
}

log_callback(level, file, line, func, buffer, log_callback_user_data_pointer);
}

0 comments on commit 0c5d6ea

Please sign in to comment.