Jump to content

Color Console Output


reepblue
 Share

Go to solution Solved by Josh,

Recommended Posts

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.

  • Upvote 2

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

  • 4 months later...
  • Solution

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;
}

 

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

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...