From 40988b901c46952f6327b6fba944550236c5d90c Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Mon, 21 Apr 2014 14:05:56 +0200 Subject: [PATCH] Unify print_desc() & print_result() in print() and refactor ... Signed-off-by: Joachim Nilsson --- conf.c | 9 ++------- finit.c | 8 ++------ helpers.c | 57 +++++++++++++++++++++++++++++++++++++++---------------- helpers.h | 1 + 4 files changed, 46 insertions(+), 29 deletions(-) diff --git a/conf.c b/conf.c index 56544a4b..d150ce1a 100644 --- a/conf.c +++ b/conf.c @@ -98,7 +98,7 @@ static int parse_conf(char *file) if (!fp) return 1; - _d("Parse %s ...", file); + print(2, "Loading %s", file); while (!feof(fp)) { if (!fgets(line, sizeof(line), fp)) continue; @@ -165,9 +165,7 @@ static int parse_conf(char *file) } /* Parse any include file, use rcsd if absolute path not given */ if (MATCH_CMD(line, "include ", x)) { - int result; char *file = strip_line(x); - char buf[60]; strlcpy(cmd, file, sizeof(cmd)); if (!fexist(cmd)) { @@ -177,10 +175,7 @@ static int parse_conf(char *file) continue; } - snprintf(buf, sizeof(buf), "Loading include file %s", cmd); - result = parse_conf(cmd); - print_desc("", buf); - print_result(result); + parse_conf(cmd); continue; } if (MATCH_CMD(line, "startx ", x)) { diff --git a/finit.c b/finit.c index c379fb77..cc4b6f64 100644 --- a/finit.c +++ b/finit.c @@ -160,8 +160,6 @@ static void banner(void) int main(int argc, char* argv[]) { - int result; - if (getpid() != 1) return client(argc, argv); @@ -202,16 +200,14 @@ int main(int argc, char* argv[]) /* * Parse configuration file */ - result = parse_finit_conf(FINIT_CONF); - print_desc("Loading " FINIT_CONF, NULL); - print_result(result); + parse_finit_conf(FINIT_CONF); /* * Load plugins. Must run after finit.conf has registered * all services, or service plugins won't have anything to * hook on to. */ - print_desc("Loading Finit plugins", NULL); + print_desc("Loading plugins", NULL); print_result(plugin_load_all(PLUGIN_PATH)); /* diff --git a/helpers.c b/helpers.c index da141112..f47cb672 100644 --- a/helpers.c +++ b/helpers.c @@ -330,30 +330,55 @@ static int print_timestamp(void) return 0; } -void print_desc(char *action, char *desc) +void print(int action, const char *fmt, ...) { + char buf[80]; + size_t len; + va_list ap; + const char failure[] = " \e[7m[FAIL]\e[0m\n"; + const char success[] = " \e[1m[ OK ]\e[0m\n"; + const char pending[] = " \e[1m[ \\/ ]\e[0m\n"; const char dots[] = " ....................................................................."; - delline(); - print_timestamp(); + if (fmt) { + va_start(ap, fmt); + len = vsnprintf(buf, sizeof(buf), fmt, ap); + va_end(ap); - write(STDERR_FILENO, "\r", 1); - if (!action) - action = ""; - write(STDERR_FILENO, action, strlen(action)); - if (!desc) - desc = ""; - write(STDERR_FILENO, desc, strlen(desc)); - write(STDERR_FILENO, dots, 60 - strlen(desc) - strlen(action)); /* pad with dots. */ + delline(); + print_timestamp(); + + write(STDERR_FILENO, "\r", 1); + write(STDERR_FILENO, buf, len); + write(STDERR_FILENO, dots, 60 - len); /* pad with dots. */ + } + + switch (action) { + case -1: + break; + + case 0: + write(STDERR_FILENO, success, 15); + break; + + case 1: + write(STDERR_FILENO, failure, 15); + break; + + default: + write(STDERR_FILENO, pending, 15); + break; + } +} + +void print_desc(char *action, char *desc) +{ + print(-1, "%s %s", action ?: "", desc ?: ""); } int print_result(int fail) { - if (fail) - fprintf(stderr, " \e[7m[FAIL]\e[0m\n"); - else - fprintf(stderr, " \e[1m[ OK ]\e[0m\n"); - + print(!!fail, NULL); return fail; } diff --git a/helpers.h b/helpers.h index 5cb98e7f..9dc45605 100644 --- a/helpers.h +++ b/helpers.h @@ -93,6 +93,7 @@ char *pid_get_name (pid_t pid, char *name, size_t len); void procname_set (char *name, char *args[]); int procname_kill (char *name, int signo); +void print (int action, const char *fmt, ...); void print_desc (char *action, char *desc); int print_result (int fail); int start_process (char *cmd, char *args[], int console);