summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorlinuxdaemon <linuxdaemon@users.noreply.github.com>2019-01-03 07:17:23 -0600
committerPeter Powell <petpow@saberuk.com>2019-01-03 13:17:23 +0000
commitb6ca49a232c24701ba72630619683672303386c8 (patch)
treef5396fd6b6bf0355f0b44b4d24cc86f2f2d4d2f5 /include
parente433401fb883584d32b9b8159370ee57823b01bf (diff)
Add the ability to disable console colors in stdout logging (#1539).
Diffstat (limited to 'include')
-rw-r--r--include/consolecolors.h51
1 files changed, 45 insertions, 6 deletions
diff --git a/include/consolecolors.h b/include/consolecolors.h
index d92be58bc..8d154f7ee 100644
--- a/include/consolecolors.h
+++ b/include/consolecolors.h
@@ -18,6 +18,27 @@
#pragma once
#include <ostream>
+#include <stdio.h>
+
+#ifdef _WIN32
+# include <io.h>
+# define isatty(x) _isatty((x))
+# define fileno(x) _fileno((x))
+#else
+# include <unistd.h>
+#endif
+
+namespace
+{
+ inline bool CanUseColors()
+ {
+#ifdef INSPIRCD_DISABLE_COLORS
+ return false;
+#else
+ return isatty(fileno(stdout));
+#endif
+ }
+}
#ifdef _WIN32
@@ -29,37 +50,43 @@ extern HANDLE g_hStdout;
inline std::ostream& con_green(std::ostream &s)
{
- SetConsoleTextAttribute(g_hStdout, FOREGROUND_GREEN|FOREGROUND_INTENSITY|g_wBackgroundColor);
+ if (CanUseColors())
+ SetConsoleTextAttribute(g_hStdout, FOREGROUND_GREEN|FOREGROUND_INTENSITY|g_wBackgroundColor);
return s;
}
inline std::ostream& con_red(std::ostream &s)
{
- SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_INTENSITY|g_wBackgroundColor);
+ if (CanUseColors())
+ SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_INTENSITY|g_wBackgroundColor);
return s;
}
inline std::ostream& con_white(std::ostream &s)
{
- SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN|g_wBackgroundColor);
+ if (CanUseColors())
+ SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN|g_wBackgroundColor);
return s;
}
inline std::ostream& con_white_bright(std::ostream &s)
{
- SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_INTENSITY|g_wBackgroundColor);
+ if (CanUseColors())
+ SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_INTENSITY|g_wBackgroundColor);
return s;
}
inline std::ostream& con_bright(std::ostream &s)
{
- SetConsoleTextAttribute(g_hStdout, FOREGROUND_INTENSITY|g_wBackgroundColor);
+ if (CanUseColors())
+ SetConsoleTextAttribute(g_hStdout, FOREGROUND_INTENSITY|g_wBackgroundColor);
return s;
}
inline std::ostream& con_reset(std::ostream &s)
{
- SetConsoleTextAttribute(g_hStdout, g_wOriginalColors);
+ if (CanUseColors())
+ SetConsoleTextAttribute(g_hStdout, g_wOriginalColors);
return s;
}
@@ -67,31 +94,43 @@ inline std::ostream& con_reset(std::ostream &s)
inline std::ostream& con_green(std::ostream &s)
{
+ if (!CanUseColors())
+ return s;
return s << "\033[1;32m";
}
inline std::ostream& con_red(std::ostream &s)
{
+ if (!CanUseColors())
+ return s;
return s << "\033[1;31m";
}
inline std::ostream& con_white(std::ostream &s)
{
+ if (!CanUseColors())
+ return s;
return s << "\033[0m";
}
inline std::ostream& con_white_bright(std::ostream &s)
{
+ if (!CanUseColors())
+ return s;
return s << "\033[1m";
}
inline std::ostream& con_bright(std::ostream &s)
{
+ if (!CanUseColors())
+ return s;
return s << "\033[1m";
}
inline std::ostream& con_reset(std::ostream &s)
{
+ if (!CanUseColors())
+ return s;
return s << "\033[0m";
}