The Modified Dot Library for SX1272

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MTSLog.h Source File

MTSLog.h

00001 #ifndef MTSLOG_H
00002 #define MTSLOG_H
00003 
00004 #include <string>
00005 
00006 inline const char* className(const std::string& prettyFunction)
00007 {
00008     size_t colons = prettyFunction.find_last_of("::");
00009     if (colons == std::string::npos)
00010         return "";
00011     size_t begin = prettyFunction.substr(0,colons).rfind(" ") + 1;
00012     size_t end = colons - begin;
00013 
00014     return prettyFunction.substr(begin,end).c_str();
00015 }
00016 
00017 #define __CLASSNAME__ className(__PRETTY_FUNCTION__)
00018 
00019 #ifdef MTS_TIMESTAMP_LOG
00020 #define __LOG__(logLevel, format, ...)                                   \
00021     mts::MTSLog::printMessage(logLevel, "%s| [%s] " format "\r\n", \
00022                               mts::MTSLog::getTime().c_str(), mts::MTSLog::getLogLevelString(logLevel), ##__VA_ARGS__)
00023 #else
00024 #define __LOG__(logLevel, format, ...)                                   \
00025     mts::MTSLog::printMessage(logLevel, "[%s] " format "\r\n", mts::MTSLog::getLogLevelString(logLevel), ##__VA_ARGS__)
00026 #endif
00027 
00028 #ifdef MTS_DEBUG
00029 #define logFatal(format, ...) \
00030     mts::MTSLog::printMessage(mts::MTSLog::FATAL_LEVEL, "%s:%s:%d| [%s] " format "\r\n", __CLASSNAME__, __func__, __LINE__, mts::MTSLog::FATAL_LABEL, ##__VA_ARGS__)
00031 #define logError(format, ...) \
00032     mts::MTSLog::printMessage(mts::MTSLog::ERROR_LEVEL, "%s:%s:%d| [%s] " format "\r\n", __CLASSNAME__, __func__, __LINE__, mts::MTSLog::ERROR_LABEL, ##__VA_ARGS__)
00033 #define logWarning(format, ...) \
00034     mts::MTSLog::printMessage(mts::MTSLog::WARNING_LEVEL, "%s:%s:%d| [%s] " format "\r\n", __CLASSNAME__, __func__, __LINE__, mts::MTSLog::WARNING_LABEL, ##__VA_ARGS__)
00035 #define logInfo(format, ...) \
00036     mts::MTSLog::printMessage(mts::MTSLog::INFO_LEVEL, "%s:%s:%d| [%s] " format "\r\n", __CLASSNAME__, __func__, __LINE__, mts::MTSLog::INFO_LABEL, ##__VA_ARGS__)
00037 #define logDebug(format, ...) \
00038     mts::MTSLog::printMessage(mts::MTSLog::DEBUG_LEVEL, "%s:%s:%d| [%s] " format "\r\n", __CLASSNAME__, __func__, __LINE__, mts::MTSLog::DEBUG_LABEL, ##__VA_ARGS__)
00039 #define logTrace(format, ...) \
00040     mts::MTSLog::printMessage(mts::MTSLog::TRACE_LEVEL, "%s:%s:%d| [%s] " format "\r\n", __CLASSNAME__, __func__, __LINE__, mts::MTSLog::TRACE_LABEL, ##__VA_ARGS__)
00041 #else
00042 #define logFatal(format, ...) \
00043     __LOG__(mts::MTSLog::FATAL_LEVEL, format, ##__VA_ARGS__)
00044 #define logError(format, ...) \
00045     __LOG__(mts::MTSLog::ERROR_LEVEL, format, ##__VA_ARGS__)
00046 #define logWarning(format, ...) \
00047     __LOG__(mts::MTSLog::WARNING_LEVEL, format, ##__VA_ARGS__)
00048 #define logInfo(format, ...) \
00049     __LOG__(mts::MTSLog::INFO_LEVEL, format, ##__VA_ARGS__)
00050 #define logDebug(format, ...) \
00051     __LOG__(mts::MTSLog::DEBUG_LEVEL, format, ##__VA_ARGS__)
00052 #define logTrace(format, ...) \
00053     __LOG__(mts::MTSLog::TRACE_LEVEL, format, ##__VA_ARGS__)
00054 #endif
00055 
00056 namespace mts {
00057 
00058 class MTSLog
00059 {
00060 public:
00061 
00062     /** Enum of log levels.
00063      */
00064     enum logLevel {
00065         NONE_LEVEL = 0,
00066         FATAL_LEVEL = 1,
00067         ERROR_LEVEL = 2,
00068         WARNING_LEVEL = 3,
00069         INFO_LEVEL = 4,
00070         DEBUG_LEVEL = 5,
00071         TRACE_LEVEL = 6
00072     };
00073 
00074     /** Print log message.
00075      */
00076     static void printMessage(int level, const char* format, ...);
00077 
00078     /** Determine if the given level is currently printable.
00079      */
00080     static bool printable(int level);
00081 
00082     /** Set log level
00083      * Messages with lower priority than the current level will not be printed.
00084      * If the level is set to NONE, no messages will print.
00085      */
00086     static void setLogLevel(int level);
00087 
00088     /** Get the current log level.
00089      */
00090     static int getLogLevel();
00091 
00092     /** Get string representation of the current log level.
00093      */
00094     static const char* getLogLevelString();
00095     static const char* getLogLevelString(int level);
00096 
00097     /** Get a formatted time string as HH:MM:SS
00098      */
00099     static std::string getTime();
00100 
00101     static const char* NONE_LABEL;
00102     static const char* FATAL_LABEL;
00103     static const char* ERROR_LABEL;
00104     static const char* WARNING_LABEL;
00105     static const char* INFO_LABEL;
00106     static const char* DEBUG_LABEL;
00107     static const char* TRACE_LABEL;
00108 
00109 private:
00110 
00111     /** Constructor
00112      */
00113     MTSLog();
00114 
00115     static int currentLevel;
00116 
00117 };
00118 
00119 }
00120 
00121 #endif