Skip to content

Commit

Permalink
logging: Add handler pointer for logging
Browse files Browse the repository at this point in the history
Default to old stderr logging method.
Add function pointer and userdata pointer as static globals.
  • Loading branch information
bwrsandman committed Jan 3, 2024
1 parent 950b463 commit 54e2ae3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/libunshield.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ typedef struct _Unshield Unshield;

#define UNSHIELD_LOG_LEVEL_HIGHEST 4

typedef void (UnshieldLogCallback)(void* userdata, int level, const char* file, int line, const char* format, ...);
void unshield_set_log_level(int level);
void unshield_set_log_handler(UnshieldLogCallback* handler, void* userdata);

/*
Open/close functions
Expand Down
23 changes: 22 additions & 1 deletion lib/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@

/* evil static data */
static int current_log_level = UNSHIELD_LOG_LEVEL_HIGHEST;
static UnshieldLogCallback* current_log_handler = NULL;
static void* current_log_handler_userdata = NULL;

void unshield_set_log_level(int level)
{
current_log_level = level;
}

void _unshield_log(int level, const char* file, int line, const char* format, ...)
void unshield_set_log_handler(UnshieldLogCallback* handler, void* userdata)
{
current_log_handler = handler;
current_log_handler_userdata = userdata;
}

void unshield_default_log_handler(void* userdata, int level, const char* file, int line, const char* format, ...)
{
(void)userdata;

va_list ap;

if (level > current_log_level)
Expand All @@ -26,3 +36,14 @@ void _unshield_log(int level, const char* file, int line, const char* format, ..
fprintf(stderr, "\n");
}

void _unshield_log(int level, const char* file, int line, const char* format, ...)
{
va_list ap;

if (current_log_handler == NULL)
unshield_set_log_handler(unshield_default_log_handler, NULL);

va_start(ap, format);
current_log_handler(current_log_handler_userdata, level, file, line, format, ap);
va_end(ap);
}

0 comments on commit 54e2ae3

Please sign in to comment.