X-Git-Url: http://git.squeep.com/?p=lemu;a=blobdiff_plain;f=notify.c;fp=notify.c;h=8662f05f351bdb8870b02c73d05738f4c00a4881;hp=0000000000000000000000000000000000000000;hb=3c54afe11e890fc476cc4730226be1c45f8a04cb;hpb=29235d4c1f0b11bd2efcad262eaae70383228293 diff --git a/notify.c b/notify.c new file mode 100644 index 0000000..8662f05 --- /dev/null +++ b/notify.c @@ -0,0 +1,52 @@ +#define _GNU_SOURCE 1 + +#include +#include +#include +#include +#include + +#include "notify.h" + +/* + * Generic console notifier. + */ + +static void +notify_fn_default_(int level, const char * func_name, const char * fmt, ...) +{ + FILE *f = (level <= 1) ? stderr : stdout; + static const char * const levels[] = { "FATAL", "ERROR", "INFO", "DEBUG" }; + const int levels_num = sizeof levels / sizeof *levels; + va_list ap; + + flockfile(f); + + fputs_unlocked((func_name && *func_name) ? func_name : "[null]", f); + fputs_unlocked(": --", f); + if (level >= 0 + && level <= levels_num) { + fputs_unlocked(levels[level], f); + } else { + fprintf(f, "%d", level); + } + fputs_unlocked("-- ", f); + + va_start(ap, fmt); + vfprintf(f, fmt, ap); + va_end(ap); + + fputc_unlocked('\n', f); + fflush_unlocked(f); + + funlockfile(f); + +} + +void (* notify_fn)(int level, const char *func_name, const char *msg, ...) __attribute__((format(printf, 3, 4))) = notify_fn_default_; + +void +notify_fn_set(void (* fn)(int level, const char *func_name, const char *fmt, ...)) +{ + notify_fn = fn; +}