1 | // %pacpus:license{
|
---|
2 | // This file is part of the PACPUS framework distributed under the
|
---|
3 | // CECILL-C License, Version 1.0.
|
---|
4 | // %pacpus:license}
|
---|
5 | /// @version $Id: Log.cpp 76 2013-01-10 17:05:10Z kurdejma $
|
---|
6 | /// @file
|
---|
7 | /// @author Marek Kurdej <firstname.surname@utc.fr>
|
---|
8 | /// @date 2013-11-28
|
---|
9 | /// @version $Id$
|
---|
10 | /// @copyright Copyright (c) UTC/CNRS Heudiasyc 2006 - 2013. All rights reserved.
|
---|
11 | /// @brief Color logger output depending on severity level.
|
---|
12 | ///
|
---|
13 | /// Detailed description.
|
---|
14 |
|
---|
15 | #ifndef COLORSEVERITYFORMATTER_HPP
|
---|
16 | #define COLORSEVERITYFORMATTER_HPP
|
---|
17 |
|
---|
18 | #if defined(PACPUS_LOG_COLORED_OUTPUT)
|
---|
19 |
|
---|
20 | #include <boost/assert.hpp>
|
---|
21 | #include <boost/log/attributes/attribute_name.hpp>
|
---|
22 | #include <boost/log/attributes/fallback_policy.hpp>
|
---|
23 | #include <boost/log/attributes/value_visitation.hpp>
|
---|
24 | #include <boost/log/expressions.hpp>
|
---|
25 | #include <boost/log/utility/functional/bind.hpp>
|
---|
26 | #include <cstdlib>
|
---|
27 | #include <sstream>
|
---|
28 | #include <string>
|
---|
29 |
|
---|
30 | // could use Boost.Predef with Boost >= 1.55
|
---|
31 | #if defined(WIN32) || defined(_WINDOWS)
|
---|
32 | # define PACPUS_OS_WINDOWS 1
|
---|
33 | #elif defined(__unix) || defined(__unix__)
|
---|
34 | # define PACPUS_OS_UNIX 1
|
---|
35 | # if defined(__linux) || defined(__linux__)
|
---|
36 | # define PACPUS_OS_LINUX 1
|
---|
37 | # endif
|
---|
38 | #elif defined(__APPLE__) || defined(__MACH__) || defined(Macintosh) || defined(macintosh)
|
---|
39 | # define PACPUS_OS_MACOS 1
|
---|
40 | #else
|
---|
41 | // unknown system
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | #if defined(PACPUS_OS_WINDOWS)
|
---|
45 | # include <Windows.h>
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | namespace pacpus
|
---|
49 | {
|
---|
50 |
|
---|
51 | enum Color {
|
---|
52 | COLOR_DEFAULT,
|
---|
53 | COLOR_BLACK,
|
---|
54 | COLOR_RED,
|
---|
55 | COLOR_GREEN,
|
---|
56 | COLOR_YELLOW,
|
---|
57 | COLOR_BLUE,
|
---|
58 | COLOR_MAGENTA,
|
---|
59 | COLOR_CYAN,
|
---|
60 | COLOR_WHITE
|
---|
61 | };
|
---|
62 |
|
---|
63 | Color getColor(SeverityLevel const& sev)
|
---|
64 | {
|
---|
65 | if (sev >= error) {
|
---|
66 | return COLOR_RED;
|
---|
67 | } else if (sev >= warning) {
|
---|
68 | return COLOR_YELLOW;
|
---|
69 | } else if (sev >= info) {
|
---|
70 | return COLOR_WHITE; //COLOR_GREEN;
|
---|
71 | }
|
---|
72 | return COLOR_DEFAULT;
|
---|
73 | }
|
---|
74 |
|
---|
75 | #if defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
|
---|
76 |
|
---|
77 | // Returns the character attribute for the given color.
|
---|
78 | WORD getColorAttribute(Color color)
|
---|
79 | {
|
---|
80 | switch (color) {
|
---|
81 | case COLOR_BLACK: return 0;
|
---|
82 | case COLOR_RED: return FOREGROUND_RED;
|
---|
83 | case COLOR_GREEN: return FOREGROUND_GREEN;
|
---|
84 | case COLOR_YELLOW: return FOREGROUND_RED | FOREGROUND_GREEN;
|
---|
85 | case COLOR_BLUE: return FOREGROUND_BLUE;
|
---|
86 | case COLOR_MAGENTA: return FOREGROUND_RED | FOREGROUND_BLUE;
|
---|
87 | case COLOR_CYAN: return FOREGROUND_GREEN | FOREGROUND_BLUE;
|
---|
88 | case COLOR_WHITE: return FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
|
---|
89 | case COLOR_DEFAULT: return 0;
|
---|
90 | default:
|
---|
91 | BOOST_ASSERT(false && "wrong Color enum value");
|
---|
92 | return 0;
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | #else
|
---|
97 |
|
---|
98 | /// @returns the ANSI color code for the given color. COLOR_DEFAULT is
|
---|
99 | /// an invalid input.
|
---|
100 | std::string getAnsiColorCode(Color color)
|
---|
101 | {
|
---|
102 | const char* kEscapeSequence = "\033[";
|
---|
103 | // const char* kBackground = "";
|
---|
104 | const char* kFontRegular = "0;";
|
---|
105 | // const char* kFontBold = "1;";
|
---|
106 | // const char* kFontWeak = "2;";
|
---|
107 | // const char* kFontStrong = "3;";
|
---|
108 | // const char* kFontUnderline = "4;";
|
---|
109 |
|
---|
110 | std::stringstream ss;
|
---|
111 | ss << kEscapeSequence << kFontRegular;
|
---|
112 |
|
---|
113 | switch (color) {
|
---|
114 | case COLOR_BLACK: ss << "30"; break;
|
---|
115 | case COLOR_RED: ss << "31"; break;
|
---|
116 | case COLOR_GREEN: ss << "32"; break;
|
---|
117 | case COLOR_YELLOW: ss << "33"; break;
|
---|
118 | case COLOR_BLUE: ss << "34"; break;
|
---|
119 | case COLOR_MAGENTA: ss << "35"; break;
|
---|
120 | case COLOR_CYAN: ss << "36"; break;
|
---|
121 | case COLOR_WHITE: ss << "37"; break;
|
---|
122 | default: return "";
|
---|
123 | };
|
---|
124 |
|
---|
125 | const char* kPostfix = "m";
|
---|
126 | ss << kPostfix;
|
---|
127 |
|
---|
128 | return ss.str();
|
---|
129 | }
|
---|
130 |
|
---|
131 | std::string getAnsiColorCodeRestoreDefault()
|
---|
132 | {
|
---|
133 | return "\033[0m";
|
---|
134 | }
|
---|
135 |
|
---|
136 | #endif // defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
|
---|
137 |
|
---|
138 | bool shouldUseColor(bool stdoutIsTty)
|
---|
139 | {
|
---|
140 | #if defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
|
---|
141 | return true;
|
---|
142 | #else
|
---|
143 | // On non-Windows platforms, we rely on the TERM variable.
|
---|
144 | std::string const term = std::getenv("TERM");
|
---|
145 | const bool termSupportsColor = (term == "xterm")
|
---|
146 | || (term == "xterm-color")
|
---|
147 | || (term == "xterm-256color")
|
---|
148 | || (term == "screen")
|
---|
149 | || (term == "linux")
|
---|
150 | || (term == "cygwin");
|
---|
151 | return stdoutIsTty && termSupportsColor;
|
---|
152 | #endif // defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
|
---|
153 | }
|
---|
154 |
|
---|
155 | template < typename CharT >
|
---|
156 | struct ColorFormatter
|
---|
157 | {
|
---|
158 | ColorFormatter()
|
---|
159 | #if defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
|
---|
160 | : mStreamHandle(GetStdHandle(STD_OUTPUT_HANDLE))
|
---|
161 | #endif // defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
|
---|
162 | {
|
---|
163 | mShouldUseColor = shouldUseColor(/*stdoutIsTty*/ true);
|
---|
164 | }
|
---|
165 |
|
---|
166 | void operator()(boost::log::basic_formatting_ostream<CharT>& strm, SeverityLevel const& sev)
|
---|
167 | {
|
---|
168 | if (!mShouldUseColor) {
|
---|
169 | return;
|
---|
170 | }
|
---|
171 | #if defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
|
---|
172 | // Gets the current text color.
|
---|
173 | CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
|
---|
174 | GetConsoleScreenBufferInfo(mStreamHandle, &bufferInfo);
|
---|
175 | mSavedConsoleBufferInfo = bufferInfo.wAttributes;
|
---|
176 |
|
---|
177 | // We need to flush the stream buffers into the console before each
|
---|
178 | // SetConsoleTextAttribute call lest it affect the text that is already
|
---|
179 | // printed but has not yet reached the console.
|
---|
180 | fflush(stdout);
|
---|
181 | SetConsoleTextAttribute(mStreamHandle, getColorAttribute(getColor(sev)) | FOREGROUND_INTENSITY);
|
---|
182 | #else
|
---|
183 | strm << getAnsiColorCode(getColor(sev)).c_str();
|
---|
184 | #endif // defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
|
---|
185 | }
|
---|
186 |
|
---|
187 | bool mShouldUseColor;
|
---|
188 | #if defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
|
---|
189 | const HANDLE mStreamHandle;
|
---|
190 | WORD mSavedConsoleBufferInfo;
|
---|
191 | #endif // defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
|
---|
192 | };
|
---|
193 |
|
---|
194 | template < typename CharT >
|
---|
195 | struct DefaultFormatter
|
---|
196 | {
|
---|
197 | DefaultFormatter()
|
---|
198 | #if defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
|
---|
199 | //DefaultFormatter(WORD savedConsoleBufferInfo)
|
---|
200 | : mStreamHandle(GetStdHandle(STD_OUTPUT_HANDLE))
|
---|
201 | //, mSavedConsoleBufferInfo(savedConsoleBufferInfo)
|
---|
202 | , mSavedConsoleBufferInfo(getColorAttribute(COLOR_WHITE)) // FIXME: restore old value, not just reset
|
---|
203 | #endif // defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
|
---|
204 | {
|
---|
205 | mShouldUseColor = shouldUseColor(/*stdoutIsTty*/ true);
|
---|
206 | }
|
---|
207 |
|
---|
208 | void operator()(boost::log::basic_formatting_ostream<CharT>& strm, SeverityLevel const& /*sev*/)
|
---|
209 | {
|
---|
210 | if (!mShouldUseColor) {
|
---|
211 | return;
|
---|
212 | }
|
---|
213 | #if defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
|
---|
214 | fflush(stdout);
|
---|
215 | // Restores the text color.
|
---|
216 | SetConsoleTextAttribute(mStreamHandle, mSavedConsoleBufferInfo);
|
---|
217 | #else
|
---|
218 | strm << getAnsiColorCodeRestoreDefault().c_str();
|
---|
219 | #endif // defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
|
---|
220 | }
|
---|
221 |
|
---|
222 | bool mShouldUseColor;
|
---|
223 | #if defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
|
---|
224 | const HANDLE mStreamHandle;
|
---|
225 | WORD mSavedConsoleBufferInfo;
|
---|
226 | #endif // defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
|
---|
227 | };
|
---|
228 |
|
---|
229 | template< typename T, typename FallbackPolicyT, typename CharT >
|
---|
230 | class FormatSeverityWithColorsTerminal
|
---|
231 | {
|
---|
232 | public:
|
---|
233 | //! Internal typedef for type categorization
|
---|
234 | typedef void _is_boost_log_terminal;
|
---|
235 |
|
---|
236 | //! Attribute value type
|
---|
237 | typedef T value_type;
|
---|
238 | //! Fallback policy
|
---|
239 | typedef FallbackPolicyT fallback_policy;
|
---|
240 | //! Character type
|
---|
241 | typedef CharT char_type;
|
---|
242 | //! String type
|
---|
243 | typedef std::basic_string< char_type > string_type;
|
---|
244 | //! Formatting stream type
|
---|
245 | typedef boost::log::basic_formatting_ostream< char_type > stream_type;
|
---|
246 |
|
---|
247 | //! Formatter function
|
---|
248 | typedef boost::log::aux::light_function< void (stream_type&, value_type const&) > formatter_function_type;
|
---|
249 |
|
---|
250 | //! Function result type
|
---|
251 | typedef string_type result_type;
|
---|
252 |
|
---|
253 | typedef boost::log::attribute_name attribute_name;
|
---|
254 |
|
---|
255 | private:
|
---|
256 | //! Attribute value visitor invoker
|
---|
257 | typedef boost::log::value_visitor_invoker< value_type, fallback_policy > visitor_invoker_type;
|
---|
258 |
|
---|
259 | private:
|
---|
260 | //! Attribute name
|
---|
261 | attribute_name m_name;
|
---|
262 | //! Formattr function
|
---|
263 | formatter_function_type m_formatter;
|
---|
264 | //! Attribute value visitor invoker
|
---|
265 | visitor_invoker_type m_visitor_invoker;
|
---|
266 |
|
---|
267 | public:
|
---|
268 | //! Initializing constructor
|
---|
269 | FormatSeverityWithColorsTerminal(attribute_name const& name, fallback_policy const& fallback, bool restoreDefault)
|
---|
270 | : m_name(name)
|
---|
271 | //, m_formatter(formatter_generator::parse(format))
|
---|
272 | , m_visitor_invoker(fallback)
|
---|
273 | {
|
---|
274 | if (restoreDefault) {
|
---|
275 | //#if defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
|
---|
276 | // m_formatter = formatter_function_type(DefaultFormatter<char_type>());
|
---|
277 | //#else // defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
|
---|
278 | m_formatter = formatter_function_type(DefaultFormatter<char_type>());
|
---|
279 | //#endif // defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
|
---|
280 | } else {
|
---|
281 | m_formatter = formatter_function_type(ColorFormatter<char_type>());
|
---|
282 | }
|
---|
283 | }
|
---|
284 |
|
---|
285 | //! Copy constructor
|
---|
286 | FormatSeverityWithColorsTerminal(FormatSeverityWithColorsTerminal const& that) :
|
---|
287 | m_name(that.m_name), m_formatter(that.m_formatter), m_visitor_invoker(that.m_visitor_invoker)
|
---|
288 | {
|
---|
289 | }
|
---|
290 |
|
---|
291 | //! Invokation operator
|
---|
292 | template< typename ContextT >
|
---|
293 | result_type operator() (ContextT const& ctx)
|
---|
294 | {
|
---|
295 | using namespace boost;
|
---|
296 | using boost::log::binder1st;
|
---|
297 | string_type str;
|
---|
298 | stream_type strm(str);
|
---|
299 | m_visitor_invoker(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args()), binder1st< formatter_function_type&, stream_type& >(m_formatter, strm));
|
---|
300 | strm.flush();
|
---|
301 | return boost::move(str);
|
---|
302 | }
|
---|
303 |
|
---|
304 | //! Invokation operator
|
---|
305 | template< typename ContextT >
|
---|
306 | result_type operator() (ContextT const& ctx) const
|
---|
307 | {
|
---|
308 | using namespace boost;
|
---|
309 | using boost::log::binder1st;
|
---|
310 | string_type str;
|
---|
311 | stream_type strm(str);
|
---|
312 | m_visitor_invoker(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args()), binder1st< formatter_function_type const&, stream_type& >(m_formatter, strm));
|
---|
313 | strm.flush();
|
---|
314 | return boost::move(str);
|
---|
315 | }
|
---|
316 |
|
---|
317 | BOOST_DELETED_FUNCTION(FormatSeverityWithColorsTerminal())
|
---|
318 | };
|
---|
319 |
|
---|
320 | template< typename T, typename FallbackPolicyT, typename CharT = char, template< typename > class ActorT = boost::phoenix::actor >
|
---|
321 | class FormatSeverityWithColorsActor
|
---|
322 | : public ActorT< FormatSeverityWithColorsTerminal< T, FallbackPolicyT, CharT > >
|
---|
323 | {
|
---|
324 | public:
|
---|
325 | //! Attribute value type
|
---|
326 | typedef T value_type;
|
---|
327 | //! Character type
|
---|
328 | typedef CharT char_type;
|
---|
329 | //! Fallback policy
|
---|
330 | typedef FallbackPolicyT fallback_policy;
|
---|
331 | //! Base terminal type
|
---|
332 | typedef FormatSeverityWithColorsTerminal< value_type, fallback_policy, char_type > terminal_type;
|
---|
333 | //! Formatter function
|
---|
334 | typedef typename terminal_type::formatter_function_type formatter_function_type;
|
---|
335 |
|
---|
336 | //! Base actor type
|
---|
337 | typedef ActorT< terminal_type > base_type;
|
---|
338 |
|
---|
339 | public:
|
---|
340 | //! Initializing constructor
|
---|
341 | explicit FormatSeverityWithColorsActor(base_type const& act)
|
---|
342 | : base_type(act)
|
---|
343 | {
|
---|
344 | }
|
---|
345 | };
|
---|
346 |
|
---|
347 | template< typename AttributeValueT >
|
---|
348 | /*BOOST_FORCEINLINE*/ FormatSeverityWithColorsActor< AttributeValueT, boost::log::fallback_to_none >
|
---|
349 | formatSeverityWithColors(boost::log::attribute_name const& name, bool restoreDefault = false)
|
---|
350 | {
|
---|
351 | using boost::log::fallback_to_none;
|
---|
352 | typedef FormatSeverityWithColorsActor< AttributeValueT, fallback_to_none > actor_type;
|
---|
353 | typedef typename actor_type::terminal_type terminal_type;
|
---|
354 | typename actor_type::base_type act = {{ terminal_type(name, fallback_to_none(), restoreDefault) }};
|
---|
355 | return actor_type(act);
|
---|
356 | }
|
---|
357 |
|
---|
358 | template< typename DescriptorT, template< typename > class ActorT, typename CharT >
|
---|
359 | /*BOOST_FORCEINLINE*/ FormatSeverityWithColorsActor< typename DescriptorT::value_type, boost::log::fallback_to_none, CharT, ActorT >
|
---|
360 | formatSeverityWithColors(boost::log::expressions::attribute_keyword< DescriptorT, ActorT > const& keyword, bool restoreDefault = false)
|
---|
361 | {
|
---|
362 | using boost::log::fallback_to_none;
|
---|
363 | typedef FormatSeverityWithColorsActor< typename DescriptorT::value_type, fallback_to_none, CharT, ActorT > actor_type;
|
---|
364 | typedef typename actor_type::terminal_type terminal_type;
|
---|
365 | typename actor_type::base_type act = {{ terminal_type(keyword.get_name(), fallback_to_none(), restoreDefault) }};
|
---|
366 | return actor_type(act);
|
---|
367 | }
|
---|
368 |
|
---|
369 | template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT>
|
---|
370 | /*BOOST_FORCEINLINE*/ FormatSeverityWithColorsActor< T, FallbackPolicyT, char, ActorT >
|
---|
371 | formatSeverityWithColors(boost::log::expressions::attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& placeholder, bool restoreDefault = false)
|
---|
372 | {
|
---|
373 | typedef FormatSeverityWithColorsActor< T, FallbackPolicyT, char, ActorT > actor_type;
|
---|
374 | typedef typename actor_type::terminal_type terminal_type;
|
---|
375 | typename actor_type::base_type act = {{ terminal_type(placeholder.get_name(), placeholder.get_fallback_policy(), restoreDefault) }};
|
---|
376 | return actor_type(act);
|
---|
377 | }
|
---|
378 |
|
---|
379 | } // namespace pacpus
|
---|
380 |
|
---|
381 | #endif // defined(PACPUS_LOG_COLORED_OUTPUT)
|
---|
382 |
|
---|
383 | #endif // COLORSEVERITYFORMATTER_HPP
|
---|