]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
daemon: Make daemon_init_colors_or_die() independent of gengetopt.
authorAndre Noll <maan@tuebingen.mpg.de>
Tue, 21 Jun 2016 07:19:57 +0000 (09:19 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Sun, 7 Aug 2016 12:03:44 +0000 (14:03 +0200)
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
daemon.c
daemon.h
server.c

index 75073ec22bcc2a09c8a548f57db1560b8df0cef8..fedeff7af880a009d938e73087edb7ca6c83f267 100644 (file)
--- 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);
index 945c5a39a094e8314c5667f57f0b6a8099b12b45..7c625bbea9d84c5ac9b37a22253346576cbb7183 100644 (file)
--- 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;
 }
 
 /**
index e97678d660c12cf9a3ae1415c38fb0ae55b5ee5d..c715360583d350275641042b574708fd0cd2ec04 100644 (file)
--- 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. */
index 71a9ec505f6d096a1dc589a9cdf6c1f484e5090d..088cc8b124448123ed6f4cb73f0226f584dffc25 100644 (file)
--- 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);