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