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