Cleanups and small fixes

This commit is contained in:
2016-08-09 14:16:30 +03:00
parent 0d5eeed758
commit 3fecfdbbc4
6 changed files with 51 additions and 49 deletions

View File

@@ -6,49 +6,51 @@
#include "log_trace.h"
#include "confparser.h"
static FILE *fderr = NULL;
static FILE *fdout = NULL;
static FILE *fderr;
static FILE *fdout;
static pthread_once_t once = PTHREAD_ONCE_INIT;
static pthread_once_t init_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");
if ((fderr = fopen(conf.rmps.errlog, "a")) == NULL)
fderr = stderr;
if ((fdout = fopen(conf.rmps.logfile, "a")) == NULL)
fdout = stdout;
}
void log_ssl()
void log_ssl(void)
{
ERR_print_errors_fp(fderr);
fflush(fderr);
}
static void set_fpts(void)
{
fderr = stderr;
fdout = stdout;
}
void log_trace(LOG_LEVEL lvl, char *fmt, ... )
{
LOG_LEVEL cur_lvl = conf.rmps.loglevel - '0';
char fmt_with_pfx[1024];
pthread_once(&init_once, set_fpts);
if (conf.isvalid)
pthread_once(&once, open_logs);
if (lvl <= cur_lvl) {
va_list list;
FILE *fd;
FILE *fp;
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;
fp = fderr;
else
fd = fdout;
if (!fd) {
fprintf( stderr,
"Failed to append errlog: %s. Redirecting to stderr:\n",
conf.rmps.errlog );
fd = stderr;
}
fp = fdout;
snprintf( fmt_with_pfx,
sizeof(fmt_with_pfx),
"[%d-%02d-%02d %02d:%02d:%02d] %s: %s\n",
@@ -62,10 +64,9 @@ void log_trace(LOG_LEVEL lvl, char *fmt, ... )
fmt );
va_start(list, fmt);
vfprintf(fd, fmt_with_pfx, list);
fflush(fd);
vfprintf(fp, fmt_with_pfx, list);
fflush(fp);
va_end(list);
} else
return;
}
}