1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "base/console.hpp"
4 #include "base/initialize.hpp"
7 using namespace icinga;
9 static ConsoleType l_ConsoleType = Console_Dumb;
11 static void InitializeConsole()
13 l_ConsoleType = Console_Dumb;
17 l_ConsoleType = Console_VT100;
19 l_ConsoleType = Console_Windows;
23 INITIALIZE_ONCE(InitializeConsole);
25 ConsoleColorTag::ConsoleColorTag(int color, ConsoleType consoleType)
26 : m_Color(color), m_ConsoleType(consoleType)
29 std::ostream& icinga::operator<<(std::ostream& fp, const ConsoleColorTag& cct)
32 if (cct.m_ConsoleType == Console_VT100 || Console::GetType(fp) == Console_VT100)
33 Console::PrintVT100ColorCode(fp, cct.m_Color);
35 if (Console::GetType(fp) == Console_Windows) {
37 Console::SetWindowsConsoleColor(fp, cct.m_Color);
44 void Console::SetType(std::ostream& fp, ConsoleType type)
46 if (&fp == &std::cout || &fp == &std::cerr)
50 ConsoleType Console::GetType(std::ostream& fp)
52 if (&fp == &std::cout || &fp == &std::cerr)
59 void Console::PrintVT100ColorCode(std::ostream& fp, int color)
61 if (color == Console_Normal) {
66 switch (color & 0xff) {
67 case Console_ForegroundBlack:
70 case Console_ForegroundRed:
73 case Console_ForegroundGreen:
76 case Console_ForegroundYellow:
79 case Console_ForegroundBlue:
82 case Console_ForegroundMagenta:
85 case Console_ForegroundCyan:
88 case Console_ForegroundWhite:
93 switch (color & 0xff00) {
94 case Console_BackgroundBlack:
97 case Console_BackgroundRed:
100 case Console_BackgroundGreen:
103 case Console_BackgroundYellow:
106 case Console_BackgroundBlue:
109 case Console_BackgroundMagenta:
112 case Console_BackgroundCyan:
115 case Console_BackgroundWhite:
120 if (color & Console_Bold)
124 void Console::SetWindowsConsoleColor(std::ostream& fp, int color)
126 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
129 if (&fp == &std::cout)
130 hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
131 else if (&fp == &std::cerr)
132 hConsole = GetStdHandle(STD_ERROR_HANDLE);
136 if (!GetConsoleScreenBufferInfo(hConsole, &consoleInfo))
141 if (color == Console_Normal)
142 attrs = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
144 switch (color & 0xff) {
145 case Console_ForegroundBlack:
148 case Console_ForegroundRed:
149 attrs |= FOREGROUND_RED;
151 case Console_ForegroundGreen:
152 attrs |= FOREGROUND_GREEN;
154 case Console_ForegroundYellow:
155 attrs |= FOREGROUND_RED | FOREGROUND_GREEN;
157 case Console_ForegroundBlue:
158 attrs |= FOREGROUND_BLUE;
160 case Console_ForegroundMagenta:
161 attrs |= FOREGROUND_RED | FOREGROUND_BLUE;
163 case Console_ForegroundCyan:
164 attrs |= FOREGROUND_GREEN | FOREGROUND_BLUE;
166 case Console_ForegroundWhite:
167 attrs |= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
171 switch (color & 0xff00) {
172 case Console_BackgroundBlack:
175 case Console_BackgroundRed:
176 attrs |= BACKGROUND_RED;
178 case Console_BackgroundGreen:
179 attrs |= BACKGROUND_GREEN;
181 case Console_BackgroundYellow:
182 attrs |= BACKGROUND_RED | BACKGROUND_GREEN;
184 case Console_BackgroundBlue:
185 attrs |= BACKGROUND_BLUE;
187 case Console_BackgroundMagenta:
188 attrs |= BACKGROUND_RED | BACKGROUND_BLUE;
190 case Console_BackgroundCyan:
191 attrs |= BACKGROUND_GREEN | BACKGROUND_BLUE;
193 case Console_BackgroundWhite:
194 attrs |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
198 if (color & Console_Bold)
199 attrs |= FOREGROUND_INTENSITY;
201 SetConsoleTextAttribute(hConsole, attrs);