From e045a7b80cc0cb96b79456f9c74c3705989a12df Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Wed, 1 Jul 2020 17:28:09 +0200 Subject: Turn colors into global variables --- minecproxy/main.c | 120 +++++++++++++++++++++++++++++------------------------- 1 file changed, 64 insertions(+), 56 deletions(-) (limited to 'minecproxy') 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, ...) -- cgit v1.2.3