diff options
author | Simon Ser <contact@emersion.fr> | 2020-08-27 15:56:14 +0000 |
---|---|---|
committer | Kenny Levinsen <kl@kl.wtf> | 2020-08-28 01:21:57 +0200 |
commit | 47d4b43f1a63ca83b7f2dd8bc7eb24f194ebb32f (patch) | |
tree | 31477317808700f412bf7b0b37cbea4b7b2552ba /common/log.c | |
parent | a254fe3692e63f92aa710310ba8113373180448c (diff) |
Introduce libseat_set_log_handler
This allows libseat users to register a custom logging function.
Diffstat (limited to 'common/log.c')
-rw-r--r-- | common/log.c | 51 |
1 files changed, 33 insertions, 18 deletions
diff --git a/common/log.c b/common/log.c index 7e5e8d4..646480d 100644 --- a/common/log.c +++ b/common/log.c @@ -10,7 +10,10 @@ const long NSEC_PER_SEC = 1000000000; +static void log_stderr(enum libseat_log_level level, const char *fmt, va_list args); + static enum libseat_log_level current_log_level; +static libseat_log_func current_log_handler = log_stderr; static struct timespec start_time = {-1, -1}; static bool colored = false; @@ -37,22 +40,7 @@ static void timespec_sub(struct timespec *r, const struct timespec *a, const str } } -void log_init(enum libseat_log_level level) { - if (start_time.tv_sec >= 0) { - return; - } - clock_gettime(CLOCK_MONOTONIC, &start_time); - current_log_level = level; - colored = isatty(STDERR_FILENO); -} - -void _logf(enum libseat_log_level level, const char *fmt, ...) { - int stored_errno = errno; - va_list args; - if (level > current_log_level) { - return; - } - +static void log_stderr(enum libseat_log_level level, const char *fmt, va_list args) { struct timespec ts = {0}; clock_gettime(CLOCK_MONOTONIC, &ts); timespec_sub(&ts, &ts, &start_time); @@ -71,10 +59,37 @@ void _logf(enum libseat_log_level level, const char *fmt, ...) { fprintf(stderr, "%02d:%02d:%02d.%03ld %s ", (int)(ts.tv_sec / 60 / 60), (int)(ts.tv_sec / 60 % 60), (int)(ts.tv_sec % 60), ts.tv_nsec / 1000000, prefix); - va_start(args, fmt); vfprintf(stderr, fmt, args); - va_end(args); fprintf(stderr, "%s", postfix); +} + +void log_init(enum libseat_log_level level) { + if (start_time.tv_sec >= 0) { + return; + } + clock_gettime(CLOCK_MONOTONIC, &start_time); + current_log_level = level; + colored = isatty(STDERR_FILENO); +} + +void _logf(enum libseat_log_level level, const char *fmt, ...) { + int stored_errno = errno; + va_list args; + if (level > current_log_level) { + return; + } + + va_start(args, fmt); + current_log_handler(level, fmt, args); + va_end(args); + errno = stored_errno; } + +void libseat_set_log_handler(libseat_log_func handler) { + if (handler == NULL) { + handler = log_stderr; + } + current_log_handler = handler; +} |