summaryrefslogtreecommitdiff
path: root/minecproxy
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-07-01 17:28:09 +0200
committerDavid Härdeman <david@hardeman.nu>2020-07-01 17:28:09 +0200
commite045a7b80cc0cb96b79456f9c74c3705989a12df (patch)
tree00532a51671d2835217997dfdb204ba863fb0a1e /minecproxy
parent829ea0768672dad977ddeb30dd5e2b254bb4a4e9 (diff)
Turn colors into global variables
Diffstat (limited to 'minecproxy')
-rw-r--r--minecproxy/main.c120
1 files changed, 64 insertions, 56 deletions
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, ...)