ULogger.h
Go to the documentation of this file.
00001 /*
00002 *  utilite is a cross-platform library with
00003 *  useful utilities for fast and small developing.
00004 *  Copyright (C) 2010  Mathieu Labbe
00005 *
00006 *  utilite is free library: you can redistribute it and/or modify
00007 *  it under the terms of the GNU Lesser General Public License as published by
00008 *  the Free Software Foundation, either version 3 of the License, or
00009 *  (at your option) any later version.
00010 *
00011 *  utilite is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 *  GNU Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public License
00017 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 #ifndef ULOGGER_H
00021 #define ULOGGER_H
00022 
00023 #include "rtabmap/utilite/UtiLiteExp.h" // DLL export/import defines
00024 
00025 #include "rtabmap/utilite/UMutex.h"
00026 #include "rtabmap/utilite/UDestroyer.h"
00027 #include "rtabmap/utilite/UEvent.h"
00028 
00029 #include <stdio.h>
00030 #include <time.h>
00031 #include <string>
00032 #include <vector>
00033 
00034 #include <stdarg.h>
00035 
00045 /*
00046  * Convenient macros for logging...
00047  */
00048 #define ULOGGER_LOG(level, ...) ULogger::write(level, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
00049 
00050 #define ULOGGER_DEBUG(...)   ULOGGER_LOG(ULogger::kDebug,   __VA_ARGS__)
00051 #define ULOGGER_INFO(...)    ULOGGER_LOG(ULogger::kInfo,    __VA_ARGS__)
00052 #define ULOGGER_WARN(...)        ULOGGER_LOG(ULogger::kWarning, __VA_ARGS__)
00053 #define ULOGGER_ERROR(...)   ULOGGER_LOG(ULogger::kError,   __VA_ARGS__)
00054 #define ULOGGER_FATAL(...)   ULOGGER_LOG(ULogger::kFatal,   __VA_ARGS__)
00055 
00056 #define UDEBUG(...)   ULOGGER_DEBUG(__VA_ARGS__)
00057 #define UINFO(...)    ULOGGER_INFO(__VA_ARGS__)
00058 #define UWARN(...)        ULOGGER_WARN(__VA_ARGS__)
00059 #define UERROR(...)   ULOGGER_ERROR(__VA_ARGS__)
00060 #define UFATAL(...)   ULOGGER_FATAL(__VA_ARGS__)
00061 
00062 #define UASSERT(condition) if(!(condition)) ULogger::write(ULogger::kFatal, __FILE__, __LINE__, __FUNCTION__, "Condition (%s) not met!", #condition)
00063 #define UASSERT_MSG(condition, msg_str) if(!(condition)) ULogger::write(ULogger::kFatal, __FILE__, __LINE__, __FUNCTION__, "Condition (%s) not met! [%s]", #condition, msg_str)
00064 
00117 class ULogEvent : public UEvent
00118 {
00119 public:
00125         ULogEvent(const std::string & msg, int level) :
00126                 UEvent(level),
00127                 msg_(msg)
00128         {}
00129         virtual ~ULogEvent() {}
00133         const std::string & getMsg() const {return msg_;}
00137         virtual std::string getClassName() const {return "ULogEvent";}
00138 private:
00139         std::string msg_;
00140 };
00141 
00225 class UTILITE_EXP ULogger
00226 {
00227 
00228 public:
00232     static const std::string kDefaultLogFileName;
00233 
00240     enum Type{kTypeNoLog, kTypeConsole, kTypeFile};
00241 
00248     enum Level{kDebug, kInfo, kWarning, kError, kFatal};
00249 
00261     static void setType(Type type, const std::string &fileName = kDefaultLogFileName, bool append = true);
00262     static Type type() {return type_;}
00263 
00264     // Setters
00269     static void setPrintTime(bool printTime) {printTime_ = printTime;}
00270     static bool isPrintTime() {return printTime_;}
00271 
00276     static void setPrintLevel(bool printLevel) {printLevel_ = printLevel;}
00277     static bool isPrintLevel() {return printLevel_;}
00278 
00283     static void setPrintEndline(bool printEndline) {printEndline_ = printEndline;}
00284     static bool isPrintEndLine() {return printEndline_;}
00285 
00291         static void setPrintColored(bool printColored) {printColored_ = printColored;}
00292         static bool isPrintColored() {return printColored_;}
00293 
00298     static void setPrintWhere(bool printWhere) {printWhere_ = printWhere;}
00299     static bool isPrintWhere() {return printWhere_;}
00300 
00305     static void setPrintWhereFullPath(bool printWhereFullPath) {printWhereFullPath_ = printWhereFullPath;}
00306     static bool isPrintWhereFullPath() {return printWhereFullPath_;}
00307 
00314     static void setBuffered(bool buffered);
00315     static bool isBuffered() {return buffered_;}
00316 
00328     static void setLevel(ULogger::Level level) {level_ = level;}
00329     static ULogger::Level level() {return level_;}
00330 
00337         static void setExitLevel(ULogger::Level exitLevel) {exitLevel_ = exitLevel;}
00338         static ULogger::Level exitLevel() {return exitLevel_;}
00339 
00346         static void setEventLevel(ULogger::Level eventSentLevel) {eventLevel_ = eventSentLevel;}
00347         static ULogger::Level eventLevel() {return eventLevel_;}
00348 
00352     static void reset();
00353 
00358         static void flush();
00359 
00366     static void write(const char* msg, ...);
00367 
00368     /*
00369      * Write a message to logger: use UDEBUG(), UINFO(), UWARN(), UERROR() or UFATAL() instead.
00370      * @param level the log level of this message
00371      * @param file the file path
00372      * @param line the line in the file
00373      * @param function the function name in which the message is logged
00374      * @param msg the message to write
00375      * @param ... the variable arguments
00376      */
00377     static void write(ULogger::Level level,
00378                 const char * file,
00379                 int line,
00380                 const char *function,
00381                 const char* msg,
00382                 ...);
00383 
00389     static int getTime(std::string &timeStr);
00390 
00391 protected:
00392     /*
00393      * This method is used to have a reference on the 
00394      * Logger. When no Logger exists, one is 
00395      * created. There is only one instance in the application.
00396      * Must be protected by loggerMutex_.
00397      * See the Singleton pattern for further explanation.
00398      *
00399      * @return the reference on the Logger
00400      */
00401     static ULogger* getInstance();
00402 
00403     /*
00404      * Called only once in getInstance(). It can't be instanciated 
00405      * by the user.
00406      *
00407      * @see getInstance()
00408      */
00409     ULogger() {}
00410 
00411     /*
00412      * Only called by a Destroyer.
00413      * @see Destroyer
00414      */
00415     virtual ~ULogger();
00416 
00417     /*
00418          * Flush buffered messages
00419          */
00420         void _flush();
00421 
00422     /*
00423      * A Destroyer is used to remove a dynamicaly created 
00424      * Singleton. It is friend here to have access to the 
00425      * destructor.
00426      *
00427      * @see Destroyer
00428      */
00429     friend class UDestroyer<ULogger>;
00430     
00431     /*
00432      * The log file name.
00433      */
00434     static std::string logFileName_;
00435 
00436     /*
00437      * Default true, it doesn't overwrite the file.
00438      */
00439     static bool append_;
00440     
00441 private:
00442     /*
00443      * Create an instance according to type. See the Abstract factory 
00444      * pattern for further explanation.
00445      * @see type_
00446      * @return the reference on the new logger
00447      */
00448     static ULogger* createInstance();
00449 
00450     /*
00451      * Write a message on the output with the format :
00452      * "A message". Inherited class
00453      * must override this method to output the message. It 
00454      * does nothing by default.
00455      * @param msg the message to write.
00456      * @param arg the variable arguments
00457      */
00458     virtual void _write(const char* msg, va_list arg) {} // Do nothing by default
00459     virtual void _writeStr(const char* msg) {} // Do nothing by default
00460 
00461 private:
00462     /*
00463      * The Logger instance pointer.
00464      */
00465     static ULogger* instance_;
00466 
00467     /*
00468      * The Logger's destroyer
00469      */
00470     static UDestroyer<ULogger> destroyer_;
00471 
00472     /*
00473      * If the logger prints the time for each message. 
00474      * Default is true.
00475      */
00476     static bool printTime_;
00477 
00478     /*
00479      * If the logger prints the level for each message. 
00480      * Default is true.
00481      */
00482     static bool printLevel_;
00483 
00484     /*
00485      * If the logger prints the end line for each message. 
00486      * Default is true.
00487      */
00488     static bool printEndline_;
00489 
00490     /*
00491          * If the logger prints text with color.
00492          * Default is true.
00493          */
00494     static bool printColored_;
00495 
00496     /*
00497          * If the logger prints where the message is logged (fileName::function():line).
00498          * Default is true.
00499          */
00500     static bool printWhere_;
00501 
00502     /*
00503          * If the logger prints the full path of the source file
00504          * where the message is written. Only works when
00505          * "printWhere_" is true.
00506          * Default is false.
00507          */
00508     static bool printWhereFullPath_;
00509 
00510     /*
00511          * If the logger limit the size of the "where" path to
00512          * characters. If the path is over 8 characters, a "~"
00513          * is added. Only works when "printWhereFullPath_" is false.
00514          * Default is false.
00515          */
00516     static bool limitWhereLength_;
00517 
00518     /*
00519      * The type of the logger.
00520      */
00521     static Type type_;
00522 
00523     /*
00524          * The severity of the log.
00525          */
00526     static Level level_;
00527 
00528     /*
00529      * The severity at which the application exits.
00530      * Note : A FATAL level will always exit whatever the level specified here.
00531      */
00532     static Level exitLevel_;
00533 
00534     /*
00535          * The severity at which the message is also sent in a ULogEvent.
00536          */
00537         static Level eventLevel_;
00538 
00539     static const char * levelName_[5];
00540 
00541     /*
00542      * Mutex used when accessing public functions.
00543      */
00544     static UMutex loggerMutex_;
00545 
00546     /*
00547          * If the logger prints messages only when ULogger::flush() is called.
00548          * Default is false.
00549          */
00550         static bool buffered_;
00551 
00552         static std::string bufferedMsgs_;
00553 
00554         /*
00555          * State attribute. This state happens when an exit level
00556          * message is received.
00557          * Messages received during this state are not logged.
00558          * @see exitLevel_
00559          */
00560         static bool exitingState_;
00561 };
00562 
00563 #endif // ULOGGER_H


rtabmap
Author(s): Mathieu Labbe
autogenerated on Fri Aug 28 2015 12:51:42