controller-legacy/logger.c
2020-04-24 15:08:26 +02:00

59 lines
1.5 KiB
C

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <config.h>
#include <logger.h>
#define COLOR_TRACE COLOR_GREEN
#define COLOR_DEBUG COLOR_BLUE
#define COLOR_INFO COLOR_CYAN
#define COLOR_WARN COLOR_YELLOW
#define COLOR_ERROR COLOR_RED
#define COLOR_FATAL COLOR_MAGENTA
void
logger_log(FILE *stream, log_level_t level, const char *filename, int line, const char *func, const char *msg, ...)
{
if(global_config.log_level < level)
{
return;
}
switch(level)
{
case LOG_LEVEL_TRACE:
fprintf(stream, COLOR_TRACE "[TRACE] ");
break;
case LOG_LEVEL_DEBUG:
fprintf(stream, COLOR_DEBUG "[DEBUG] ");
break;
case LOG_LEVEL_INFO:
fprintf(stream, COLOR_INFO "[INFO ] ");
break;
case LOG_LEVEL_WARN:
fprintf(stream, COLOR_WARN "[WARN ] ");
break;
case LOG_LEVEL_ERROR:
fprintf(stream, COLOR_ERROR "[ERROR] ");
break;
case LOG_LEVEL_FATAL:
fprintf(stream, COLOR_FATAL "[FATAL] ");
break;
default:
fprintf(stream, COLOR_NONE "[LOG ] ");
break;
}
char timestamp_str[32];
time_t rawtime;
time(&rawtime);
strftime(timestamp_str, 32, "%Y-%m-%d %H:%M:%S", localtime(&rawtime));
fprintf(stream, "%s %s:%d:%s " COLOR_NONE, timestamp_str, filename, line, func);
va_list args;
va_start(args, msg);
vfprintf(stream, msg, args);
va_end(args);
}