blob: 99a5fad3dfcaa6a9b9a6840b8521337b37332440 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "base/console.hpp"
#include "base/initialize.hpp"
#include <iostream>
using namespace icinga;
static ConsoleType l_ConsoleType = Console_Dumb;
static void InitializeConsole()
{
l_ConsoleType = Console_Dumb;
#ifndef _WIN32
if (isatty(1))
l_ConsoleType = Console_VT100;
#else /* _WIN32 */
l_ConsoleType = Console_Windows;
#endif /* _WIN32 */
}
INITIALIZE_ONCE(InitializeConsole);
ConsoleColorTag::ConsoleColorTag(int color, ConsoleType consoleType)
: m_Color(color), m_ConsoleType(consoleType)
{ }
std::ostream& icinga::operator<<(std::ostream& fp, const ConsoleColorTag& cct)
{
#ifndef _WIN32
if (cct.m_ConsoleType == Console_VT100 || Console::GetType(fp) == Console_VT100)
Console::PrintVT100ColorCode(fp, cct.m_Color);
#else /* _WIN32 */
if (Console::GetType(fp) == Console_Windows) {
fp.flush();
Console::SetWindowsConsoleColor(fp, cct.m_Color);
}
#endif /* _WIN32 */
return fp;
}
void Console::SetType(std::ostream& fp, ConsoleType type)
{
if (&fp == &std::cout || &fp == &std::cerr)
l_ConsoleType = type;
}
ConsoleType Console::GetType(std::ostream& fp)
{
if (&fp == &std::cout || &fp == &std::cerr)
return l_ConsoleType;
else
return Console_Dumb;
}
#ifndef _WIN32
void Console::PrintVT100ColorCode(std::ostream& fp, int color)
{
if (color == Console_Normal) {
fp << "\33[0m";
return;
}
switch (color & 0xff) {
case Console_ForegroundBlack:
fp << "\33[30m";
break;
case Console_ForegroundRed:
fp << "\33[31m";
break;
case Console_ForegroundGreen:
fp << "\33[32m";
break;
case Console_ForegroundYellow:
fp << "\33[33m";
break;
case Console_ForegroundBlue:
fp << "\33[34m";
break;
case Console_ForegroundMagenta:
fp << "\33[35m";
break;
case Console_ForegroundCyan:
fp << "\33[36m";
break;
case Console_ForegroundWhite:
fp << "\33[37m";
break;
}
switch (color & 0xff00) {
case Console_BackgroundBlack:
fp << "\33[40m";
break;
case Console_BackgroundRed:
fp << "\33[41m";
break;
case Console_BackgroundGreen:
fp << "\33[42m";
break;
case Console_BackgroundYellow:
fp << "\33[43m";
break;
case Console_BackgroundBlue:
fp << "\33[44m";
break;
case Console_BackgroundMagenta:
fp << "\33[45m";
break;
case Console_BackgroundCyan:
fp << "\33[46m";
break;
case Console_BackgroundWhite:
fp << "\33[47m";
break;
}
if (color & Console_Bold)
fp << "\33[1m";
}
#else /* _WIN32 */
void Console::SetWindowsConsoleColor(std::ostream& fp, int color)
{
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
HANDLE hConsole;
if (&fp == &std::cout)
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
else if (&fp == &std::cerr)
hConsole = GetStdHandle(STD_ERROR_HANDLE);
else
return;
if (!GetConsoleScreenBufferInfo(hConsole, &consoleInfo))
return;
WORD attrs = 0;
if (color == Console_Normal)
attrs = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
switch (color & 0xff) {
case Console_ForegroundBlack:
attrs |= 0;
break;
case Console_ForegroundRed:
attrs |= FOREGROUND_RED;
break;
case Console_ForegroundGreen:
attrs |= FOREGROUND_GREEN;
break;
case Console_ForegroundYellow:
attrs |= FOREGROUND_RED | FOREGROUND_GREEN;
break;
case Console_ForegroundBlue:
attrs |= FOREGROUND_BLUE;
break;
case Console_ForegroundMagenta:
attrs |= FOREGROUND_RED | FOREGROUND_BLUE;
break;
case Console_ForegroundCyan:
attrs |= FOREGROUND_GREEN | FOREGROUND_BLUE;
break;
case Console_ForegroundWhite:
attrs |= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
break;
}
switch (color & 0xff00) {
case Console_BackgroundBlack:
attrs |= 0;
break;
case Console_BackgroundRed:
attrs |= BACKGROUND_RED;
break;
case Console_BackgroundGreen:
attrs |= BACKGROUND_GREEN;
break;
case Console_BackgroundYellow:
attrs |= BACKGROUND_RED | BACKGROUND_GREEN;
break;
case Console_BackgroundBlue:
attrs |= BACKGROUND_BLUE;
break;
case Console_BackgroundMagenta:
attrs |= BACKGROUND_RED | BACKGROUND_BLUE;
break;
case Console_BackgroundCyan:
attrs |= BACKGROUND_GREEN | BACKGROUND_BLUE;
break;
case Console_BackgroundWhite:
attrs |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
break;
}
if (color & Console_Bold)
attrs |= FOREGROUND_INTENSITY;
SetConsoleTextAttribute(hConsole, attrs);
}
#endif /* _WIN32 */
|