Add timestamps to log line in stderr

Patch by Christian Kruse
This commit is contained in:
Jaime Casanova
2013-12-19 00:06:01 -05:00
parent 8b370dc581
commit 493133986d
2 changed files with 36 additions and 9 deletions

27
log.c
View File

@@ -25,9 +25,11 @@
#ifdef HAVE_SYSLOG
#include <syslog.h>
#include <stdarg.h>
#endif
#include <stdarg.h>
#include <time.h>
#include "log.h"
#define DEFAULT_IDENT "repmgr"
@@ -37,6 +39,29 @@
/* #define REPMGR_DEBUG */
void stderr_log_with_level(const char *level_name, int level, const char *fmt, ...) {
size_t len = strlen(fmt);
char fmt1[len + 150];
time_t t;
struct tm *tm;
char buff[100];
va_list ap;
if(log_level >= level) {
time(&t);
tm = localtime(&t);
va_start(ap, fmt);
strftime(buff, 100, "[%Y-%m-%d %H:%M:%S]", tm);
snprintf(fmt1, len + 150, "%s [%s] %s", buff, level_name, fmt);
vfprintf(stderr, fmt1, ap);
va_end(ap);
}
}
static int detect_log_level(const char* level);
static int detect_log_facility(const char* facility);