summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSadie Powell <sadie@witchery.services>2021-03-03 01:52:27 +0000
committerSadie Powell <sadie@witchery.services>2021-03-03 01:52:27 +0000
commitd55581a1107de1e548b67d29a9ca0fcb7f1a0572 (patch)
tree7e84901a544935455efd5ade99222960a3a7c9ea
parentdbaa0f6be119a1e74e6139d240d77efcba43b504 (diff)
Fix not being able to colour format output to stderr on Windows.
-rw-r--r--include/consolecolors.h51
-rw-r--r--src/inspircd.cpp24
-rw-r--r--win/inspircd_win32wrapper.cpp13
-rw-r--r--win/inspircd_win32wrapper.h8
4 files changed, 67 insertions, 29 deletions
diff --git a/include/consolecolors.h b/include/consolecolors.h
index fdc351f5a..e8329c44e 100644
--- a/include/consolecolors.h
+++ b/include/consolecolors.h
@@ -25,9 +25,12 @@
#include <stdio.h>
#ifdef _WIN32
+# include <iostream>
# include <io.h>
# define isatty(x) _isatty((x))
# define fileno(x) _fileno((x))
+extern WindowsStream StandardError;
+extern WindowsStream StandardOutput;
#else
# include <unistd.h>
#endif
@@ -42,55 +45,83 @@ namespace
return isatty(fileno(stdout));
#endif
}
+
+#ifdef _WIN32
+ inline WindowsStream& GetStreamHandle(std::ostream& os)
+ {
+ if (os.rdbuf() == std::cerr.rdbuf())
+ return StandardError;
+
+ if (os.rdbuf() == std::cout.rdbuf())
+ return StandardOutput;
+
+ // This will never happen.
+ throw std::invalid_argument("Tried to write color codes to a stream other than stdout or stderr!");
+ }
+#endif
}
#ifdef _WIN32
#include <windows.h>
-extern WORD g_wOriginalColors;
-extern WORD g_wBackgroundColor;
-extern HANDLE g_hStdout;
-
inline std::ostream& con_green(std::ostream& stream)
{
if (CanUseColors())
- SetConsoleTextAttribute(g_hStdout, FOREGROUND_GREEN|FOREGROUND_INTENSITY|g_wBackgroundColor);
+ {
+ const WindowsStream& ws = GetStreamHandle(stream);
+ SetConsoleTextAttribute(ws.Handle, FOREGROUND_GREEN | FOREGROUND_INTENSITY | ws.BackgroundColor);
+ }
return stream;
}
inline std::ostream& con_red(std::ostream& stream)
{
if (CanUseColors())
- SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_INTENSITY|g_wBackgroundColor);
+ {
+ const WindowsStream& ws = GetStreamHandle(stream);
+ SetConsoleTextAttribute(ws.Handle, FOREGROUND_RED | FOREGROUND_INTENSITY | ws.BackgroundColor);
+ }
return stream;
}
inline std::ostream& con_white(std::ostream& stream)
{
if (CanUseColors())
- SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN|g_wBackgroundColor);
+ {
+ const WindowsStream& ws = GetStreamHandle(stream);
+ SetConsoleTextAttribute(ws.Handle, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | ws.BackgroundColor);
+ }
return stream;
}
inline std::ostream& con_white_bright(std::ostream& stream)
{
if (CanUseColors())
- SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_INTENSITY|g_wBackgroundColor);
+ {
+ const WindowsStream& ws = GetStreamHandle(stream);
+ SetConsoleTextAttribute(ws.Handle, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY | ws.BackgroundColor);
+ }
return stream;
}
inline std::ostream& con_bright(std::ostream& stream)
{
if (CanUseColors())
- SetConsoleTextAttribute(g_hStdout, FOREGROUND_INTENSITY|g_wBackgroundColor);
+ {
+ const WindowsStream& ws = GetStreamHandle(stream);
+ SetConsoleTextAttribute(ws.Handle, FOREGROUND_INTENSITY | ws.BackgroundColor);
+ }
return stream;
}
inline std::ostream& con_reset(std::ostream& stream)
{
if (CanUseColors())
- SetConsoleTextAttribute(g_hStdout, g_wOriginalColors);
+ {
+ const WindowsStream& ws = GetStreamHandle(stream);
+ SetConsoleTextAttribute(ws.Handle, ws.ForegroundColor);
+ }
return stream;
}
diff --git a/src/inspircd.cpp b/src/inspircd.cpp
index c0d7a1614..fca3af6b4 100644
--- a/src/inspircd.cpp
+++ b/src/inspircd.cpp
@@ -41,9 +41,11 @@
#include <pwd.h> // setuid
#include <grp.h> // setgid
#else
- WORD g_wOriginalColors;
- WORD g_wBackgroundColor;
- HANDLE g_hStdout;
+ /** Manages formatting lines written to stderr on Windows. */
+ WindowsStream StandardError(STD_ERROR_HANDLE);
+
+ /** Manages formatting lines written to stdout on Windows. */
+ WindowsStream StandardOutput(STD_OUTPUT_HANDLE);
#endif
#include <fstream>
@@ -492,22 +494,6 @@ InspIRCd::InspIRCd(int argc, char** argv)
this->Config->cmdline.argc = argc;
ParseOptions();
-#ifdef _WIN32
- // Initialize the console values
- g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_SCREEN_BUFFER_INFO bufinf;
- if(GetConsoleScreenBufferInfo(g_hStdout, &bufinf))
- {
- g_wOriginalColors = bufinf.wAttributes & 0x00FF;
- g_wBackgroundColor = bufinf.wAttributes & 0x00F0;
- }
- else
- {
- g_wOriginalColors = FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN;
- g_wBackgroundColor = 0;
- }
-#endif
-
{
ServiceProvider* provs[] =
{
diff --git a/win/inspircd_win32wrapper.cpp b/win/inspircd_win32wrapper.cpp
index c6f5d8232..169741ba5 100644
--- a/win/inspircd_win32wrapper.cpp
+++ b/win/inspircd_win32wrapper.cpp
@@ -55,3 +55,16 @@ DWORD CWin32Exception::GetErrorCode()
{
return dwErrorCode;
}
+
+WindowsStream::WindowsStream(DWORD handle)
+ : BackgroundColor(0)
+ , ForegroundColor(FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN)
+{
+ this->Handle = GetStdHandle(handle);
+ CONSOLE_SCREEN_BUFFER_INFO bufinf;
+ if (GetConsoleScreenBufferInfo(this->Handle, &bufinf))
+ {
+ this->BackgroundColor = bufinf.wAttributes & 0x00F0;
+ this->ForegroundColor = bufinf.wAttributes & 0x00FF;
+ }
+}
diff --git a/win/inspircd_win32wrapper.h b/win/inspircd_win32wrapper.h
index 49fc1c665..0f492c924 100644
--- a/win/inspircd_win32wrapper.h
+++ b/win/inspircd_win32wrapper.h
@@ -196,3 +196,11 @@ struct sockaddr_un
ADDRESS_FAMILY sun_family;
char sun_path[6];
};
+
+struct WindowsStream
+{
+ WORD BackgroundColor;
+ WORD ForegroundColor;
+ HANDLE Handle;
+ WindowsStream(DWORD handle);
+}; \ No newline at end of file