84 lines
2.2 KiB
C
84 lines
2.2 KiB
C
#include <stdio.h>
|
|
#include <stdarg.h>
|
|
#include <string.h>
|
|
|
|
#include <config.h>
|
|
#include <logger.h>
|
|
|
|
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(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_DEBUG:
|
|
color = COLOR_DEBUG;
|
|
level_str = "DEBUG";
|
|
break;
|
|
case LOG_INFO:
|
|
color = COLOR_INFO;
|
|
level_str = "INFO";
|
|
break;
|
|
case LOG_NOTICE:
|
|
color = COLOR_NOTICE;
|
|
level_str = "NOTE";
|
|
break;
|
|
case LOG_WARNING:
|
|
color = COLOR_WARNING;
|
|
level_str = "WARN";
|
|
break;
|
|
case LOG_ERR:
|
|
color = COLOR_ERR;
|
|
level_str = "ERROR";
|
|
break;
|
|
case LOG_CRIT:
|
|
color = COLOR_CRIT;
|
|
level_str = "CRIT";
|
|
break;
|
|
case LOG_EMERG:
|
|
color = COLOR_EMERG;
|
|
level_str = "EMERG";
|
|
break;
|
|
default:
|
|
return;
|
|
}
|
|
|
|
char timestamp_str[32];
|
|
time_t rawtime;
|
|
time(&rawtime);
|
|
strftime(timestamp_str, 32, "%Y-%m-%d %H:%M:%S", localtime(&rawtime));
|
|
|
|
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);
|
|
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);
|
|
}
|