reepblue Posted November 27, 2022 Share Posted November 27, 2022 This would be nice to identify what messages are from Print, DebugLog, Warn, etc. It'll help make errors pop out too. Here's how I'd do it. Works on all platforms. enum PrintColor { PRINT_COLOR_NORM = 0, PRINT_COLOR_MAGENTA, PRINT_COLOR_GREEN, PRINT_COLOR_BLUE, PRINT_COLOR_YELLOW, PRINT_COLOR_RED }; void PrintWString(const std::wstring& message, PrintColor printcolor) { #if defined (_WIN32) HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // Apply a log tag switch (printcolor) { case PRINT_COLOR_MAGENTA: SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY); break; case PRINT_COLOR_GREEN: SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY); break; case PRINT_COLOR_BLUE: SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY); break; case PRINT_COLOR_YELLOW: SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY); break; case PRINT_COLOR_RED: SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY); break; default: SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); break; } std::wcout << message << std::endl; SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); #else static const std::wstring ColorRed(L"\033[0;31m"); static const std::wstring ColorYellow(L"\033[1;33m"); static const std::wstring ColorGreen(L"\033[1;32m"); static const std::wstring ColorCyan(L"\033[0;36m"); static const std::wstring ColorMagenta(L"\033[0;35m"); static const std::wstring ColorDefault(L"\033[0m"); switch (printcolor) { case PRINT_COLOR_MAGENTA: std::wcout << ColorMagenta; break; case PRINT_COLOR_GREEN: std::wcout << ColorGreen; break; case PRINT_COLOR_BLUE: std::wcout << ColorCyan; break; case PRINT_COLOR_YELLOW: std::wcout << ColorYellow; break; case PRINT_COLOR_RED: std::wcout << ColorRed; break; default: std::wcout << ColorDefault; break; } std::wcout << message << ColorDefault << std::endl; #endif } I don't think this changes the color in Visual Studio's output window, but I'll be someone who will still be forcing the console window for my debug builds. 2 Quote Cyclone - Ultra Game System - Component Preprocessor - Tex2TGA - Darkness Awaits Template (Leadwerks) If you like my work, consider supporting me on Patreon! Link to comment Share on other sites More sharing options...
Solution Josh Posted April 12, 2023 Solution Share Posted April 12, 2023 How about this? #include "UltraEngine.h" using namespace UltraEngine; bool PrintColorHook(const Event& e, shared_ptr<Object> extra) { //https://learn.microsoft.com/en-us/windows/console/getstdhandle#handle-disposal HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); if (e.text.Left(8) == "Warning:") { SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY); } else if (e.text.Left(6) == "Error:") { SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY); } else { SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); } return true; } int main(int argc, const char* argv[]) { ListenEvent(EVENT_PRINT, NULL, PrintColorHook); Print("Here is a normal text message."); Print("Warning: Here is a warning."); Print("Here is a normal text message."); Print("Error: Here is an error."); Print("Here is a normal text message."); return 0; } Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
reepblue Posted April 12, 2023 Author Share Posted April 12, 2023 Idk why I didn't think of that before. I guess that could work too. I ended up making a wrapper function but I wanna revisit that to make sure I'm not losing anything. Quote Cyclone - Ultra Game System - Component Preprocessor - Tex2TGA - Darkness Awaits Template (Leadwerks) If you like my work, consider supporting me on Patreon! Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.