diff options
author | David Härdeman <david@hardeman.nu> | 2020-07-01 17:28:09 +0200 |
---|---|---|
committer | David Härdeman <david@hardeman.nu> | 2020-07-01 17:28:09 +0200 |
commit | e045a7b80cc0cb96b79456f9c74c3705989a12df (patch) | |
tree | 00532a51671d2835217997dfdb204ba863fb0a1e | |
parent | 829ea0768672dad977ddeb30dd5e2b254bb4a4e9 (diff) |
Turn colors into global variables
-rw-r--r-- | minecctl/misc.c | 23 | ||||
-rw-r--r-- | minecctl/misc.h | 2 | ||||
-rw-r--r-- | minecctl/rcon-commands.c | 5 | ||||
-rw-r--r-- | minecproxy/main.c | 120 | ||||
-rw-r--r-- | shared/ansi-colors.h | 8 | ||||
-rw-r--r-- | shared/utils.c | 19 | ||||
-rw-r--r-- | shared/utils.h | 2 |
7 files changed, 106 insertions, 73 deletions
diff --git a/minecctl/misc.c b/minecctl/misc.c index 3f5ba50..c8e1d05 100644 --- a/minecctl/misc.c +++ b/minecctl/misc.c @@ -9,8 +9,6 @@ #include "misc.h" #include "minecctl.h" -bool use_colors = false; - /* FIXME: Can be shared */ void set_use_colors() { @@ -34,7 +32,7 @@ void set_use_colors() if (streq(e, "dumb")) return; - use_colors = true; + enable_colors(); } char **strv_copy(char *const *strv) @@ -210,20 +208,23 @@ void free_password(char **password) void __debug(_unused_ enum debug_lvl lvl, const char *fmt, ...) { va_list ap; + const char *color = NULL; - if (use_colors) { - if (lvl & DBG_ERROR) - fprintf(stderr, ANSI_RED); - else if (!(lvl & (DBG_INFO | DBG_VERBOSE))) - fprintf(stderr, ANSI_GREY); - } + + if (lvl & DBG_ERROR) + color = ansi_red; + else if (!(lvl & (DBG_INFO | DBG_VERBOSE))) + color = ansi_grey; + + if (color) + fprintf(stderr, "%s", color); va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); - if (use_colors && !(lvl & (DBG_INFO | DBG_VERBOSE))) - fprintf(stderr, ANSI_NORMAL); + if (color) + fprintf(stderr, "%s", ansi_normal); } _noreturn_ void __die(const char *fmt, ...) diff --git a/minecctl/misc.h b/minecctl/misc.h index c8254df..0c97ad4 100644 --- a/minecctl/misc.h +++ b/minecctl/misc.h @@ -1,8 +1,6 @@ #ifndef foomischfoo #define foomischfoo -extern bool use_colors; - void set_use_colors(); char **strv_copy(char *const *strv); diff --git a/minecctl/rcon-commands.c b/minecctl/rcon-commands.c index eff397d..fc5b932 100644 --- a/minecctl/rcon-commands.c +++ b/minecctl/rcon-commands.c @@ -166,10 +166,7 @@ static bool send_cmd(int sfd, const char *cmd) return false; } - if (use_colors) - info("%s%s%s", ANSI_GREY, reply, ANSI_NORMAL); - else - info("%s", reply); + info("%s%s%s", ansi_grey, reply, ansi_normal); return true; } diff --git a/minecproxy/main.c b/minecproxy/main.c index 6a02531..54a0d7b 100644 --- a/minecproxy/main.c +++ b/minecproxy/main.c @@ -38,77 +38,85 @@ static bool daemonize = false; static FILE *log_file = NULL; static const char *log_file_path = NULL; -static void msg(enum debug_lvl lvl, const char *fmt, va_list ap) +static void set_logging_type(bool *use_colors, bool *sd_daemon) { - static bool first = true; - static bool use_colors = false; - static bool sd_daemon = false; - const char *color; - const char *sd_lvl; + int fd; + const char *e; - assert_return(lvl != 0 && !empty_str(fmt) && ap); + /* assume we're not launched by systemd when daemonized */ + if (daemonize) { + *sd_daemon = false; + *use_colors = false; + return; + } - while (first) { - int fd; - const char *e; + if (log_file) { + *sd_daemon = false; + *use_colors = false; + return; + } - first = false; + if (getenv("NO_COLOR")) { + *sd_daemon = false; + *use_colors = false; + return; + } - /* assume we're not launched by systemd when daemonized */ - if (daemonize) { - sd_daemon = false; - use_colors = false; - break; - } + fd = fileno(stderr); + if (fd < 0) { + /* Umm... */ + *sd_daemon = true; + *use_colors = false; + return; + } - if (log_file) { - sd_daemon = false; - use_colors = false; - break; - } + if (!isatty(fd)) { + *sd_daemon = true; + *use_colors = false; + return; + } - if (getenv("NO_COLOR")) { - sd_daemon = false; - use_colors = false; - break; - } + /* systemd wouldn't normally set TERM */ + e = getenv("TERM"); + if (!e) { + *sd_daemon = true; + *use_colors = false; + return; + } - fd = fileno(stderr); - if (fd < 0) { - /* Umm... */ - sd_daemon = true; - use_colors = false; - break; - } + if (streq(e, "dumb")) { + *sd_daemon = false; + *use_colors = false; + return; + } - if (!isatty(fd)) { - sd_daemon = true; - use_colors = false; - break; - } + *sd_daemon = false; + *use_colors = true; +} - /* systemd wouldn't normally set TERM */ - e = getenv("TERM"); - if (!e) { - sd_daemon = true; - use_colors = false; - break; - } +static void msg(enum debug_lvl lvl, const char *fmt, va_list ap) +{ + static bool first = true; + static bool sd_daemon; + const char *color; + const char *sd_lvl; - if (streq(e, "dumb")) { - sd_daemon = false; - use_colors = false; - break; - } + assert_return(lvl != 0 && !empty_str(fmt) && ap); + + if (first) { + bool use_colors; - sd_daemon = false; - use_colors = true; + set_logging_type(&use_colors, &sd_daemon); + if (use_colors) + enable_colors(); + + first = false; } switch (lvl) { case DBG_ERROR: sd_lvl = SD_ERR; - color = use_colors ? ANSI_RED : NULL; + color = ansi_red; break; case DBG_VERBOSE: sd_lvl = SD_INFO; @@ -120,7 +128,7 @@ static void msg(enum debug_lvl lvl, const char *fmt, va_list ap) break; default: sd_lvl = SD_DEBUG; - color = use_colors ? ANSI_GREY : NULL; + color = ansi_grey; break; } @@ -132,7 +140,7 @@ static void msg(enum debug_lvl lvl, const char *fmt, va_list ap) vfprintf(log_file ? log_file : stderr, fmt, ap); if (color) - fprintf(stderr, ANSI_NORMAL); + fprintf(stderr, ansi_normal); } void __debug(enum debug_lvl lvl, const char *fmt, ...) diff --git a/shared/ansi-colors.h b/shared/ansi-colors.h index 8294f2d..6e07e1e 100644 --- a/shared/ansi-colors.h +++ b/shared/ansi-colors.h @@ -1,6 +1,14 @@ #ifndef fooansicolorshfoo #define fooansicolorshfoo +extern const char *ansi_red; +extern const char *ansi_green; +extern const char *ansi_yellow; +extern const char *ansi_blue; +extern const char *ansi_magenta; +extern const char *ansi_grey; +extern const char *ansi_normal; + #define ANSI_RED "\x1B[0;31m" #define ANSI_GREEN "\x1B[0;32m" #define ANSI_YELLOW "\x1B[0;33m" diff --git a/shared/utils.c b/shared/utils.c index 20d9809..13cacac 100644 --- a/shared/utils.c +++ b/shared/utils.c @@ -17,6 +17,25 @@ unsigned debug_mask = 0; +const char *ansi_red = ""; +const char *ansi_green = ""; +const char *ansi_yellow = ""; +const char *ansi_blue = ""; +const char *ansi_magenta = ""; +const char *ansi_grey = ""; +const char *ansi_normal = ""; + +void enable_colors() +{ + ansi_red = ANSI_RED; + ansi_green = ANSI_GREEN; + ansi_yellow = ANSI_YELLOW; + ansi_blue = ANSI_BLUE; + ansi_magenta = ANSI_MAGENTA; + ansi_grey = ANSI_GREY; + ansi_normal = ANSI_NORMAL; +} + void socket_set_low_latency(int sfd, bool keepalive, bool iptos, bool nodelay) { int option; diff --git a/shared/utils.h b/shared/utils.h index 9171648..f5af292 100644 --- a/shared/utils.h +++ b/shared/utils.h @@ -66,6 +66,8 @@ struct saddr { struct list_head list; }; +void enable_colors(); + void socket_set_low_latency(int sfd, bool keepalive, bool iptos, bool nodelay); char *saddr_addr(struct saddr *saddr, char *buf, size_t len); |