log: initial framework for logging things

This commit is contained in:
Daniel Eklöf 2018-12-17 20:25:18 +01:00
parent c1e71eca60
commit d516ffdda0
3 changed files with 62 additions and 0 deletions

45
log.c Normal file
View file

@ -0,0 +1,45 @@
#include "log.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
static void
_log(enum log_class log_class, const char *module, const char *file, int lineno,
const char *fmt, va_list va)
{
bool colorize = true;
const char *class;
int class_clr;
switch (log_class) {
case LOG_CLASS_ERROR: class = "error"; class_clr = 31; break;
case LOG_CLASS_WARNING: class = "warning"; class_clr = 33; break;
case LOG_CLASS_INFO: class = "info"; class_clr = 97; break;
case LOG_CLASS_DEBUG: class = "debug"; class_clr = 36; break;
}
char clr[16];
snprintf(clr, sizeof(clr), "\e[%dm", class_clr);
printf("%s%s%s: ", colorize ? clr : "", class, colorize ? "\e[0m" : "");
#if defined(_DEBUG)
printf("%s:%d: ", file, lineno);
#else
printf("%s: ", module);
#endif
//printf("%%s\n", buf);
vprintf(fmt, va);
printf("\n");
}
void
log_class(enum log_class log_class, const char *module,
const char *file, int lineno, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
_log(log_class, module, file, lineno, fmt, ap);
va_end(ap);
}