1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "base/console.hpp"
21 #include "base/initialize.hpp"
24 using namespace icinga;
26 INITIALIZE_ONCE(&Console::DetectType);
28 static ConsoleType l_ConsoleType = Console_Dumb;
30 ConsoleColorTag::ConsoleColorTag(int color, ConsoleType consoleType)
31 : m_Color(color), m_ConsoleType(consoleType)
34 std::ostream& icinga::operator<<(std::ostream& fp, const ConsoleColorTag& cct)
37 if (cct.m_ConsoleType == Console_VT100 || Console::GetType(fp) == Console_VT100)
38 Console::PrintVT100ColorCode(fp, cct.m_Color);
40 if (Console::GetType(fp) == Console_Windows) {
42 Console::SetWindowsConsoleColor(fp, cct.m_Color);
49 void Console::DetectType(void)
51 l_ConsoleType = Console_Dumb;
55 l_ConsoleType = Console_VT100;
57 l_ConsoleType = Console_Windows;
61 void Console::SetType(std::ostream& fp, ConsoleType type)
63 if (&fp == &std::cout || &fp == &std::cerr)
67 ConsoleType Console::GetType(std::ostream& fp)
69 if (&fp == &std::cout || &fp == &std::cerr)
76 void Console::PrintVT100ColorCode(std::ostream& fp, int color)
78 if (color == Console_Normal) {
83 switch (color & 0xff) {
84 case Console_ForegroundBlack:
87 case Console_ForegroundRed:
90 case Console_ForegroundGreen:
93 case Console_ForegroundYellow:
96 case Console_ForegroundBlue:
99 case Console_ForegroundMagenta:
102 case Console_ForegroundCyan:
105 case Console_ForegroundWhite:
110 switch (color & 0xff00) {
111 case Console_BackgroundBlack:
114 case Console_BackgroundRed:
117 case Console_BackgroundGreen:
120 case Console_BackgroundYellow:
123 case Console_BackgroundBlue:
126 case Console_BackgroundMagenta:
129 case Console_BackgroundCyan:
132 case Console_BackgroundWhite:
137 if (color & Console_Bold)
141 void Console::SetWindowsConsoleColor(std::ostream& fp, int color)
143 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
146 if (&fp == &std::cout)
147 hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
148 else if (&fp == &std::cerr)
149 hConsole = GetStdHandle(STD_ERROR_HANDLE);
153 if (!GetConsoleScreenBufferInfo(hConsole, &consoleInfo))
158 if (color == Console_Normal)
159 attrs = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
161 switch (color & 0xff) {
162 case Console_ForegroundBlack:
165 case Console_ForegroundRed:
166 attrs |= FOREGROUND_RED;
168 case Console_ForegroundGreen:
169 attrs |= FOREGROUND_GREEN;
171 case Console_ForegroundYellow:
172 attrs |= FOREGROUND_RED | FOREGROUND_GREEN;
174 case Console_ForegroundBlue:
175 attrs |= FOREGROUND_BLUE;
177 case Console_ForegroundMagenta:
178 attrs |= FOREGROUND_RED | FOREGROUND_BLUE;
180 case Console_ForegroundCyan:
181 attrs |= FOREGROUND_GREEN | FOREGROUND_BLUE;
183 case Console_ForegroundWhite:
184 attrs |= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
188 switch (color & 0xff00) {
189 case Console_BackgroundBlack:
192 case Console_BackgroundRed:
193 attrs |= BACKGROUND_RED;
195 case Console_BackgroundGreen:
196 attrs |= BACKGROUND_GREEN;
198 case Console_BackgroundYellow:
199 attrs |= BACKGROUND_RED | BACKGROUND_GREEN;
201 case Console_BackgroundBlue:
202 attrs |= BACKGROUND_BLUE;
204 case Console_BackgroundMagenta:
205 attrs |= BACKGROUND_RED | BACKGROUND_BLUE;
207 case Console_BackgroundCyan:
208 attrs |= BACKGROUND_GREEN | BACKGROUND_BLUE;
210 case Console_BackgroundWhite:
211 attrs |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
215 if (color & Console_Bold)
216 attrs |= FOREGROUND_INTENSITY;
218 SetConsoleTextAttribute(hConsole, attrs);