diff options
| author | Joel Stålnacke <joel@saker.fi> | 2024-10-13 14:32:14 +0300 |
|---|---|---|
| committer | Joel Stålnacke <joel@saker.fi> | 2024-10-13 14:32:14 +0300 |
| commit | 11b2cfa087f5de09b97eb42fb219f563886f6d40 (patch) | |
| tree | 215a8e65edc0b74a47da63a0c8230b8eadba060f /lib/log.c | |
| parent | 53a1cdf5bee2955995dfbf441f5354d1dcfc1e0c (diff) | |
Add libdjup logging
Diffstat (limited to 'lib/log.c')
| -rw-r--r-- | lib/log.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/lib/log.c b/lib/log.c new file mode 100644 index 0000000..51ab107 --- /dev/null +++ b/lib/log.c @@ -0,0 +1,56 @@ +#include <stdarg.h> +#include <stdlib.h> +#include <stdio.h> +#include <time.h> + +#include "log.h" + +/* Message buffer */ +#define MBUF_SIZE 512 + +static void (*log_output)(int level, const char *source, int line, const char *msg) = NULL; +static int min_level = LOGL_INFO; + +void +dp_log_set_output(void (*output)(int, const char *, int, const char *)) +{ + log_output = output; +} + +void +dp_log_set_level(int level) +{ + min_level = level; +} + +void +dp_log_write(int level, const char *source, int line, const char *fmt, ...) +{ + char mbuf[MBUF_SIZE]; + char *msg; + va_list va; + int len; + + if (level < min_level) + return; + if (!log_output) + return; + + msg = mbuf; + va_start(va, fmt); + len = vsnprintf(msg, MBUF_SIZE, fmt, va); + va_end(va); + + if (len >= MBUF_SIZE) + { + msg = (char *)malloc(len + 1); + va_start(va, fmt); + len = vsnprintf(msg, len + 1, fmt, va); + va_end(va); + } + + log_output(level, source, line, msg); + + if (msg != mbuf) + free(msg); +} |
