-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support runtime pluggable logging callback
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
Showing
17 changed files
with
95 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |