diff --git a/log.c b/log.c index e56bd4f8..41e18f2c 100644 --- a/log.c +++ b/log.c @@ -25,9 +25,11 @@ #ifdef HAVE_SYSLOG #include -#include #endif +#include +#include + #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); diff --git a/log.h b/log.h index c7c2af3c..643b3f69 100644 --- a/log.h +++ b/log.h @@ -25,15 +25,17 @@ #define REPMGR_SYSLOG 1 #define REPMGR_STDERR 2 +void stderr_log_with_level(const char *level_name, int level, const char *fmt, ...); + /* Standard error logging */ -#define stderr_log_debug(...) if (log_level >= LOG_DEBUG) fprintf(stderr, __VA_ARGS__) -#define stderr_log_info(...) if (log_level >= LOG_INFO) fprintf(stderr, __VA_ARGS__) -#define stderr_log_notice(...) if (log_level >= LOG_NOTICE) fprintf(stderr, __VA_ARGS__) -#define stderr_log_warning(...) if (log_level >= LOG_WARNING) fprintf(stderr, __VA_ARGS__) -#define stderr_log_err(...) if (log_level >= LOG_ERR) fprintf(stderr, __VA_ARGS__) -#define stderr_log_crit(...) if (log_level >= LOG_CRIT) fprintf(stderr, __VA_ARGS__) -#define stderr_log_alert(...) if (log_level >= LOG_ALERT) fprintf(stderr, __VA_ARGS__) -#define stderr_log_emerg(...) if (log_level >= LOG_EMERG) fprintf(stderr, __VA_ARGS__) +#define stderr_log_debug(...) stderr_log_with_level("DEBUG", LOG_DEBUG, __VA_ARGS__) +#define stderr_log_info(...) stderr_log_with_level("INFO", LOG_INFO, __VA_ARGS__) +#define stderr_log_notice(...) stderr_log_with_level("NOTICE", LOG_NOTICE, __VA_ARGS__) +#define stderr_log_warning(...) stderr_log_with_level("WARNING", LOG_WARNING, __VA_ARGS__) +#define stderr_log_err(...) stderr_log_with_level("ERROR", LOG_ERR, __VA_ARGS__) +#define stderr_log_crit(...) stderr_log_with_level("CRITICAL", LOG_CRIT, __VA_ARGS__) +#define stderr_log_alert(...) stderr_log_with_level("ALERT", LOG_ALERT, __VA_ARGS__) +#define stderr_log_emerg(...) stderr_log_with_level("EMERGENCY", LOG_EMERG, __VA_ARGS__) #ifdef HAVE_SYSLOG