#include #include #include #include #include #include "log.h" #include "confparser.h" 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 ((fderr = fopen(conf.rmps.errlog, "a")) == NULL) fderr = stderr; else setvbuf(fderr, NULL, _IOLBF, 0); if ((fdout = fopen(conf.rmps.logfile, "a")) == NULL) fdout = stdout; else setvbuf(fdout, NULL, _IOLBF, 0); } void log_ssl(void) { ERR_print_errors_fp(fderr); fflush(fderr); } static void set_fpts(void) { fderr = stderr; fdout = stdout; } void log(LOG_LEVEL lvl, char *fmt, ... ) { char fmt_with_pfx[1024]; pthread_once(&init_once, set_fpts); if (conf.isvalid) pthread_once(&once, open_logs); if (lvl <= conf.rmps.loglevel) { va_list list; FILE *fp; static const char *prefixes[] = { "ERROR", "WARNING", "INFO", "VERBOSE" }; time_t t = time(NULL); struct tm tm; localtime_r(&t, &tm); if (lvl == ERROR || lvl == WARNING) fp = fderr; else fp = fdout; 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(fp, fmt_with_pfx, list); va_end(list); } }