libqi-api  release-2.5.3-2016-11-18
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
log.hpp
Go to the documentation of this file.
1 #pragma once
2 /*
3  * Copyright (c) 2012 Aldebaran Robotics. All rights reserved.
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the COPYING file.
6  */
7 
15 #ifndef _QI_LOG_HPP_
16 # define _QI_LOG_HPP_
17 
18 # include <string>
19 # include <sstream>
20 # include <cstdarg>
21 # include <cstdio>
22 
23 # include <boost/format.hpp>
24 # include <boost/function/function_fwd.hpp>
25 
26 # include <qi/os.hpp>
27 
28 
45 #define qiLogCategory(Cat) \
46  static ::qi::log::CategoryType _QI_LOG_CATEGORY_GET() QI_ATTR_UNUSED = \
47  ::qi::log::addCategory(Cat)
48 
49 
64 #if defined(NO_QI_DEBUG) || defined(NDEBUG)
65 # define qiLogDebug(...) ::qi::log::detail::qiFalse() && false < qi::log::detail::NullStream().self()
66 # define qiLogDebugF(Msg, ...)
67 #else
68 # define qiLogDebug(...) _QI_LOG_MESSAGE_STREAM(LogLevel_Debug, Debug , __VA_ARGS__)
69 # define qiLogDebugF(Msg, ...) _QI_LOG_MESSAGE(LogLevel_Debug, _QI_LOG_FORMAT(Msg, __VA_ARGS__))
70 #endif
71 
75 #if defined(NO_QI_VERBOSE)
76 # define qiLogVerbose(...) ::qi::log::detail::qiFalse() && false < qi::log::detail::NullStream().self()
77 # define qiLogVerboseF(Msg, ...)
78 #else
79 # define qiLogVerbose(...) _QI_LOG_MESSAGE_STREAM(LogLevel_Verbose, Verbose, __VA_ARGS__)
80 # define qiLogVerboseF(Msg, ...) _QI_LOG_MESSAGE(LogLevel_Verbose, _QI_LOG_FORMAT(Msg, __VA_ARGS__))
81 #endif
82 
86 #if defined(NO_QI_INFO)
87 # define qiLogInfo(...) ::qi::log::detail::qiFalse() && false < qi::log::detail::NullStream().self()
88 # define qiLogInfoF(Msg, ...)
89 #else
90 # define qiLogInfo(...) _QI_LOG_MESSAGE_STREAM(LogLevel_Info, Info, __VA_ARGS__)
91 # define qiLogInfoF(Msg, ...) _QI_LOG_MESSAGE(LogLevel_Info, _QI_LOG_FORMAT(Msg, __VA_ARGS__))
92 #endif
93 
97 #if defined(NO_QI_WARNING)
98 # define qiLogWarning(...) ::qi::log::detail::qiFalse() && false < qi::log::detail::NullStream().self()
99 # define qiLogWarningF(Msg, ...)
100 #else
101 # define qiLogWarning(...) _QI_LOG_MESSAGE_STREAM(LogLevel_Warning, Warning, __VA_ARGS__)
102 # define qiLogWarningF(Msg, ...) _QI_LOG_MESSAGE(LogLevel_Warning, _QI_LOG_FORMAT(Msg, __VA_ARGS__))
103 #endif
104 
108 #if defined(NO_QI_ERROR)
109 # define qiLogError(...) ::qi::log::detail::qiFalse() && false < qi::log::detail::NullStream().self()
110 # define qiLogErrorF(Msg, ...)
111 #else
112 # define qiLogError(...) _QI_LOG_MESSAGE_STREAM(LogLevel_Error, Error, __VA_ARGS__)
113 # define qiLogErrorF(Msg, ...) _QI_LOG_MESSAGE(LogLevel_Error, _QI_LOG_FORMAT(Msg, __VA_ARGS__))
114 #endif
115 
119 #if defined(NO_QI_FATAL)
120 # define qiLogFatal(...) ::qi::log::detail::qiFalse() && false < qi::log::detail::NullStream().self()
121 # define qiLogFatalF(Msg, ...)
122 #else
123 # define qiLogFatal(...) _QI_LOG_MESSAGE_STREAM(LogLevel_Fatal, Fatal, __VA_ARGS__)
124 # define qiLogFatalF(Msg, ...) _QI_LOG_MESSAGE(LogLevel_Fatal, _QI_LOG_FORMAT(Msg, __VA_ARGS__))
125 #endif
126 
127 
128 namespace qi {
132  enum LogLevel {
140  };
141 
145  enum LogColor {
149  };
150 
165  };
166 
170  using LogContext = int;
171 
175  namespace log {
176 
178  QI_API_DEPRECATED_MSG(Use 'LogLevel_Silent' instead) static const qi::LogLevel silent = LogLevel_Silent;
180  QI_API_DEPRECATED_MSG(Use 'LogLevel_Fatal' instead) static const qi::LogLevel fatal = LogLevel_Fatal;
182  QI_API_DEPRECATED_MSG(Use 'LogLevel_Error' instead) static const qi::LogLevel error = LogLevel_Error;
184  QI_API_DEPRECATED_MSG(Use 'LogLevel_Warning' instead) static const qi::LogLevel warning = LogLevel_Warning;
186  QI_API_DEPRECATED_MSG(Use 'LogLevel_Info' instead) static const qi::LogLevel info = LogLevel_Info;
188  QI_API_DEPRECATED_MSG(Use 'LogLevel_Verbose' instead) static const qi::LogLevel verbose = LogLevel_Verbose;
190  QI_API_DEPRECATED_MSG(Use 'LogLevel_Debug' instead) static const qi::LogLevel debug = LogLevel_Debug;
191 
193  QI_API_DEPRECATED_MSG('qi::log::LogLevel' is deprecated. Use 'qi::LogLevel' instead) typedef qi::LogLevel LogLevel;
194  }
195 }
196 
197 namespace qi {
198  namespace log {
199  namespace detail {
200  struct Category;
201  }
202 
203  using SubscriberId = unsigned int;
204  using CategoryType = detail::Category*;
205 
207  QI_API_DEPRECATED_MSG(Use 'SubscriberId' instead)
208  typedef unsigned int Subscriber;
209 
215  using logFuncHandler = boost::function7<void,
216  const qi::LogLevel,
217  const qi::os::timeval,
218  const char*,
219  const char*,
220  const char*,
221  const char*,
222  int>;
227  using Handler = boost::function8<void,
228  const qi::LogLevel,
229  const qi::Clock::time_point,
230  const qi::SystemClock::time_point,
231  const char*,
232  const char*,
233  const char*,
234  const char*,
235  int>;
236 
243  QI_API void init(qi::LogLevel verb = qi::LogLevel_Info,
245  bool synchronous = true);
246 
247 
261  QI_API void destroy();
262 
263 
274  QI_API void log(const qi::LogLevel verb,
275  const char* category,
276  const char* msg,
277  const char* file = "",
278  const char* fct = "",
279  const int line = 0);
280 
284  QI_API void log(const qi::LogLevel verb,
285  CategoryType category,
286  const std::string& msg,
287  const char* file = "",
288  const char* fct = "",
289  const int line = 0);
290 
291 
298  QI_API const char* logLevelToString(const qi::LogLevel verb, bool verbose = true);
299 
305  QI_API qi::LogLevel stringToLogLevel(const char* verb);
306 
328  QI_API void setLogLevel(const qi::LogLevel lv, SubscriberId sub = 0);
329 
335  QI_API qi::LogLevel logLevel(SubscriberId sub = 0);
336 
341  QI_API std::vector<std::string> categories();
342 
348  QI_API CategoryType addCategory(const std::string& name);
354  QI_API void enableCategory(const std::string& cat, SubscriberId sub = 0);
360  QI_API void disableCategory(const std::string& cat, SubscriberId sub = 0);
361 
368  inline bool isVisible(CategoryType category, qi::LogLevel level);
369 
373  QI_API bool isVisible(const std::string& category, qi::LogLevel level);
374 
389  QI_API void addFilters(const std::string& rules, SubscriberId sub = 0);
390 
408  QI_API void addFilter(const std::string& cat, qi::LogLevel level, SubscriberId sub = 0);
409 
434  QI_API void setContext(int ctx);
435 
440  QI_API int context();
441 
447 
452  QI_API LogColor color();
453 
461  QI_API void setSynchronousLog(bool sync);
462 
471  QI_API SubscriberId addHandler(const std::string& name,
472  qi::log::Handler fct,
473  qi::LogLevel defaultLevel = LogLevel_Info);
482  QI_API_DEPRECATED_MSG(Use 'addHandler' instead)
483  QI_API SubscriberId addLogHandler(const std::string& name,
484  qi::log::logFuncHandler fct,
485  qi::LogLevel defaultLevel = LogLevel_Info);
486 
491  QI_API void removeHandler(const std::string& name);
492 
498  QI_API_DEPRECATED_MSG(Use 'removeHandler' instead)
499  QI_API void removeLogHandler(const std::string& name);
500 
504  QI_API void flush();
505 
506 
507  #include <qi/detail/warn_push_ignore_deprecated.hpp>
508  QI_API_DEPRECATED_MSG(Use 'setLogLevel' instead)
510  inline void setVerbosity(SubscriberId sub, const qi::log::LogLevel lv) { setLogLevel((qi::LogLevel)lv, sub); }
512  QI_API_DEPRECATED_MSG(Use 'addFilter' instead)
513  inline void setCategory(SubscriberId sub, const std::string& cat, qi::log::LogLevel level) { addFilter(cat, (qi::LogLevel)level, sub); }
515 
520  QI_API QI_API_DEPRECATED_MSG(Use 'logLevel' instead)
521  qi::LogLevel verbosity(SubscriberId sub = 0);
522 
527  QI_API QI_API_DEPRECATED_MSG(Use 'addFilters' instead)
528  void setVerbosity(const std::string& rules, SubscriberId sub = 0);
529 
534  QI_API QI_API_DEPRECATED_MSG(Use 'setLogLevel' instead)
535  void setVerbosity(const qi::LogLevel lv, SubscriberId sub = 0);
536 
541  QI_API QI_API_DEPRECATED_MSG(Use 'addFilter' instead)
542  void setCategory(const std::string& catName, qi::LogLevel level, SubscriberId sub = 0);
543 
544  }
545 }
546 
547 # include <qi/detail/log.hxx>
548 
549 
550 #endif // _QI_LOG_HPP_
error log level
Definition: log.hpp:135
Show logs level.
Definition: log.hpp:156
void destroy()
Stop and flush the logging system.
LogColor
Logs color mode.
Definition: log.hpp:145
Show qi::Clock dates.
Definition: log.hpp:164
Show categories.
Definition: log.hpp:160
std::vector< std::string > categories()
Get the list of all categories.
SubscriberId addHandler(const std::string &name, qi::log::Handler fct, qi::LogLevel defaultLevel=LogLevel_Info)
Add a log handler for this process' logs.
Print an end line between contexts and logs.
Definition: log.hpp:163
Never show color.
Definition: log.hpp:146
void setContext(int ctx)
Set log context verbosity.
void setVerbosity(SubscriberId sub, const qi::log::LogLevel lv)
Definition: log.hpp:510
CategoryType addCategory(const std::string &name)
Add/get a category.
silent log level
Definition: log.hpp:133
#define QI_API
Definition: api.hpp:33
Always show color.
Definition: log.hpp:148
LogLevel
Log level verbosity.
Definition: log.hpp:132
The SystemClock class represents the system-wide real time wall clock. It may not be monotonic: on mo...
Definition: clock.hpp:167
debug log level
Definition: log.hpp:139
qi::LogLevel logLevel(SubscriberId sub=0)
Get log verbosity.
SubscriberId addLogHandler(const std::string &name, qi::log::logFuncHandler fct, qi::LogLevel defaultLevel=LogLevel_Info)
Add a log handler.
unsigned int SubscriberId
Subscriber Identifier.
Definition: log.hpp:203
void disableCategory(const std::string &cat, SubscriberId sub=0)
Set category to silent log level. Globbing is supported.
void setColor(LogColor color)
Set log color.
Show functions name.
Definition: log.hpp:162
void addFilter(const std::string &cat, qi::LogLevel level, SubscriberId sub=0)
Set per-subscriber category to level. Globbing is supported.
const char * logLevelToString(const qi::LogLevel verb, bool verbose=true)
Convert log verbosity to a readable string.
void addFilters(const std::string &rules, SubscriberId sub=0)
Parse and execute a set of verbosity rules.
void log(const qi::LogLevel verb, const char *category, const char *msg, const char *file="", const char *fct="", const int line=0)
Log function. You should call qiLog* macros instead.
void setSynchronousLog(bool sync)
Enables or disables synchronous logs.
qi::LogLevel stringToLogLevel(const char *verb)
Convert string to log verbosity.
void log(const qi::LogLevel verb, CategoryType category, const std::string &msg, const char *file="", const char *fct="", const int line=0)
Log function. You should call qiLog* macros instead.
void setLogLevel(const qi::LogLevel lv, SubscriberId sub=0)
Set log Level.
#define QI_API_DEPRECATED_MSG(msg__)
Compiler flags to mark a function as deprecated. It will generate a compiler warning.
Definition: macro.hpp:53
Auto color.
Definition: log.hpp:147
No context.
Definition: log.hpp:155
bool isVisible(CategoryType category, qi::LogLevel level)
Check if the given combination of category and level is enable.
Definition: log.hxx:218
Show logs files.
Definition: log.hpp:161
verbose log level
Definition: log.hpp:138
boost::function8< void, const qi::LogLevel, const qi::Clock::time_point, const qi::SystemClock::time_point, const char *, const char *, const char *, const char *, int > Handler
Boost delegate to log function (verbosity lv, dates of log, category, message, file, function, line).
Definition: log.hpp:235
unsigned int Subscriber
Definition: log.hpp:208
warning log level
Definition: log.hpp:136
void removeHandler(const std::string &name)
Remove a log handler.
Show qi::SystemClock dates.
Definition: log.hpp:158
Show short logs level.
Definition: log.hpp:157
info log level
Definition: log.hpp:137
fatal log level
Definition: log.hpp:134
Show threads id.
Definition: log.hpp:159
LogColor color()
Get log color.
int LogContext
Logs context attribute value.
Definition: log.hpp:170
The Clock class represents a system-wide clock, best suitable for timestamping events. Typically monotonic and unaffected by the system clock adjustment, altough this is not guaranteed.
Definition: clock.hpp:85
void enableCategory(const std::string &cat, SubscriberId sub=0)
Set category to current verbosity level. Globbing is supported.
LogContextAttr
Logs context attribute.
Definition: log.hpp:154
void flush()
Flush asynchronous logs.
void init(qi::LogLevel verb=qi::LogLevel_Info, qi::LogContext context=qi::LogContextAttr_ShortVerbosity|qi::LogContextAttr_Tid|qi::LogContextAttr_Category, bool synchronous=true)
Initialization of the logging system (could be avoided)
void setCategory(SubscriberId sub, const std::string &cat, qi::log::LogLevel level)
Definition: log.hpp:513
boost::function7< void, const qi::LogLevel, const qi::os::timeval, const char *, const char *, const char *, const char *, int > logFuncHandler
Boost delegate to log function (verbosity lv, date of log, category, message, file, function, line).
Definition: log.hpp:222
int context()
Get log context.
void removeLogHandler(const std::string &name)
Remove a log handler.