Files
rmps/log_trace.c

72 lines
1.4 KiB
C

#include <time.h>
#include <stdio.h>
#include <stdarg.h>
#include <pthread.h>
#include <openssl/err.h>
#include "log_trace.h"
#include "confparser.h"
static FILE *fderr = NULL;
static FILE *fdout = NULL;
static pthread_once_t once = PTHREAD_ONCE_INIT;
static void open_logs(void)
{
if (conf.rmps.errlog)
fderr = fopen(conf.rmps.errlog, "a");
if (conf.rmps.logfile)
fdout = fopen(conf.rmps.logfile, "a");
}
void log_ssl()
{
ERR_print_errors_fp(fderr);
fflush(fderr);
}
void log_trace(LOG_LEVEL lvl, char *fmt, ... )
{
LOG_LEVEL cur_lvl = conf.rmps.loglevel - '0';
char fmt_with_pfx[1024];
if (conf.isvalid)
pthread_once(&once, open_logs);
if (lvl <= cur_lvl) {
va_list list;
FILE *fd;
static const char *prefixes[] = {
"ERROR", "WARNING", "INFO", "VERBOSE"
};
time_t t = time(NULL);
struct tm tm = *localtime(&t);
if (lvl == ERROR || lvl == WARNING)
fd = fderr;
else
fd = fdout;
if (!fd) {
fprintf( stderr,
"Failed to append errlog: %s. Redirecting to stderr:\n",
conf.rmps.errlog );
fd = stderr;
}
snprintf( fmt_with_pfx,
sizeof(fmt_with_pfx),
"[%d-%02d-%02d %02d:%02d:%02d] %s: %s\n",
tm.tm_year + 1900,
tm.tm_mon + 1,
tm.tm_mday,
tm.tm_hour,
tm.tm_min,
tm.tm_sec,
prefixes[lvl-1],
fmt );
va_start(list, fmt);
vfprintf(fd, fmt_with_pfx, list);
fflush(fd);
va_end(list);
} else
return;
}