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