fix: better logging behaviour

This commit is contained in:
Tobias Reisinger 2020-07-26 21:05:41 +02:00
parent 011c5a6102
commit 0530a350df
30 changed files with 343 additions and 176 deletions

View file

@ -1,58 +1,85 @@
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.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
const char *COLOR_DEBUG = COLOR_GREEN;
const char *COLOR_INFO = COLOR_CYAN;
const char *COLOR_NOTICE = COLOR_CYAN;
const char *COLOR_WARNING = COLOR_YELLOW;
const char *COLOR_ERR = COLOR_RED;
const char *COLOR_CRIT = COLOR_MAGENTA;
const char *COLOR_EMERG = COLOR_MAGENTA;
void
logger_log(FILE *stream, log_level_t level, const char *filename, int line, const char *func, const char *msg, ...)
logger_log(int level, const char *filename, int line, const char *func, const char *msg, ...)
{
if(global_config.log_level < level)
{
return;
}
const char *level_str;
const char *color;
switch(level)
{
case LOG_LEVEL_TRACE:
fprintf(stream, COLOR_TRACE "[TRACE] ");
case LOG_DEBUG:
color = COLOR_DEBUG;
level_str = "DEBUG";
break;
case LOG_LEVEL_DEBUG:
fprintf(stream, COLOR_DEBUG "[DEBUG] ");
case LOG_INFO:
color = COLOR_INFO;
level_str = "INFO";
break;
case LOG_LEVEL_INFO:
fprintf(stream, COLOR_INFO "[INFO ] ");
case LOG_NOTICE:
color = COLOR_NOTICE;
level_str = "NOTE";
break;
case LOG_LEVEL_WARN:
fprintf(stream, COLOR_WARN "[WARN ] ");
case LOG_WARNING:
color = COLOR_WARNING;
level_str = "WARN";
break;
case LOG_LEVEL_ERROR:
fprintf(stream, COLOR_ERROR "[ERROR] ");
case LOG_ERR:
color = COLOR_ERR;
level_str = "ERROR";
break;
case LOG_LEVEL_FATAL:
fprintf(stream, COLOR_FATAL "[FATAL] ");
case LOG_CRIT:
color = COLOR_CRIT;
level_str = "CRIT";
break;
case LOG_EMERG:
color = COLOR_EMERG;
level_str = "EMERG";
break;
default:
fprintf(stream, COLOR_NONE "[LOG ] ");
break;
return;
}
char timestamp_str[32];
time_t rawtime = time(NULL);
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);
char *buffer = malloc(sizeof(char) * (128 + strlen(msg)));
sprintf(buffer, "%s[%5s] %s:%d:%s " COLOR_NONE "%s", color, level_str, filename, line, func, msg);
//fprintf(stream, "%s %s:%d:%s " COLOR_NONE, timestamp_str, filename, line, func);
va_list args;
va_start(args, msg);
vfprintf(stream, msg, args);
vsyslog(level, buffer, args);
va_end(args);
char *buffer_timed = malloc(sizeof(char) * (strlen(timestamp_str) + strlen(buffer) + 2));
sprintf(buffer_timed, "%s %s", timestamp_str, buffer);
va_start(args, msg);
vfprintf(global_config.log_file, buffer_timed, args);
fflush(global_config.log_file);
va_end(args);
free(buffer);
free(buffer_timed);
}