libmDot0mbed5

Fork of libmDot-mbed5 by MultiTech

Committer:
efersko
Date:
Thu Jul 13 19:03:33 2017 +0000
Revision:
60:d58af0eae613
Parent:
16:b630e18103e5
No change

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Mike Fiore 16:b630e18103e5 1 #ifndef MTSLOG_H
Mike Fiore 16:b630e18103e5 2 #define MTSLOG_H
Mike Fiore 16:b630e18103e5 3
Mike Fiore 16:b630e18103e5 4 #include <string>
Mike Fiore 16:b630e18103e5 5
Mike Fiore 16:b630e18103e5 6 inline const char* className(const std::string& prettyFunction)
Mike Fiore 16:b630e18103e5 7 {
Mike Fiore 16:b630e18103e5 8 size_t colons = prettyFunction.find_last_of("::");
Mike Fiore 16:b630e18103e5 9 if (colons == std::string::npos)
Mike Fiore 16:b630e18103e5 10 return "";
Mike Fiore 16:b630e18103e5 11 size_t begin = prettyFunction.substr(0,colons).rfind(" ") + 1;
Mike Fiore 16:b630e18103e5 12 size_t end = colons - begin;
Mike Fiore 16:b630e18103e5 13
Mike Fiore 16:b630e18103e5 14 return prettyFunction.substr(begin,end).c_str();
Mike Fiore 16:b630e18103e5 15 }
Mike Fiore 16:b630e18103e5 16
Mike Fiore 16:b630e18103e5 17 #define __CLASSNAME__ className(__PRETTY_FUNCTION__)
Mike Fiore 16:b630e18103e5 18
Mike Fiore 16:b630e18103e5 19
Mike Fiore 16:b630e18103e5 20 #ifdef MTS_DEBUG
Mike Fiore 16:b630e18103e5 21 #define logFatal(format, ...) \
Mike Fiore 16:b630e18103e5 22 mts::MTSLog::printMessage(mts::MTSLog::FATAL_LEVEL, "%s:%s:%d| [%s] " format "\r\n", __CLASSNAME__, __func__, __LINE__, mts::MTSLog::FATAL_LABEL, ##__VA_ARGS__)
Mike Fiore 16:b630e18103e5 23 #define logError(format, ...) \
Mike Fiore 16:b630e18103e5 24 mts::MTSLog::printMessage(mts::MTSLog::ERROR_LEVEL, "%s:%s:%d| [%s] " format "\r\n", __CLASSNAME__, __func__, __LINE__, mts::MTSLog::ERROR_LABEL, ##__VA_ARGS__)
Mike Fiore 16:b630e18103e5 25 #define logWarning(format, ...) \
Mike Fiore 16:b630e18103e5 26 mts::MTSLog::printMessage(mts::MTSLog::WARNING_LEVEL, "%s:%s:%d| [%s] " format "\r\n", __CLASSNAME__, __func__, __LINE__, mts::MTSLog::WARNING_LABEL, ##__VA_ARGS__)
Mike Fiore 16:b630e18103e5 27 #define logInfo(format, ...) \
Mike Fiore 16:b630e18103e5 28 mts::MTSLog::printMessage(mts::MTSLog::INFO_LEVEL, "%s:%s:%d| [%s] " format "\r\n", __CLASSNAME__, __func__, __LINE__, mts::MTSLog::INFO_LABEL, ##__VA_ARGS__)
Mike Fiore 16:b630e18103e5 29 #define logDebug(format, ...) \
Mike Fiore 16:b630e18103e5 30 mts::MTSLog::printMessage(mts::MTSLog::DEBUG_LEVEL, "%s:%s:%d| [%s] " format "\r\n", __CLASSNAME__, __func__, __LINE__, mts::MTSLog::DEBUG_LABEL, ##__VA_ARGS__)
Mike Fiore 16:b630e18103e5 31 #define logTrace(format, ...) \
Mike Fiore 16:b630e18103e5 32 mts::MTSLog::printMessage(mts::MTSLog::TRACE_LEVEL, "%s:%s:%d| [%s] " format "\r\n", __CLASSNAME__, __func__, __LINE__, mts::MTSLog::TRACE_LABEL, ##__VA_ARGS__)
Mike Fiore 16:b630e18103e5 33 #else
Mike Fiore 16:b630e18103e5 34 #define logFatal(format, ...) \
Mike Fiore 16:b630e18103e5 35 mts::MTSLog::printMessage(mts::MTSLog::FATAL_LEVEL, "[%s] " format "\r\n", mts::MTSLog::FATAL_LABEL, ##__VA_ARGS__)
Mike Fiore 16:b630e18103e5 36 #define logError(format, ...) \
Mike Fiore 16:b630e18103e5 37 mts::MTSLog::printMessage(mts::MTSLog::ERROR_LEVEL, "[%s] " format "\r\n", mts::MTSLog::ERROR_LABEL, ##__VA_ARGS__)
Mike Fiore 16:b630e18103e5 38 #define logWarning(format, ...) \
Mike Fiore 16:b630e18103e5 39 mts::MTSLog::printMessage(mts::MTSLog::WARNING_LEVEL, "[%s] " format "\r\n", mts::MTSLog::WARNING_LABEL, ##__VA_ARGS__)
Mike Fiore 16:b630e18103e5 40 #define logInfo(format, ...) \
Mike Fiore 16:b630e18103e5 41 mts::MTSLog::printMessage(mts::MTSLog::INFO_LEVEL, "[%s] " format "\r\n", mts::MTSLog::INFO_LABEL, ##__VA_ARGS__)
Mike Fiore 16:b630e18103e5 42 #define logDebug(format, ...) \
Mike Fiore 16:b630e18103e5 43 mts::MTSLog::printMessage(mts::MTSLog::DEBUG_LEVEL, "[%s] " format "\r\n", mts::MTSLog::DEBUG_LABEL, ##__VA_ARGS__)
Mike Fiore 16:b630e18103e5 44 #define logTrace(format, ...) \
Mike Fiore 16:b630e18103e5 45 mts::MTSLog::printMessage(mts::MTSLog::TRACE_LEVEL, "[%s] " format "\r\n", mts::MTSLog::TRACE_LABEL, ##__VA_ARGS__)
Mike Fiore 16:b630e18103e5 46 #endif
Mike Fiore 16:b630e18103e5 47
Mike Fiore 16:b630e18103e5 48 namespace mts {
Mike Fiore 16:b630e18103e5 49
Mike Fiore 16:b630e18103e5 50 class MTSLog
Mike Fiore 16:b630e18103e5 51 {
Mike Fiore 16:b630e18103e5 52 public:
Mike Fiore 16:b630e18103e5 53
Mike Fiore 16:b630e18103e5 54 /** Enum of log levels.
Mike Fiore 16:b630e18103e5 55 */
Mike Fiore 16:b630e18103e5 56 enum logLevel {
Mike Fiore 16:b630e18103e5 57 NONE_LEVEL = 0,
Mike Fiore 16:b630e18103e5 58 FATAL_LEVEL = 1,
Mike Fiore 16:b630e18103e5 59 ERROR_LEVEL = 2,
Mike Fiore 16:b630e18103e5 60 WARNING_LEVEL = 3,
Mike Fiore 16:b630e18103e5 61 INFO_LEVEL = 4,
Mike Fiore 16:b630e18103e5 62 DEBUG_LEVEL = 5,
Mike Fiore 16:b630e18103e5 63 TRACE_LEVEL = 6
Mike Fiore 16:b630e18103e5 64 };
Mike Fiore 16:b630e18103e5 65
Mike Fiore 16:b630e18103e5 66 /** Print log message.
Mike Fiore 16:b630e18103e5 67 */
Mike Fiore 16:b630e18103e5 68 static void printMessage(int level, const char* format, ...);
Mike Fiore 16:b630e18103e5 69
Mike Fiore 16:b630e18103e5 70 /** Determine if the given level is currently printable.
Mike Fiore 16:b630e18103e5 71 */
Mike Fiore 16:b630e18103e5 72 static bool printable(int level);
Mike Fiore 16:b630e18103e5 73
Mike Fiore 16:b630e18103e5 74 /** Set log level
Mike Fiore 16:b630e18103e5 75 * Messages with lower priority than the current level will not be printed.
Mike Fiore 16:b630e18103e5 76 * If the level is set to NONE, no messages will print.
Mike Fiore 16:b630e18103e5 77 */
Mike Fiore 16:b630e18103e5 78 static void setLogLevel(int level);
Mike Fiore 16:b630e18103e5 79
Mike Fiore 16:b630e18103e5 80 /** Get the current log level.
Mike Fiore 16:b630e18103e5 81 */
Mike Fiore 16:b630e18103e5 82 static int getLogLevel();
Mike Fiore 16:b630e18103e5 83
Mike Fiore 16:b630e18103e5 84 /** Get string representation of the current log level.
Mike Fiore 16:b630e18103e5 85 */
Mike Fiore 16:b630e18103e5 86 static const char* getLogLevelString();
Mike Fiore 16:b630e18103e5 87
Mike Fiore 16:b630e18103e5 88 static const char* NONE_LABEL;
Mike Fiore 16:b630e18103e5 89 static const char* FATAL_LABEL;
Mike Fiore 16:b630e18103e5 90 static const char* ERROR_LABEL;
Mike Fiore 16:b630e18103e5 91 static const char* WARNING_LABEL;
Mike Fiore 16:b630e18103e5 92 static const char* INFO_LABEL;
Mike Fiore 16:b630e18103e5 93 static const char* DEBUG_LABEL;
Mike Fiore 16:b630e18103e5 94 static const char* TRACE_LABEL;
Mike Fiore 16:b630e18103e5 95
Mike Fiore 16:b630e18103e5 96 private:
Mike Fiore 16:b630e18103e5 97
Mike Fiore 16:b630e18103e5 98 /** Constructor
Mike Fiore 16:b630e18103e5 99 */
Mike Fiore 16:b630e18103e5 100 MTSLog();
Mike Fiore 16:b630e18103e5 101
Mike Fiore 16:b630e18103e5 102 static int currentLevel;
Mike Fiore 16:b630e18103e5 103
Mike Fiore 16:b630e18103e5 104 };
Mike Fiore 16:b630e18103e5 105
Mike Fiore 16:b630e18103e5 106 }
Mike Fiore 16:b630e18103e5 107
Mike Fiore 16:b630e18103e5 108 #endif