core-legacy/src/logger.c

85 lines
2.2 KiB
C
Raw Normal View History

2020-05-05 09:42:02 +00:00
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <config.h>
#include <logger.h>
2020-07-26 19:00:05 +00:00
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;
2020-05-05 09:42:02 +00:00
void
2020-07-26 19:00:05 +00:00
logger_log(int level, const char *filename, int line, const char *func, const char *msg, ...)
2020-05-05 09:42:02 +00:00
{
if(global_config.log_level < level)
{
return;
}
2020-07-26 19:00:05 +00:00
const char *level_str;
const char *color;
2020-05-05 09:42:02 +00:00
switch(level)
{
2020-07-26 19:00:05 +00:00
case LOG_DEBUG:
color = COLOR_DEBUG;
level_str = "DEBUG";
2020-05-05 09:42:02 +00:00
break;
2020-07-26 19:00:05 +00:00
case LOG_INFO:
color = COLOR_INFO;
level_str = "INFO";
2020-05-05 09:42:02 +00:00
break;
2020-07-26 19:00:05 +00:00
case LOG_NOTICE:
color = COLOR_NOTICE;
level_str = "NOTE";
2020-05-05 09:42:02 +00:00
break;
2020-07-26 19:00:05 +00:00
case LOG_WARNING:
color = COLOR_WARNING;
level_str = "WARN";
2020-05-05 09:42:02 +00:00
break;
2020-07-26 19:00:05 +00:00
case LOG_ERR:
color = COLOR_ERR;
level_str = "ERROR";
2020-05-05 09:42:02 +00:00
break;
2020-07-26 19:00:05 +00:00
case LOG_CRIT:
color = COLOR_CRIT;
level_str = "CRIT";
2020-05-05 09:42:02 +00:00
break;
2020-07-26 19:00:05 +00:00
case LOG_EMERG:
color = COLOR_EMERG;
level_str = "EMERG";
2020-05-05 09:42:02 +00:00
break;
2020-07-26 19:00:05 +00:00
default:
return;
2020-05-05 09:42:02 +00:00
}
char timestamp_str[32];
time_t rawtime;
time(&rawtime);
strftime(timestamp_str, 32, "%Y-%m-%d %H:%M:%S", localtime(&rawtime));
2020-07-26 19:00:05 +00:00
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);
2020-05-05 09:42:02 +00:00
va_list args;
va_start(args, msg);
2020-07-26 19:00:05 +00:00
vsyslog(level, buffer, args);
2020-05-05 09:42:02 +00:00
va_end(args);
2020-07-26 19:00:05 +00:00
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);
2020-05-05 09:42:02 +00:00
}