1
0
mirror of https://github.com/unrealircd/unrealircd.git synced 2026-07-09 22:13:11 +02:00

Add terminal_supports_color(), used by logging to terminal code.

We now also correctly disable color support if someone is on
a color-capable terminal but redirects the output of the boot
to a file, eg: bin/unrealircd >boot.log 2>&1
This commit is contained in:
Bram Matthys
2021-08-10 09:20:00 +02:00
parent e2be262088
commit d59cfa092a
3 changed files with 30 additions and 3 deletions
+1
View File
@@ -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 */
+1 -3
View File
@@ -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)
{
+28
View File
@@ -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)
{