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 a97300a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
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, va_list ap);
void unshield_set_log_level(int level);
void unshield_set_log_handler(UnshieldLogCallback* handler, void* userdata);

/*
Open/close functions
Expand Down
25 changes: 21 additions & 4 deletions lib/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,42 @@

/* 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)
{
va_list ap;
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, va_list ap)
{
(void)userdata;

if (level > current_log_level)
return;

fprintf(stderr, "[%s:%i] ", file, line);

va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);

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 a97300a

Please sign in to comment.