From acc0ffa81f20cdec30d6bbe4b8930c74a4949b2c Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Fri, 13 Nov 2015 20:54:58 +0900 Subject: [PATCH] Add -L/--log-level command line option to repmgr Overrides any setting in the config file. This will replace the -v/--verbose option. --- log.c | 4 +--- log.h | 5 ++++- repmgr.c | 26 +++++++++++++++++++++++++- repmgr.h | 3 ++- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/log.c b/log.c index abecb9e0..1e89272d 100644 --- a/log.c +++ b/log.c @@ -39,7 +39,6 @@ /* #define REPMGR_DEBUG */ -static int detect_log_level(const char *level); static int detect_log_facility(const char *facility); int log_type = REPMGR_STDERR; @@ -217,7 +216,7 @@ logger_min_verbose(int minimum) log_level = minimum; } -static int +int detect_log_level(const char *level) { if (!strcmp(level, "DEBUG")) @@ -247,7 +246,6 @@ detect_log_facility(const char *facility) if (!strncmp(facility, "LOCAL", 5) && strlen(facility) == 6) { - local = atoi(&facility[5]); switch (local) diff --git a/log.h b/log.h index 4dcee307..6a56b240 100644 --- a/log.h +++ b/log.h @@ -112,12 +112,15 @@ __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4))); #endif +int detect_log_level(const char *level); + /* Logger initialisation and shutdown */ -bool logger_shutdown(void); bool logger_init(t_configuration_options * opts, const char *ident, const char *level, const char *facility); +bool logger_shutdown(void); + void logger_min_verbose(int minimum); void log_hint(const char *fmt, ...); diff --git a/repmgr.c b/repmgr.c index 8d382f9d..6730277e 100644 --- a/repmgr.c +++ b/repmgr.c @@ -151,6 +151,7 @@ main(int argc, char **argv) {"pg_bindir", required_argument, NULL, 'b'}, {"rsync-only", no_argument, NULL, 'r'}, {"fast-checkpoint", no_argument, NULL, 'c'}, + {"log-level", required_argument, NULL, 'L'}, {"initdb-no-pwprompt", no_argument, NULL, 1}, {"check-upstream-config", no_argument, NULL, 2}, {"recovery-min-apply-delay", required_argument, NULL, 3}, @@ -174,7 +175,7 @@ main(int argc, char **argv) /* Prevent getopt_long() from printing an error message */ opterr = 0; - while ((c = getopt_long(argc, argv, "?Vd:h:p:U:S:D:l:f:R:w:k:FWIvb:r:c", long_options, + while ((c = getopt_long(argc, argv, "?Vd:h:p:U:S:D:l:f:R:w:k:FWIvb:r:c:L:", long_options, &optindex)) != -1) { /* @@ -256,6 +257,22 @@ main(int argc, char **argv) case 'c': runtime_options.fast_checkpoint = true; break; + case 'L': + { + int detected_log_level = detect_log_level(optarg); + if (detected_log_level != -1) + { + strncpy(runtime_options.loglevel, optarg, MAXLEN); + } + else + { + PQExpBufferData invalid_log_level; + initPQExpBuffer(&invalid_log_level); + appendPQExpBuffer(&invalid_log_level, _("Invalid log level \"%s\" provided"), optarg); + error_list_append(&cli_errors, invalid_log_level.data); + } + break; + } case 1: runtime_options.initdb_no_pwprompt = true; break; @@ -496,6 +513,12 @@ main(int argc, char **argv) * logging to troubleshoot problems. */ + /* Command-line parameter -L/--log-level overrides any setting in config file*/ + if(*runtime_options.loglevel != '\0') + { + strncpy(options.loglevel, runtime_options.loglevel, MAXLEN); + } + logger_init(&options, progname(), options.loglevel, options.logfacility); if (runtime_options.verbose) logger_min_verbose(LOG_INFO); @@ -2524,6 +2547,7 @@ help(void) printf(_(" -R, --remote-user=USERNAME database server username for rsync\n")); printf(_(" -F, --force force potentially dangerous operations to happen\n")); printf(_(" --check-upstream-config verify upstream server configuration\n")); + printf(_(" -L, --log-level set log level (overrides configuration file)\n")); printf(_("\n")); printf(_("Command-specific configuration options:\n")); printf(_(" -c, --fast-checkpoint (standby clone) force fast checkpoint\n")); diff --git a/repmgr.h b/repmgr.h index 0ed86b42..c2895bf6 100644 --- a/repmgr.h +++ b/repmgr.h @@ -82,6 +82,7 @@ typedef struct bool ignore_external_config_files; char masterport[MAXLEN]; char localport[MAXLEN]; + char loglevel[MAXLEN]; /* parameter used by CLUSTER CLEANUP */ int keep_history; @@ -91,7 +92,7 @@ typedef struct char recovery_min_apply_delay[MAXLEN]; } t_runtime_options; -#define T_RUNTIME_OPTIONS_INITIALIZER { "", "", "", "", "", "", "", DEFAULT_WAL_KEEP_SEGMENTS, false, false, false, false, false, false, false, false, "", "", 0, "", "" } +#define T_RUNTIME_OPTIONS_INITIALIZER { "", "", "", "", "", "", "", DEFAULT_WAL_KEEP_SEGMENTS, false, false, false, false, false, false, false, false, "", "", "", 0, "", "" } extern char repmgr_schema[MAXLEN];