libqi-api  2.1.4.13
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 
13 #ifndef _QI_LOG_HPP_
14 #define _QI_LOG_HPP_
15 
16 # include <string>
17 # include <iostream>
18 # include <sstream>
19 # include <cstdarg>
20 # include <cstdio>
21 
22 #include <boost/format.hpp>
23 #include <boost/function/function_fwd.hpp>
24 
25 #include <qi/os.hpp>
26 
27 
28 # define qiLogCategory(Cat) \
29  static ::qi::log::CategoryType _QI_LOG_CATEGORY_GET() QI_ATTR_UNUSED = \
30  ::qi::log::addCategory(Cat)
31 
32 
33 
34 #if defined(NO_QI_DEBUG) || defined(NDEBUG)
35 # define qiLogDebug(...) ::qi::log::detail::qiFalse() && false < qi::log::detail::NullStream().self()
36 # define qiLogDebugF(Msg, ...)
37 #else
38 # define qiLogDebug(...) _QI_LOG_MESSAGE_STREAM(LogLevel_Debug, Debug , __VA_ARGS__)
39 # define qiLogDebugF(Msg, ...) _QI_LOG_MESSAGE(LogLevel_Debug, _QI_LOG_FORMAT(Msg, __VA_ARGS__))
40 #endif
41 
42 #if defined(NO_QI_VERBOSE)
43 # define qiLogVerbose(...) ::qi::log::detail::qiFalse() && false < qi::log::detail::NullStream().self()
44 # define qiLogVerboseF(Msg, ...)
45 #else
46 # define qiLogVerbose(...) _QI_LOG_MESSAGE_STREAM(LogLevel_Verbose, Verbose, __VA_ARGS__)
47 # define qiLogVerboseF(Msg, ...) _QI_LOG_MESSAGE(LogLevel_Verbose, _QI_LOG_FORMAT(Msg, __VA_ARGS__))
48 #endif
49 
50 #if defined(NO_QI_INFO)
51 # define qiLogInfo(...) ::qi::log::detail::qiFalse() && false < qi::log::detail::NullStream().self()
52 # define qiLogInfoF(Msg, ...)
53 #else
54 # define qiLogInfo(...) _QI_LOG_MESSAGE_STREAM(LogLevel_Info, Info, __VA_ARGS__)
55 # define qiLogInfoF(Msg, ...) _QI_LOG_MESSAGE(LogLevel_Info, _QI_LOG_FORMAT(Msg, __VA_ARGS__))
56 #endif
57 
58 #if defined(NO_QI_WARNING)
59 # define qiLogWarning(...) ::qi::log::detail::qiFalse() && false < qi::log::detail::NullStream().self()
60 # define qiLogWarningF(Msg, ...)
61 #else
62 # define qiLogWarning(...) _QI_LOG_MESSAGE_STREAM(LogLevel_Warning, Warning, __VA_ARGS__)
63 # define qiLogWarningF(Msg, ...) _QI_LOG_MESSAGE(LogLevel_Warning, _QI_LOG_FORMAT(Msg, __VA_ARGS__))
64 #endif
65 
66 #if defined(NO_QI_ERROR)
67 # define qiLogError(...) ::qi::log::detail::qiFalse() && false < qi::log::detail::NullStream().self()
68 # define qiLogErrorF(Msg, ...)
69 #else
70 # define qiLogError(...) _QI_LOG_MESSAGE_STREAM(LogLevel_Error, Error, __VA_ARGS__)
71 # define qiLogErrorF(Msg, ...) _QI_LOG_MESSAGE(LogLevel_Error, _QI_LOG_FORMAT(Msg, __VA_ARGS__))
72 #endif
73 
74 #if defined(NO_QI_FATAL)
75 # define qiLogFatal(...) ::qi::log::detail::qiFalse() && false < qi::log::detail::NullStream().self()
76 # define qiLogFatalF(Msg, ...)
77 #else
78 # define qiLogFatal(...) _QI_LOG_MESSAGE_STREAM(LogLevel_Fatal, Fatal, __VA_ARGS__)
79 # define qiLogFatalF(Msg, ...) _QI_LOG_MESSAGE(LogLevel_Fatal, _QI_LOG_FORMAT(Msg, __VA_ARGS__))
80 #endif
81 
82 namespace qi {
83 
84  enum LogLevel {
92  };
93 
94  enum LogColor {
98  };
99 
110  };
111 
112  typedef int LogContext;
113 
114  namespace log {
115 
116  //deprecated 1.22
117  QI_API_DEPRECATED static const qi::LogLevel silent = LogLevel_Silent;
118  QI_API_DEPRECATED static const qi::LogLevel fatal = LogLevel_Fatal;
119  QI_API_DEPRECATED static const qi::LogLevel error = LogLevel_Error;
120  QI_API_DEPRECATED static const qi::LogLevel warning = LogLevel_Warning;
121  QI_API_DEPRECATED static const qi::LogLevel info = LogLevel_Info;
122  QI_API_DEPRECATED static const qi::LogLevel verbose = LogLevel_Verbose;
123  QI_API_DEPRECATED static const qi::LogLevel debug = LogLevel_Debug;
124 
125  //deprecated 1.22
127  }
128 }
129 
130 namespace qi {
131  namespace log {
132  namespace detail {
133  struct Category;
134  }
135 
136  typedef unsigned int SubscriberId;
137  typedef detail::Category* CategoryType;
138 
139  //Deprecated 1.22
140  QI_API_DEPRECATED typedef unsigned int Subscriber;
141 
142  typedef boost::function7<void,
143  const qi::LogLevel,
144  const qi::os::timeval,
145  const char*,
146  const char*,
147  const char*,
148  const char*,
150 
153  bool synchronous = true);
154 
155  QI_API void destroy();
156 
157  QI_API void log(const qi::LogLevel verb,
158  const char* category,
159  const char* msg,
160  const char* file = "",
161  const char* fct = "",
162  const int line = 0);
163 
164  QI_API void log(const qi::LogLevel verb,
165  CategoryType category,
166  const std::string& msg,
167  const char* file = "",
168  const char* fct = "",
169  const int line = 0);
170 
171 
172  QI_API const char* logLevelToString(const qi::LogLevel verb, bool verbose = true);
173 
174  QI_API qi::LogLevel stringToLogLevel(const char* verb);
175 
177 
179  QI_API std::vector<std::string> categories();
180 
190  QI_API void setVerbosity(const std::string& rules, SubscriberId sub = 0);
191  QI_API void setVerbosity(const qi::LogLevel lv, SubscriberId sub = 0);
192 
193 
195  QI_API CategoryType addCategory(const std::string& name);
197  QI_API void enableCategory(const std::string& cat, SubscriberId sub = 0);
199  QI_API void disableCategory(const std::string& cat, SubscriberId sub = 0);
201  QI_API void setCategory(const std::string& cat, qi::LogLevel level, SubscriberId sub = 0);
202 
203 
205  QI_API bool isVisible(CategoryType category, qi::LogLevel level);
207  QI_API bool isVisible(const std::string& category, qi::LogLevel level);
208 
209  QI_API void setContext(int ctx);
210 
211  QI_API int context();
212 
214 
216 
222  QI_API void setSynchronousLog(bool sync);
223 
224  QI_API SubscriberId addLogHandler(const std::string& name,
226  qi::LogLevel defaultLevel = LogLevel_Info);
227 
228  QI_API void removeLogHandler(const std::string& name);
229 
230  QI_API void flush();
231 
232 
234  // Deprecated 1.22
236  // Deprecated 1.22
237  QI_API_DEPRECATED inline void setCategory(SubscriberId sub, const std::string& cat, qi::log::LogLevel level) { setCategory(cat, (qi::LogLevel)level, sub); }
239 
240  }
241 }
242 
243 #include <qi/details/log.hxx>
244 
245 
246 #endif // _QI_LOG_HPP_
fatal log level
Definition: log.hpp:86
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.
LogColor color()
int context()
Get log context.
void removeLogHandler(const std::string &name)
Remove a log handler.
std::vector< std::string > categories()
void setCategory(const std::string &cat, qi::LogLevel level, SubscriberId sub=0)
Set per-subscriber.
CategoryType addCategory(const std::string &name)
Add/get a category.
void init(qi::LogLevel verb=qi::LogLevel_Info, qi::LogContext context=qi::LogContextAttr_ShortVerbosity|qi::LogContextAttr_Tid|qi::LogContextAttr_Category, bool synchronous=true)
init the logging system (could be avoided)
void setVerbosity(const std::string &rules, SubscriberId sub=0)
void setSynchronousLog(bool sync)
Enables or disables synchronous logs.
void destroy()
Stop and flush the logging system.
QI_API_DEPRECATED typedef unsigned int Subscriber
Definition: log.hpp:140
bool isVisible(CategoryType category, qi::LogLevel level)
silent log level
Definition: log.hpp:85
LogContextAttr
Definition: log.hpp:100
void log(const qi::LogLevel verb, CategoryType category, const std::string &msg, const char *file="", const char *fct="", const int line=0)
detail::Category * CategoryType
Definition: log.hxx:210
error log level
Definition: log.hpp:87
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:149
unsigned int SubscriberId
Definition: log.hpp:136
verbose log level
Definition: log.hpp:90
const char * logLevelToString(const qi::LogLevel verb, bool verbose=true)
#define QI_API_DEPRECATED
Compiler flags to mark a function as deprecated. It will generate a compiler warning.
Definition: macro.hpp:25
SubscriberId addLogHandler(const std::string &name, qi::log::logFuncHandler fct, qi::LogLevel defaultLevel=LogLevel_Info)
void setContext(int ctx)
Set log context verbosity.
warning log level
Definition: log.hpp:88
qi::LogLevel verbosity(SubscriberId sub=0)
qi::LogLevel stringToLogLevel(const char *verb)
Convert string to log verbosity.
LogLevel
Log level verbosity.
Definition: log.hpp:84
void flush()
Flush asynchronous logs.
LogColor
Definition: log.hpp:94
void disableCategory(const std::string &cat, SubscriberId sub=0)
Set.
info log level
Definition: log.hpp:89
void enableCategory(const std::string &cat, SubscriberId sub=0)
Set.
QI_API_DEPRECATED typedef qi::LogLevel LogLevel
Definition: log.hpp:126
int LogContext
Definition: log.hpp:112
void setColor(LogColor color)
#define QI_API
Definition: api.hpp:24
debug log level
Definition: log.hpp:91