From cf47da359caca907d8f0fd8fe9f5f4ddb398b67d Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Tue, 21 Jun 2016 09:19:57 +0200 Subject: [PATCH 1/1] daemon: Make daemon_init_colors_or_die() independent of gengetopt. The function receives the values given to the --log-color option as a char * array, which is the type that gengetopt provides for the arguments to string options which may be given multiple times. This patch gets get rid of this implementation detail. The function no longer takes the arguments to --log-color at all and applications now must call daemon_set_log_color_or_die() themselves to set user-defined per-loglevel colors. To make this work, we let daemon_init_colors_or_die() return a boolean which indicates whether color mode should be enabled, and daemon_set_log_color_or_die() is made public. The two users of this API, para_server and para_audiod, are adjusted accordingly. --- audiod.c | 7 +++++-- daemon.c | 39 +++++++++++++++++---------------------- daemon.h | 6 +++--- server.c | 8 ++++++-- 4 files changed, 31 insertions(+), 29 deletions(-) diff --git a/audiod.c b/audiod.c index 75073ec2..fedeff7a 100644 --- a/audiod.c +++ b/audiod.c @@ -1506,8 +1506,11 @@ int main(int argc, char *argv[]) daemon_set_priority(conf.priority_arg); daemon_drop_privileges_or_die(conf.user_arg, conf.group_arg); parse_config_or_die(); - daemon_init_colors_or_die(conf.color_arg, color_arg_auto, color_arg_no, - conf.logfile_given, conf.log_color_arg, conf.log_color_given); + if (daemon_init_colors_or_die(conf.color_arg, color_arg_auto, color_arg_no, + conf.logfile_given)) { + for (i = 0; i < conf.log_color_given; i++) + daemon_set_log_color_or_die(conf.log_color_arg[i]); + } init_random_seed_or_die(); daemon_set_flag(DF_LOG_TIME); daemon_set_flag(DF_LOG_HOSTNAME); diff --git a/daemon.c b/daemon.c index 945c5a39..7c625bbe 100644 --- a/daemon.c +++ b/daemon.c @@ -54,12 +54,12 @@ static void daemon_set_default_log_colors(void) color_parse_or_die(default_log_colors[i], me->log_colors[i]); } -/* +/** * Set the color for one loglevel. * - * arg must be of the form "ll:[fg [bg]] [attr]". + * \param arg Must be of the form "ll:[fg [bg]] [attr]". */ -static void daemon_set_log_color_or_die(char const *arg) +void daemon_set_log_color_or_die(const char *arg) { char *p = strchr(arg, ':'); int ret, ll; @@ -85,36 +85,31 @@ err: * \param color_arg_auto The value for automatic color detection. * \param color_arg_no The value to disable colored log messages. * \param logfile_given In auto mode colors are disabled if this value is true. - * \param log_color_argv Color specifiers given to --log-color. - * \param argc Number of color specifiers in \a log_color_argv. * - * If \a color_arg equals \a color_arg_no, color mode is disabled, i.e., calls - * to \a para_log() will not produce colored output. If \a color_arg == \a - * color_arg_auto, the function autodetects whether to activate colors. - * Otherwise color mode is enabled. + * If color_arg equals color_arg_no, color mode is disabled. That is, calls to + * para_log() will not produce colored output. If color_arg equals + * color_arg_auto, the function detects automatically whether to activate + * colors. Otherwise color mode is enabled. + * + * If color mode is to be enabled, the default colors are set for each + * loglevel. They can be overwritten by calling daemon_set_log_color_or_die(). * - * If color mode is to be enabled, the strings in \a log_color_argv are parsed - * and the color scheme is updated accordingly. For each loglevel, the default - * colors apply if there is no log_color_argv for this loglevel. + * \return Whether colors have been enabled by the function. */ -void daemon_init_colors_or_die(int color_arg, int color_arg_auto, - int color_arg_no, bool logfile_given, char **log_color_argv, - int argc) +bool daemon_init_colors_or_die(int color_arg, int color_arg_auto, + int color_arg_no, bool logfile_given) { - int i; - if (color_arg == color_arg_no) - return; + return false; if (color_arg == color_arg_auto) { if (logfile_given) - return; + return false; if (!isatty(STDERR_FILENO)) - return; + return false; } daemon_set_flag(DF_COLOR_LOG); daemon_set_default_log_colors(); - for (i = 0; i < argc; i++) - daemon_set_log_color_or_die(log_color_argv[i]); + return true; } /** diff --git a/daemon.h b/daemon.h index e97678d6..c7153605 100644 --- a/daemon.h +++ b/daemon.h @@ -13,9 +13,9 @@ __malloc char *daemon_get_uptime_str(const struct timeval *current_time); void daemon_set_logfile(const char *logfile_name); void daemon_set_flag(unsigned flag); void daemon_set_loglevel(const char *loglevel); -void daemon_init_colors_or_die(int color_arg, int color_arg_auto, - int color_arg_no, bool logfile_given, char **log_color_argv, - int argc); +bool daemon_init_colors_or_die(int color_arg, int color_arg_auto, + int color_arg_no, bool logfile_given); +void daemon_set_log_color_or_die(const char *arg); __printf_2_3 void daemon_log(int ll, const char* fmt,...); /** Daemon log configuration flags. */ diff --git a/server.c b/server.c index 71a9ec50..088cc8b1 100644 --- a/server.c +++ b/server.c @@ -207,8 +207,12 @@ void parse_config_or_die(int override) daemon_open_log_or_die(); } - daemon_init_colors_or_die(conf.color_arg, color_arg_auto, color_arg_no, - conf.logfile_given, conf.log_color_arg, conf.log_color_given); + if (daemon_init_colors_or_die(conf.color_arg, color_arg_auto, color_arg_no, + conf.logfile_given)) { + int i; + for (i = 0; i < conf.log_color_given; i++) + daemon_set_log_color_or_die(conf.log_color_arg[i]); + } daemon_set_flag(DF_LOG_PID); daemon_set_flag(DF_LOG_LL); daemon_set_flag(DF_LOG_TIME); -- 2.39.2