diff options
author | Alfred E. Heggestad <aeh@db.org> | 2014-02-09 11:50:07 +0100 |
---|---|---|
committer | Alfred E. Heggestad <aeh@db.org> | 2014-02-09 11:50:07 +0100 |
commit | 98bf08bdcf2edd9d397f32650a8bfe62186fbecf (patch) | |
tree | ebc6ec71f44bff8c42e4eefced61948623df02fc /src/log.c | |
parent | e6ad5cf4401b860ba402d4b7b3c7c254bc87a019 (diff) |
baresip 0.4.10
Diffstat (limited to 'src/log.c')
-rw-r--r-- | src/log.c | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..1ccdc32 --- /dev/null +++ b/src/log.c @@ -0,0 +1,139 @@ +/** + * @file log.c Logging + * + * Copyright (C) 2010 Creytiv.com + */ + +#include <re.h> +#include <baresip.h> + + +static struct { + struct list logl; + bool debug; + bool stder; +} lg = { + .logl = LIST_INIT, + .debug = false, + .stder = true +}; + + +void log_register_handler(struct log *log) +{ + if (!log) + return; + + list_append(&lg.logl, &log->le, log); +} + + +void log_unregister_handler(struct log *log) +{ + if (!log) + return; + + list_unlink(&log->le); +} + + +void log_enable_debug(bool enable) +{ + lg.debug = enable; +} + + +void log_enable_stderr(bool enable) +{ + lg.stder = enable; +} + + +void vlog(enum log_level level, const char *fmt, va_list ap) +{ + char buf[4096]; + struct le *le; + + if (re_vsnprintf(buf, sizeof(buf), fmt, ap) < 0) + return; + + if (lg.stder) { + + bool color = level == WARN || level == ERROR; + + if (color) + (void)re_fprintf(stderr, "\x1b[31m"); /* Red */ + + (void)re_fprintf(stderr, "%s", buf); + + if (color) + (void)re_fprintf(stderr, "\x1b[;m"); + } + + le = lg.logl.head; + + while (le) { + + struct log *log = le->data; + le = le->next; + + if (log->h) + log->h(level, buf); + } +} + + +void loglv(enum log_level level, const char *fmt, ...) +{ + va_list ap; + + if ((DEBUG == level) && !lg.debug) + return; + + va_start(ap, fmt); + vlog(level, fmt, ap); + va_end(ap); +} + + +void debug(const char *fmt, ...) +{ + va_list ap; + + if (!lg.debug) + return; + + va_start(ap, fmt); + vlog(DEBUG, fmt, ap); + va_end(ap); +} + + +void info(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vlog(INFO, fmt, ap); + va_end(ap); +} + + +void warning(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vlog(WARN, fmt, ap); + va_end(ap); +} + + +void error(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vlog(ERROR, fmt, ap); + va_end(ap); +} |