libqi-api  2.4.3.28
 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 <iostream>
20 # include <sstream>
21 # include <cstdarg>
22 # include <cstdio>
23 
24 # include <boost/format.hpp>
25 # include <boost/function/function_fwd.hpp>
26 
27 # include <qi/os.hpp>
28 
29 
46 #define qiLogCategory(Cat) \
47  static ::qi::log::CategoryType _QI_LOG_CATEGORY_GET() QI_ATTR_UNUSED = \
48  ::qi::log::addCategory(Cat)
49 
50 
65 #if defined(NO_QI_DEBUG) || defined(NDEBUG)
66 # define qiLogDebug(...) ::qi::log::detail::qiFalse() && false < qi::log::detail::NullStream().self()
67 # define qiLogDebugF(Msg, ...)
68 #else
69 # define qiLogDebug(...) _QI_LOG_MESSAGE_STREAM(LogLevel_Debug, Debug , __VA_ARGS__)
70 # define qiLogDebugF(Msg, ...) _QI_LOG_MESSAGE(LogLevel_Debug, _QI_LOG_FORMAT(Msg, __VA_ARGS__))
71 #endif
72 
76 #if defined(NO_QI_VERBOSE)
77 # define qiLogVerbose(...) ::qi::log::detail::qiFalse() && false < qi::log::detail::NullStream().self()
78 # define qiLogVerboseF(Msg, ...)
79 #else
80 # define qiLogVerbose(...) _QI_LOG_MESSAGE_STREAM(LogLevel_Verbose, Verbose, __VA_ARGS__)
81 # define qiLogVerboseF(Msg, ...) _QI_LOG_MESSAGE(LogLevel_Verbose, _QI_LOG_FORMAT(Msg, __VA_ARGS__))
82 #endif
83 
87 #if defined(NO_QI_INFO)
88 # define qiLogInfo(...) ::qi::log::detail::qiFalse() && false < qi::log::detail::NullStream().self()
89 # define qiLogInfoF(Msg, ...)
90 #else
91 # define qiLogInfo(...) _QI_LOG_MESSAGE_STREAM(LogLevel_Info, Info, __VA_ARGS__)
92 # define qiLogInfoF(Msg, ...) _QI_LOG_MESSAGE(LogLevel_Info, _QI_LOG_FORMAT(Msg, __VA_ARGS__))
93 #endif
94 
98 #if defined(NO_QI_WARNING)
99 # define qiLogWarning(...) ::qi::log::detail::qiFalse() && false < qi::log::detail::NullStream().self()
100 # define qiLogWarningF(Msg, ...)
101 #else
102 # define qiLogWarning(...) _QI_LOG_MESSAGE_STREAM(LogLevel_Warning, Warning, __VA_ARGS__)
103 # define qiLogWarningF(Msg, ...) _QI_LOG_MESSAGE(LogLevel_Warning, _QI_LOG_FORMAT(Msg, __VA_ARGS__))
104 #endif
105 
109 #if defined(NO_QI_ERROR)
110 # define qiLogError(...) ::qi::log::detail::qiFalse() && false < qi::log::detail::NullStream().self()
111 # define qiLogErrorF(Msg, ...)
112 #else
113 # define qiLogError(...) _QI_LOG_MESSAGE_STREAM(LogLevel_Error, Error, __VA_ARGS__)
114 # define qiLogErrorF(Msg, ...) _QI_LOG_MESSAGE(LogLevel_Error, _QI_LOG_FORMAT(Msg, __VA_ARGS__))
115 #endif
116 
120 #if defined(NO_QI_FATAL)
121 # define qiLogFatal(...) ::qi::log::detail::qiFalse() && false < qi::log::detail::NullStream().self()
122 # define qiLogFatalF(Msg, ...)
123 #else
124 # define qiLogFatal(...) _QI_LOG_MESSAGE_STREAM(LogLevel_Fatal, Fatal, __VA_ARGS__)
125 # define qiLogFatalF(Msg, ...) _QI_LOG_MESSAGE(LogLevel_Fatal, _QI_LOG_FORMAT(Msg, __VA_ARGS__))
126 #endif
127 
128 
129 namespace qi {
133  enum LogLevel {
141  };
142 
146  enum LogColor {
150  };
151 
166  };
167 
171  typedef int LogContext;
172 
176  namespace log {
177 
179  QI_API_DEPRECATED static const qi::LogLevel silent = LogLevel_Silent;
181  QI_API_DEPRECATED static const qi::LogLevel fatal = LogLevel_Fatal;
183  QI_API_DEPRECATED static const qi::LogLevel error = LogLevel_Error;
185  QI_API_DEPRECATED static const qi::LogLevel warning = LogLevel_Warning;
187  QI_API_DEPRECATED static const qi::LogLevel info = LogLevel_Info;
189  QI_API_DEPRECATED static const qi::LogLevel verbose = LogLevel_Verbose;
191  QI_API_DEPRECATED static const qi::LogLevel debug = LogLevel_Debug;
192 
195  }
196 }
197 
198 namespace qi {
199  namespace log {
200  namespace detail {
201  struct Category;
202  }
203 
204  typedef unsigned int SubscriberId;
205  typedef detail::Category* CategoryType;
206 
208  QI_API_DEPRECATED typedef unsigned int Subscriber;
209 
210 
216  typedef boost::function7<void,
217  const qi::LogLevel,
218  const qi::os::timeval,
219  const char*,
220  const char*,
221  const char*,
222  const char*,
228  typedef boost::function8<void,
229  const qi::LogLevel,
230  const qi::Clock::time_point,
232  const char*,
233  const char*,
234  const char*,
235  const char*,
236  int> Handler;
237 
246  bool synchronous = true);
247 
248 
262  QI_API void destroy();
263 
264 
275  QI_API void log(const qi::LogLevel verb,
276  const char* category,
277  const char* msg,
278  const char* file = "",
279  const char* fct = "",
280  const int line = 0);
281 
285  QI_API void log(const qi::LogLevel verb,
286  CategoryType category,
287  const std::string& msg,
288  const char* file = "",
289  const char* fct = "",
290  const int line = 0);
291 
292 
299  QI_API const char* logLevelToString(const qi::LogLevel verb, bool verbose = true);
300 
306  QI_API qi::LogLevel stringToLogLevel(const char* verb);
307 
329  QI_API void setLogLevel(const qi::LogLevel lv, SubscriberId sub = 0);
330 
337 
342  QI_API std::vector<std::string> categories();
343 
349  QI_API CategoryType addCategory(const std::string& name);
355  QI_API void enableCategory(const std::string& cat, SubscriberId sub = 0);
361  QI_API void disableCategory(const std::string& cat, SubscriberId sub = 0);
362 
369  inline bool isVisible(CategoryType category, qi::LogLevel level);
370 
374  QI_API bool isVisible(const std::string& category, qi::LogLevel level);
375 
390  QI_API void addFilters(const std::string& rules, SubscriberId sub = 0);
391 
409  QI_API void addFilter(const std::string& cat, qi::LogLevel level, SubscriberId sub = 0);
410 
435  QI_API void setContext(int ctx);
436 
441  QI_API int context();
442 
448 
454 
462  QI_API void setSynchronousLog(bool sync);
463 
471  QI_API SubscriberId addHandler(const std::string& name,
472  qi::log::Handler fct,
473  qi::LogLevel defaultLevel = LogLevel_Info);
483  QI_API SubscriberId addLogHandler(const std::string& name,
485  qi::LogLevel defaultLevel = LogLevel_Info);
486 
491  QI_API void removeHandler(const std::string& name);
492 
499  QI_API void removeLogHandler(const std::string& name);
500 
504  QI_API void flush();
505 
506 
511  QI_API_DEPRECATED inline void setCategory(SubscriberId sub, const std::string& cat, qi::log::LogLevel level) { addFilter(cat, (qi::LogLevel)level, sub); }
513 
519 
524  QI_API QI_API_DEPRECATED void setVerbosity(const std::string& rules, SubscriberId sub = 0);
525 
531 
536  QI_API QI_API_DEPRECATED void setCategory(const std::string& catName, qi::LogLevel level, SubscriberId sub = 0);
537 
538  }
539 }
540 
541 # include <qi/detail/log.hxx>
542 
543 
544 #endif // _QI_LOG_HPP_
error log level
Definition: log.hpp:136
Show logs level.
Definition: log.hpp:157
void destroy()
Stop and flush the logging system.
LogColor
Logs color mode.
Definition: log.hpp:146
Show qi::Clock dates.
Definition: log.hpp:165
Show categories.
Definition: log.hpp:161
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.
Print an end line between contexts and logs.
Definition: log.hpp:164
Never show color.
Definition: log.hpp:147
void setContext(int ctx)
Set log context verbosity.
int LogContext
Logs context attribute value.
Definition: log.hpp:171
CategoryType addCategory(const std::string &name)
Add/get a category.
silent log level
Definition: log.hpp:134
#define QI_API
Definition: api.hpp:34
Always show color.
Definition: log.hpp:149
LogLevel
Log level verbosity.
Definition: log.hpp:133
QI_API_DEPRECATED void removeLogHandler(const std::string &name)
Remove a log handler.
QI_API_DEPRECATED typedef unsigned int Subscriber
Definition: log.hpp:208
debug log level
Definition: log.hpp:140
boost::chrono::time_point< Clock > time_point
Definition: clock.hpp:87
QI_API_DEPRECATED SubscriberId addLogHandler(const std::string &name, qi::log::logFuncHandler fct, qi::LogLevel defaultLevel=LogLevel_Info)
Add a log handler.
qi::LogLevel logLevel(SubscriberId sub=0)
Get log verbosity.
QI_API_DEPRECATED qi::LogLevel verbosity(SubscriberId sub=0)
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:236
void disableCategory(const std::string &cat, SubscriberId sub=0)
Set category to silent log level. Globbing is supported.
QI_API_DEPRECATED typedef qi::LogLevel LogLevel
Definition: log.hpp:194
QI_API_DEPRECATED void setCategory(SubscriberId sub, const std::string &cat, qi::log::LogLevel level)
Definition: log.hpp:511
void setColor(LogColor color)
Set log color.
Show functions name.
Definition: log.hpp:163
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.
unsigned int SubscriberId
Subscriber Identifier.
Definition: log.hpp:204
Auto color.
Definition: log.hpp:148
detail::Category * CategoryType
Catergory Informations.
Definition: log.hxx:210
No context.
Definition: log.hpp:156
struct similar to POSIX timeval
Definition: os.hpp:240
bool isVisible(CategoryType category, qi::LogLevel level)
Check if the given combination of category and level is enable.
Definition: log.hxx:205
Show logs files.
Definition: log.hpp:162
verbose log level
Definition: log.hpp:139
warning log level
Definition: log.hpp:137
void removeHandler(const std::string &name)
Remove a log handler.
Show qi::SystemClock dates.
Definition: log.hpp:159
Show short logs level.
Definition: log.hpp:158
info log level
Definition: log.hpp:138
fatal log level
Definition: log.hpp:135
#define QI_API_DEPRECATED
Compiler flags to mark a function as deprecated. It will generate a compiler warning.
Definition: macro.hpp:39
Show threads id.
Definition: log.hpp:160
boost::chrono::time_point< SystemClock > time_point
Definition: clock.hpp:169
LogColor color()
Get log color.
QI_API_DEPRECATED void setVerbosity(SubscriberId sub, const qi::log::LogLevel lv)
Definition: log.hpp:509
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:155
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)
int context()
Get log context.
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:223