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 "find_object/FindObjectExp.h" // DLL export/import defines
00024 
00025 #include "find_object/utilite/UMutex.h"
00026 #include "find_object/utilite/UDestroyer.h"
00027 
00028 #include <stdio.h>
00029 #include <time.h>
00030 #include <string>
00031 #include <vector>
00032 
00033 #include <stdarg.h>
00034 
00035 #if _MSC_VER
00036         #undef min
00037         #undef max
00038 #endif
00039 
00049 /*
00050  * Convenient macros for logging...
00051  */
00052 #define ULOGGER_LOG(level, ...) ULogger::write(level, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
00053 
00054 #define ULOGGER_DEBUG(...)   ULOGGER_LOG(ULogger::kDebug,   __VA_ARGS__)
00055 #define ULOGGER_INFO(...)    ULOGGER_LOG(ULogger::kInfo,    __VA_ARGS__)
00056 #define ULOGGER_WARN(...)        ULOGGER_LOG(ULogger::kWarning, __VA_ARGS__)
00057 #define ULOGGER_ERROR(...)   ULOGGER_LOG(ULogger::kError,   __VA_ARGS__)
00058 #define ULOGGER_FATAL(...)   ULOGGER_LOG(ULogger::kFatal,   __VA_ARGS__)
00059 
00060 #define UDEBUG(...)   ULOGGER_DEBUG(__VA_ARGS__)
00061 #define UINFO(...)    ULOGGER_INFO(__VA_ARGS__)
00062 #define UWARN(...)        ULOGGER_WARN(__VA_ARGS__)
00063 #define UERROR(...)   ULOGGER_ERROR(__VA_ARGS__)
00064 #define UFATAL(...)   ULOGGER_FATAL(__VA_ARGS__)
00065 
00066 #define UASSERT(condition) if(!(condition)) ULogger::write(ULogger::kFatal, __FILE__, __LINE__, __FUNCTION__, "Condition (%s) not met!", #condition)
00067 #define UASSERT_MSG(condition, msg_str) if(!(condition)) ULogger::write(ULogger::kFatal, __FILE__, __LINE__, __FUNCTION__, "Condition (%s) not met! [%s]", #condition, msg_str)
00068 
00198 class FINDOBJECT_EXP ULogger
00199 {
00200 
00201 public:
00205     static const std::string kDefaultLogFileName;
00206 
00213     enum Type{kTypeNoLog, kTypeConsole, kTypeFile};
00214 
00221     enum Level{kDebug, kInfo, kWarning, kError, kFatal};
00222 
00234     static void setType(Type type, const std::string &fileName = kDefaultLogFileName, bool append = true);
00235     static Type type() {return type_;}
00236 
00237     // Setters
00242     static void setPrintTime(bool printTime) {printTime_ = printTime;}
00243     static bool isPrintTime() {return printTime_;}
00244 
00249     static void setPrintLevel(bool printLevel) {printLevel_ = printLevel;}
00250     static bool isPrintLevel() {return printLevel_;}
00251 
00256     static void setPrintEndline(bool printEndline) {printEndline_ = printEndline;}
00257     static bool isPrintEndLine() {return printEndline_;}
00258 
00264         static void setPrintColored(bool printColored) {printColored_ = printColored;}
00265         static bool isPrintColored() {return printColored_;}
00266 
00271     static void setPrintWhere(bool printWhere) {printWhere_ = printWhere;}
00272     static bool isPrintWhere() {return printWhere_;}
00273 
00278     static void setPrintWhereFullPath(bool printWhereFullPath) {printWhereFullPath_ = printWhereFullPath;}
00279     static bool isPrintWhereFullPath() {return printWhereFullPath_;}
00280 
00287     static void setBuffered(bool buffered);
00288     static bool isBuffered() {return buffered_;}
00289 
00301     static void setLevel(ULogger::Level level) {level_ = level;}
00302     static ULogger::Level level() {return level_;}
00303 
00310         static void setExitLevel(ULogger::Level exitLevel) {exitLevel_ = exitLevel;}
00311         static ULogger::Level exitLevel() {return exitLevel_;}
00312 
00319         static void setEventLevel(ULogger::Level eventSentLevel) {eventLevel_ = eventSentLevel;}
00320         static ULogger::Level eventLevel() {return eventLevel_;}
00321 
00325     static void reset();
00326 
00331         static void flush();
00332 
00339     static void write(const char* msg, ...);
00340 
00341     /*
00342      * Write a message to logger: use UDEBUG(), UINFO(), UWARN(), UERROR() or UFATAL() instead.
00343      * @param level the log level of this message
00344      * @param file the file path
00345      * @param line the line in the file
00346      * @param function the function name in which the message is logged
00347      * @param msg the message to write
00348      * @param ... the variable arguments
00349      */
00350     static void write(ULogger::Level level,
00351                 const char * file,
00352                 int line,
00353                 const char *function,
00354                 const char* msg,
00355                 ...);
00356 
00362     static int getTime(std::string &timeStr);
00363 
00364 protected:
00365     /*
00366      * This method is used to have a reference on the 
00367      * Logger. When no Logger exists, one is 
00368      * created. There is only one instance in the application.
00369      * Must be protected by loggerMutex_.
00370      * See the Singleton pattern for further explanation.
00371      *
00372      * @return the reference on the Logger
00373      */
00374     static ULogger* getInstance();
00375 
00376     /*
00377      * Called only once in getInstance(). It can't be instanciated 
00378      * by the user.
00379      *
00380      * @see getInstance()
00381      */
00382     ULogger() {}
00383 
00384     /*
00385      * Only called by a Destroyer.
00386      * @see Destroyer
00387      */
00388     virtual ~ULogger();
00389 
00390     /*
00391          * Flush buffered messages
00392          */
00393         void _flush();
00394 
00395     /*
00396      * A Destroyer is used to remove a dynamicaly created 
00397      * Singleton. It is friend here to have access to the 
00398      * destructor.
00399      *
00400      * @see Destroyer
00401      */
00402     friend class UDestroyer<ULogger>;
00403     
00404     /*
00405      * The log file name.
00406      */
00407     static std::string logFileName_;
00408 
00409     /*
00410      * Default true, it doesn't overwrite the file.
00411      */
00412     static bool append_;
00413     
00414 private:
00415     /*
00416      * Create an instance according to type. See the Abstract factory 
00417      * pattern for further explanation.
00418      * @see type_
00419      * @return the reference on the new logger
00420      */
00421     static ULogger* createInstance();
00422 
00423     /*
00424      * Write a message on the output with the format :
00425      * "A message". Inherited class
00426      * must override this method to output the message. It 
00427      * does nothing by default.
00428      * @param msg the message to write.
00429      * @param arg the variable arguments
00430      */
00431     virtual void _write(const char* msg, va_list arg) {} // Do nothing by default
00432     virtual void _writeStr(const char* msg) {} // Do nothing by default
00433 
00434 private:
00435     /*
00436      * The Logger instance pointer.
00437      */
00438     static ULogger* instance_;
00439 
00440     /*
00441      * The Logger's destroyer
00442      */
00443     static UDestroyer<ULogger> destroyer_;
00444 
00445     /*
00446      * If the logger prints the time for each message. 
00447      * Default is true.
00448      */
00449     static bool printTime_;
00450 
00451     /*
00452      * If the logger prints the level for each message. 
00453      * Default is true.
00454      */
00455     static bool printLevel_;
00456 
00457     /*
00458      * If the logger prints the end line for each message. 
00459      * Default is true.
00460      */
00461     static bool printEndline_;
00462 
00463     /*
00464          * If the logger prints text with color.
00465          * Default is true.
00466          */
00467     static bool printColored_;
00468 
00469     /*
00470          * If the logger prints where the message is logged (fileName::function():line).
00471          * Default is true.
00472          */
00473     static bool printWhere_;
00474 
00475     /*
00476          * If the logger prints the full path of the source file
00477          * where the message is written. Only works when
00478          * "printWhere_" is true.
00479          * Default is false.
00480          */
00481     static bool printWhereFullPath_;
00482 
00483     /*
00484          * If the logger limit the size of the "where" path to
00485          * characters. If the path is over 8 characters, a "~"
00486          * is added. Only works when "printWhereFullPath_" is false.
00487          * Default is false.
00488          */
00489     static bool limitWhereLength_;
00490 
00491     /*
00492      * The type of the logger.
00493      */
00494     static Type type_;
00495 
00496     /*
00497          * The severity of the log.
00498          */
00499     static Level level_;
00500 
00501     /*
00502      * The severity at which the application exits.
00503      * Note : A FATAL level will always exit whatever the level specified here.
00504      */
00505     static Level exitLevel_;
00506 
00507     /*
00508          * The severity at which the message is also sent in a ULogEvent.
00509          */
00510         static Level eventLevel_;
00511 
00512     static const char * levelName_[5];
00513 
00514     /*
00515      * Mutex used when accessing public functions.
00516      */
00517     static UMutex loggerMutex_;
00518 
00519     /*
00520          * If the logger prints messages only when ULogger::flush() is called.
00521          * Default is false.
00522          */
00523         static bool buffered_;
00524 
00525         static std::string bufferedMsgs_;
00526 
00527         /*
00528          * State attribute. This state happens when an exit level
00529          * message is received.
00530          * Messages received during this state are not logged.
00531          * @see exitLevel_
00532          */
00533         static bool exitingState_;
00534 };
00535 
00536 #endif // ULOGGER_H


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Fri Feb 12 2016 01:18:17