libqi-api  2.0.6.8
/home/opennao/work/master/sdk/libqi/qi/log.hpp
Go to the documentation of this file.
00001 #pragma once
00002 /*
00003  * Copyright (c) 2012 Aldebaran Robotics. All rights reserved.
00004  * Use of this source code is governed by a BSD-style license that can be
00005  * found in the COPYING file.
00006  */
00007 
00013 #ifndef _QI_LOG_HPP_
00014 #define _QI_LOG_HPP_
00015 
00016 # include <string>
00017 # include <iostream>
00018 # include <sstream>
00019 # include <cstdarg>
00020 # include <cstdio>
00021 
00022 #include <boost/format.hpp>
00023 #include <boost/function/function_fwd.hpp>
00024 
00025 #include <qi/os.hpp>
00026 
00027 
00028 #  define qiLogCategory(Cat)                                             \
00029   static ::qi::log::CategoryType _QI_LOG_CATEGORY_GET() QI_ATTR_UNUSED = \
00030     ::qi::log::addCategory(Cat)
00031 
00032 
00033 
00034 #if defined(NO_QI_DEBUG) || defined(NDEBUG)
00035 # define qiLogDebug(...) ::qi::log::detail::qiFalse() && false < qi::log::detail::NullStream().self()
00036 # define qiLogDebugF(Msg, ...)
00037 #else
00038 # define qiLogDebug(...)   _QI_LOG_MESSAGE_STREAM(LogLevel_Debug,   Debug ,  __VA_ARGS__)
00039 # define qiLogDebugF(Msg, ...)   _QI_LOG_MESSAGE(LogLevel_Debug,   _QI_LOG_FORMAT(Msg, __VA_ARGS__))
00040 #endif
00041 
00042 #if defined(NO_QI_VERBOSE)
00043 # define qiLogVerbose(...) ::qi::log::detail::qiFalse() && false < qi::log::detail::NullStream().self()
00044 # define qiLogVerboseF(Msg, ...)
00045 #else
00046 # define qiLogVerbose(...) _QI_LOG_MESSAGE_STREAM(LogLevel_Verbose, Verbose, __VA_ARGS__)
00047 # define qiLogVerboseF(Msg, ...)   _QI_LOG_MESSAGE(LogLevel_Verbose,   _QI_LOG_FORMAT(Msg, __VA_ARGS__))
00048 #endif
00049 
00050 #if defined(NO_QI_INFO)
00051 # define qiLogInfo(...) ::qi::log::detail::qiFalse() && false < qi::log::detail::NullStream().self()
00052 # define qiLogInfoF(Msg, ...)
00053 #else
00054 # define qiLogInfo(...)    _QI_LOG_MESSAGE_STREAM(LogLevel_Info,    Info,    __VA_ARGS__)
00055 # define qiLogInfoF(Msg, ...)   _QI_LOG_MESSAGE(LogLevel_Info,   _QI_LOG_FORMAT(Msg, __VA_ARGS__))
00056 #endif
00057 
00058 #if defined(NO_QI_WARNING)
00059 # define qiLogWarning(...) ::qi::log::detail::qiFalse() && false < qi::log::detail::NullStream().self()
00060 # define qiLogWarningF(Msg, ...)
00061 #else
00062 # define qiLogWarning(...) _QI_LOG_MESSAGE_STREAM(LogLevel_Warning, Warning, __VA_ARGS__)
00063 # define qiLogWarningF(Msg, ...)   _QI_LOG_MESSAGE(LogLevel_Warning,   _QI_LOG_FORMAT(Msg, __VA_ARGS__))
00064 #endif
00065 
00066 #if defined(NO_QI_ERROR)
00067 # define qiLogError(...)   ::qi::log::detail::qiFalse() && false < qi::log::detail::NullStream().self()
00068 # define qiLogErrorF(Msg, ...)
00069 #else
00070 # define qiLogError(...)   _QI_LOG_MESSAGE_STREAM(LogLevel_Error,   Error,   __VA_ARGS__)
00071 # define qiLogErrorF(Msg, ...)   _QI_LOG_MESSAGE(LogLevel_Error,   _QI_LOG_FORMAT(Msg, __VA_ARGS__))
00072 #endif
00073 
00074 #if defined(NO_QI_FATAL)
00075 # define qiLogFatal(...)  ::qi::log::detail::qiFalse() && false < qi::log::detail::NullStream().self()
00076 # define qiLogFatalF(Msg, ...)
00077 #else
00078 # define qiLogFatal(...)   _QI_LOG_MESSAGE_STREAM(LogLevel_Fatal,   Fatal,   __VA_ARGS__)
00079 # define qiLogFatalF(Msg, ...)   _QI_LOG_MESSAGE(LogLevel_Fatal,   _QI_LOG_FORMAT(Msg, __VA_ARGS__))
00080 #endif
00081 
00082 namespace qi {
00083 
00084   enum LogLevel {
00085     LogLevel_Silent = 0,
00086     LogLevel_Fatal,
00087     LogLevel_Error,
00088     LogLevel_Warning,
00089     LogLevel_Info,
00090     LogLevel_Verbose,
00091     LogLevel_Debug
00092   };
00093 
00094   enum LogColor {
00095     LogColor_Never,
00096     LogColor_Auto,
00097     LogColor_Always,
00098   };
00099 
00100   enum LogContextAttr {
00101     LogContextAttr_None           = 0,
00102     LogContextAttr_Verbosity      = 1 << 0,
00103     LogContextAttr_ShortVerbosity = 1 << 1,
00104     LogContextAttr_Date           = 1 << 2,
00105     LogContextAttr_Tid            = 1 << 3,
00106     LogContextAttr_Category       = 1 << 4,
00107     LogContextAttr_File           = 1 << 5,
00108     LogContextAttr_Function       = 1 << 6,
00109     LogContextAttr_Return         = 1 << 7
00110   };
00111 
00112   typedef int LogContext;
00113 
00114   namespace log {
00115 
00116     //deprecated 1.22
00117     QI_API_DEPRECATED static const qi::LogLevel silent = LogLevel_Silent;
00118     QI_API_DEPRECATED static const qi::LogLevel fatal = LogLevel_Fatal;
00119     QI_API_DEPRECATED static const qi::LogLevel error = LogLevel_Error;
00120     QI_API_DEPRECATED static const qi::LogLevel warning = LogLevel_Warning;
00121     QI_API_DEPRECATED static const qi::LogLevel info = LogLevel_Info;
00122     QI_API_DEPRECATED static const qi::LogLevel verbose = LogLevel_Verbose;
00123     QI_API_DEPRECATED static const qi::LogLevel debug = LogLevel_Debug;
00124 
00125     //deprecated 1.22
00126     QI_API_DEPRECATED typedef qi::LogLevel LogLevel;
00127   }
00128 }
00129 
00130 namespace qi {
00131   namespace log {
00132     namespace detail {
00133       struct Category;
00134     }
00135 
00136     typedef unsigned int      SubscriberId;
00137     typedef detail::Category* CategoryType;
00138 
00139     //Deprecated 1.22
00140     QI_API_DEPRECATED typedef unsigned int Subscriber;
00141 
00142     typedef boost::function7<void,
00143                              const qi::LogLevel,
00144                              const qi::os::timeval,
00145                              const char*,
00146                              const char*,
00147                              const char*,
00148                              const char*,
00149                              int> logFuncHandler;
00150 
00151     QI_API void init(qi::LogLevel   verb = qi::LogLevel_Info,
00152                      qi::LogContext context = qi::LogContextAttr_ShortVerbosity | qi::LogContextAttr_Tid | qi::LogContextAttr_Category,
00153                      bool           synchronous = true);
00154 
00155     QI_API void destroy();
00156 
00157     QI_API void log(const qi::LogLevel verb,
00158                     const char*        category,
00159                     const char*        msg,
00160                     const char*        file = "",
00161                     const char*        fct = "",
00162                     const int          line = 0);
00163 
00164     QI_API void log(const qi::LogLevel verb,
00165                     CategoryType       category,
00166                     const std::string& msg,
00167                     const char*        file = "",
00168                     const char*        fct = "",
00169                     const int          line = 0);
00170 
00171 
00172     QI_API const char* logLevelToString(const qi::LogLevel verb, bool verbose = true);
00173 
00174     QI_API qi::LogLevel stringToLogLevel(const char* verb);
00175 
00176     QI_API qi::LogLevel verbosity(SubscriberId sub = 0);
00177 
00179     QI_API std::vector<std::string> categories();
00180 
00190     QI_API void setVerbosity(const std::string& rules, SubscriberId sub = 0);
00191     QI_API void setVerbosity(const qi::LogLevel lv, SubscriberId sub = 0);
00192 
00193 
00195     QI_API CategoryType addCategory(const std::string& name);
00197     QI_API void enableCategory(const std::string& cat, SubscriberId sub = 0);
00199     QI_API void disableCategory(const std::string& cat, SubscriberId sub = 0);
00201     QI_API void setCategory(const std::string& cat, qi::LogLevel level, SubscriberId sub = 0);
00202 
00203 
00205     QI_API bool isVisible(CategoryType category, qi::LogLevel level);
00207     QI_API bool isVisible(const std::string& category, qi::LogLevel level);
00208 
00209     QI_API void setContext(int ctx);
00210 
00211     QI_API int context();
00212 
00213     QI_API void setColor(LogColor color);
00214 
00215     QI_API LogColor color();
00216 
00222     QI_API void setSynchronousLog(bool sync);
00223 
00224     QI_API SubscriberId addLogHandler(const std::string& name,
00225                                     qi::log::logFuncHandler fct,
00226                                     qi::LogLevel defaultLevel = LogLevel_Info);
00227 
00228     QI_API void removeLogHandler(const std::string& name);
00229 
00230     QI_API void flush();
00231 
00232 
00233    #include <qi/details/warn_push_ignore_deprecated.hpp>
00234     // Deprecated 1.22
00235     QI_API_DEPRECATED inline void setVerbosity(SubscriberId sub, const qi::log::LogLevel lv) { setVerbosity((qi::LogLevel)lv, sub); }
00236     // Deprecated 1.22
00237     QI_API_DEPRECATED inline void setCategory(SubscriberId sub, const std::string& cat, qi::log::LogLevel level) { setCategory(cat, (qi::LogLevel)level, sub); }
00238    #include <qi/details/warn_pop_ignore_deprecated.hpp>
00239 
00240   }
00241 }
00242 
00243 #include <qi/details/log.hxx>
00244 
00245 
00246 #endif  // _QI_LOG_HPP_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines