repmgrd: add command line option -L/--log-level

Mainly for ad-hoc debugging purposes without needing to change
the configuration file.
This commit is contained in:
Ian Barwick
2017-06-22 00:09:18 +09:00
parent 8d84732026
commit d77736329a
5 changed files with 83 additions and 41 deletions

View File

@@ -22,7 +22,7 @@ static void tablespace_list_append(t_configuration_options *options, const char
static char *trim(char *s); static char *trim(char *s);
static void exit_with_errors(ItemList *config_errors, ItemList *config_warnings, bool terse); static void exit_with_config_file_errors(ItemList *config_errors, ItemList *config_warnings, bool terse);
void void
@@ -172,7 +172,7 @@ parse_config(t_configuration_options *options, bool terse)
/* errors found - exit after printing details, and any warnings */ /* errors found - exit after printing details, and any warnings */
if (config_errors.head != NULL) if (config_errors.head != NULL)
{ {
exit_with_errors(&config_errors, &config_warnings, terse); exit_with_config_file_errors(&config_errors, &config_warnings, terse);
} }
if (terse == false && config_warnings.head != NULL) if (terse == false && config_warnings.head != NULL)
@@ -649,7 +649,7 @@ reload_config(t_configuration_options *orig_options)
/* TODO: don't emit warnings if --terse and no errors */ /* TODO: don't emit warnings if --terse and no errors */
static void static void
exit_with_errors(ItemList *config_errors, ItemList *config_warnings, bool terse) exit_with_config_file_errors(ItemList *config_errors, ItemList *config_warnings, bool terse)
{ {
ItemListCell *cell; ItemListCell *cell;
@@ -675,6 +675,30 @@ exit_with_errors(ItemList *config_errors, ItemList *config_warnings, bool terse)
} }
void
exit_with_cli_errors(ItemList *error_list)
{
fprintf(stderr, _("The following command line errors were encountered:\n"));
print_item_list(error_list);
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname());
exit(ERR_BAD_CONFIG);
}
void
print_item_list(ItemList *item_list)
{
ItemListCell *cell;
for (cell = item_list->head; cell; cell = cell->next)
{
fprintf(stderr, " %s\n", cell->string);
}
}
/* /*

View File

@@ -167,4 +167,7 @@ bool parse_pg_basebackup_options(const char *pg_basebackup_options,
int server_version_num, int server_version_num,
ItemList *error_list); ItemList *error_list);
/* called by repmgr-client and repmgrd */
void exit_with_cli_errors(ItemList *error_list);
void print_item_list(ItemList *item_list);
#endif #endif

View File

@@ -58,8 +58,8 @@ t_node_info target_node_info = T_NODE_INFO_INITIALIZER;
/* Collate command line errors and warnings here for friendlier reporting */ /* Collate command line errors and warnings here for friendlier reporting */
ItemList cli_errors = { NULL, NULL }; static ItemList cli_errors = { NULL, NULL };
ItemList cli_warnings = { NULL, NULL }; static ItemList cli_warnings = { NULL, NULL };
int int
main(int argc, char **argv) main(int argc, char **argv)
@@ -410,7 +410,7 @@ main(int argc, char **argv)
{ {
PQExpBufferData invalid_log_level; PQExpBufferData invalid_log_level;
initPQExpBuffer(&invalid_log_level); initPQExpBuffer(&invalid_log_level);
appendPQExpBuffer(&invalid_log_level, _("Invalid log level \"%s\" provided"), optarg); appendPQExpBuffer(&invalid_log_level, _("invalid log level \"%s\" provided"), optarg);
item_list_append(&cli_errors, invalid_log_level.data); item_list_append(&cli_errors, invalid_log_level.data);
termPQExpBuffer(&invalid_log_level); termPQExpBuffer(&invalid_log_level);
} }
@@ -534,7 +534,7 @@ main(int argc, char **argv)
/* Exit here already if errors in command line options found */ /* Exit here already if errors in command line options found */
if (cli_errors.head != NULL) if (cli_errors.head != NULL)
{ {
exit_with_errors(); exit_with_cli_errors(&cli_errors);
} }
/* /*
@@ -674,7 +674,7 @@ main(int argc, char **argv)
*/ */
if (cli_errors.head != NULL) if (cli_errors.head != NULL)
{ {
exit_with_errors(); exit_with_cli_errors(&cli_errors);
} }
/* /*
@@ -1148,30 +1148,6 @@ action_name(const int action)
return "UNKNOWN ACTION"; return "UNKNOWN ACTION";
} }
static void
exit_with_errors(void)
{
fprintf(stderr, _("The following command line errors were encountered:\n"));
print_item_list(&cli_errors);
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname());
exit(ERR_BAD_CONFIG);
}
static void
print_item_list(ItemList *item_list)
{
ItemListCell *cell;
for (cell = item_list->head; cell; cell = cell->next)
{
fprintf(stderr, " %s\n", cell->string);
}
}
void void
print_error_list(ItemList *error_list, int log_level) print_error_list(ItemList *error_list, int log_level)

View File

@@ -144,8 +144,7 @@ static void do_help(void);
static const char *action_name(const int action); static const char *action_name(const int action);
static void exit_with_errors(void);
static void print_item_list(ItemList *item_list);
static void check_cli_parameters(const int action); static void check_cli_parameters(const int action);
static void write_primary_conninfo(char *line, t_conninfo_param_list *param_list); static void write_primary_conninfo(char *line, t_conninfo_param_list *param_list);

View File

@@ -26,6 +26,9 @@ static PGconn *local_conn = NULL;
static PGconn *master_conn = NULL; static PGconn *master_conn = NULL;
/* Collate command line errors here for friendlier reporting */
static ItemList cli_errors = { NULL, NULL };
/* /*
* Record receipt SIGHUP; will cause configuration file to be reread at the * Record receipt SIGHUP; will cause configuration file to be reread at the
* appropriate point in the main loop. * appropriate point in the main loop.
@@ -51,6 +54,7 @@ main(int argc, char **argv)
{ {
int optindex; int optindex;
int c; int c;
char log_level[MAXLEN] = "";
bool monitoring_history = false; bool monitoring_history = false;
static struct option long_options[] = static struct option long_options[] =
@@ -67,6 +71,7 @@ main(int argc, char **argv)
{"pid-file", required_argument, NULL, 'p'}, {"pid-file", required_argument, NULL, 'p'},
/* logging options */ /* logging options */
{"log-level", required_argument, NULL, 'L'},
{"verbose", no_argument, NULL, 'v'}, {"verbose", no_argument, NULL, 'v'},
/* legacy options */ /* legacy options */
@@ -89,7 +94,7 @@ main(int argc, char **argv)
exit(1); exit(1);
} }
while ((c = getopt_long(argc, argv, "?Vf:vdp:m", long_options, &optindex)) != -1) while ((c = getopt_long(argc, argv, "?Vf:L:vdp:m", long_options, &optindex)) != -1)
{ {
switch (c) switch (c)
{ {
@@ -137,6 +142,24 @@ main(int argc, char **argv)
/* logging options */ /* logging options */
/* -L/--log-level */
case 'L':
{
int detected_log_level = detect_log_level(optarg);
if (detected_log_level != -1)
{
strncpy(log_level, optarg, MAXLEN);
}
else
{
PQExpBufferData invalid_log_level;
initPQExpBuffer(&invalid_log_level);
appendPQExpBuffer(&invalid_log_level, _("invalid log level \"%s\" provided"), optarg);
item_list_append(&cli_errors, invalid_log_level.data);
termPQExpBuffer(&invalid_log_level);
}
break;
}
case 'v': case 'v':
verbose = true; verbose = true;
break; break;
@@ -154,6 +177,19 @@ main(int argc, char **argv)
} }
} }
/* Exit here already if errors in command line options found */
if (cli_errors.head != NULL)
{
exit_with_cli_errors(&cli_errors);
}
/* Some configuration file items can be overriden by command line options */
/* Command-line parameter -L/--log-level overrides any setting in config file*/
if (*log_level != '\0')
{
strncpy(config_file_options.loglevel, log_level, MAXLEN);
}
/* /*
* Tell the logger we're a daemon - this will ensure any output logged * Tell the logger we're a daemon - this will ensure any output logged
* before the logger is initialized will be formatted correctly * before the logger is initialized will be formatted correctly
@@ -207,7 +243,7 @@ static void
daemonize_process(void) daemonize_process(void)
{ {
char *ptr, char *ptr,
path[MAXLEN]; path[MAXPGPATH];
pid_t pid = fork(); pid_t pid = fork();
int ret; int ret;
@@ -218,7 +254,8 @@ daemonize_process(void)
exit(ERR_SYS_FAILURE); exit(ERR_SYS_FAILURE);
break; break;
case 0: /* child process */ case 0:
/* create independent session ID */
pid = setsid(); pid = setsid();
if (pid == (pid_t) -1) if (pid == (pid_t) -1)
{ {
@@ -229,20 +266,22 @@ daemonize_process(void)
/* ensure that we are no longer able to open a terminal */ /* ensure that we are no longer able to open a terminal */
pid = fork(); pid = fork();
if (pid == -1) /* error case */ /* error case */
if (pid == -1)
{ {
log_error(_("error in fork():\n %s"), strerror(errno)); log_error(_("error in fork():\n %s"), strerror(errno));
exit(ERR_SYS_FAILURE); exit(ERR_SYS_FAILURE);
} }
if (pid != 0) /* parent process */ /* parent process */
if (pid != 0)
{ {
exit(0); exit(0);
} }
/* a child just flows along */ /* child process */
memset(path, 0, MAXLEN); memset(path, 0, MAXPGPATH);
for (ptr = config_file + strlen(config_file); ptr > config_file; --ptr) for (ptr = config_file + strlen(config_file); ptr > config_file; --ptr)
{ {
@@ -257,6 +296,7 @@ daemonize_process(void)
*path = '/'; *path = '/';
} }
log_info("dir now %s", path);
ret = chdir(path); ret = chdir(path);
if (ret != 0) if (ret != 0)
{ {