diff --git a/include/h.h b/include/h.h index 7360202e3..b2f63bb45 100644 --- a/include/h.h +++ b/include/h.h @@ -1054,6 +1054,7 @@ extern char *get_connect_extinfo(Client *client); extern char *unreal_strftime(char *str); extern void strtolower_safe(char *dst, char *src, int size); extern int running_interactively(void); +extern int terminal_supports_color(void); extern void skip_whitespace(char **p); extern void read_until(char **p, char *stopchars); /* src/unrealdb.c start */ diff --git a/src/log.c b/src/log.c index 00f7238e7..43d5e7cff 100644 --- a/src/log.c +++ b/src/log.c @@ -1044,9 +1044,7 @@ void do_unreal_log_disk(LogLevel loglevel, char *subsystem, char *event_id, Mult else win_log("* [%s] %s\n", log_level_valtostring(loglevel), m->line); #else - char *t = getenv("TERM"); - /* Very lazy color detection */ - if (t && strstr(t, "color")) + if (terminal_supports_color()) { if (show_event_id_console) { diff --git a/src/misc.c b/src/misc.c index eb9bf7579..37e21661e 100644 --- a/src/misc.c +++ b/src/misc.c @@ -2129,6 +2129,34 @@ int running_interactively(void) #endif } +int terminal_supports_color(void) +{ +#ifndef _WIN32 + char *s; + + /* Yeah we check all of stdin, stdout, stderr, because + * or more may be redirected (bin/unrealircd >log 2>&1), + * and then we want to say no to color support. + */ + if (!isatty(0) || !isatty(1) || !isatty(2)) + return 0; + + s = getenv("TERM"); + /* Yeah this is a lazy way to detect color-capable terminals + * but it is good enough for me. + */ + if (s) + { + if (strstr(s, "color") || strstr(s, "ansi")) + return 1; + } + + return 0; +#else + return 0; +#endif +} + /** Skip whitespace (if any) */ void skip_whitespace(char **p) {