![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Fork to see if I can get working
Dependencies: BufferedSerial OneWire WinbondSPIFlash libxDot-dev-mbed5-deprecated
Fork of xDotBridge_update_test20180823 by
xDotBridge/inc/MyLog.h@63:e1efbe3402d9, 2017-03-14 (annotated)
- Committer:
- Matt Briggs
- Date:
- Tue Mar 14 09:54:04 2017 -0600
- Revision:
- 63:e1efbe3402d9
Quick refactor of logging
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Matt Briggs | 63:e1efbe3402d9 | 1 | #ifndef MYLOG_H |
Matt Briggs | 63:e1efbe3402d9 | 2 | #define MYLOG_H |
Matt Briggs | 63:e1efbe3402d9 | 3 | |
Matt Briggs | 63:e1efbe3402d9 | 4 | #include "MTSLog.h" |
Matt Briggs | 63:e1efbe3402d9 | 5 | |
Matt Briggs | 63:e1efbe3402d9 | 6 | //inline const char* className(const std::string& prettyFunction) |
Matt Briggs | 63:e1efbe3402d9 | 7 | //{ |
Matt Briggs | 63:e1efbe3402d9 | 8 | // size_t colons = prettyFunction.find_last_of("::"); |
Matt Briggs | 63:e1efbe3402d9 | 9 | // if (colons == std::string::npos) |
Matt Briggs | 63:e1efbe3402d9 | 10 | // return ""; |
Matt Briggs | 63:e1efbe3402d9 | 11 | // size_t begin = prettyFunction.substr(0,colons).rfind(" ") + 1; |
Matt Briggs | 63:e1efbe3402d9 | 12 | // size_t end = colons - begin; |
Matt Briggs | 63:e1efbe3402d9 | 13 | // |
Matt Briggs | 63:e1efbe3402d9 | 14 | // return prettyFunction.substr(begin,end).c_str(); |
Matt Briggs | 63:e1efbe3402d9 | 15 | //} |
Matt Briggs | 63:e1efbe3402d9 | 16 | // |
Matt Briggs | 63:e1efbe3402d9 | 17 | //#define __CLASSNAME__ className(__PRETTY_FUNCTION__) |
Matt Briggs | 63:e1efbe3402d9 | 18 | |
Matt Briggs | 63:e1efbe3402d9 | 19 | |
Matt Briggs | 63:e1efbe3402d9 | 20 | #ifdef MTS_DEBUG |
Matt Briggs | 63:e1efbe3402d9 | 21 | #define logFatal(format, ...) \ |
Matt Briggs | 63:e1efbe3402d9 | 22 | mts::MTSLog::printMessage(mts::MTSLog::FATAL_LEVEL, "%s:%s:%d| [%s] " format "\r\n", __CLASSNAME__, __func__, __LINE__, mts::MTSLog::FATAL_LABEL, ##__VA_ARGS__) |
Matt Briggs | 63:e1efbe3402d9 | 23 | #define logError(format, ...) \ |
Matt Briggs | 63:e1efbe3402d9 | 24 | mts::MTSLog::printMessage(mts::MTSLog::ERROR_LEVEL, "%s:%s:%d| [%s] " format "\r\n", __CLASSNAME__, __func__, __LINE__, mts::MTSLog::ERROR_LABEL, ##__VA_ARGS__) |
Matt Briggs | 63:e1efbe3402d9 | 25 | #define logWarning(format, ...) \ |
Matt Briggs | 63:e1efbe3402d9 | 26 | mts::MTSLog::printMessage(mts::MTSLog::WARNING_LEVEL, "%s:%s:%d| [%s] " format "\r\n", __CLASSNAME__, __func__, __LINE__, mts::MTSLog::WARNING_LABEL, ##__VA_ARGS__) |
Matt Briggs | 63:e1efbe3402d9 | 27 | #define logInfo(format, ...) \ |
Matt Briggs | 63:e1efbe3402d9 | 28 | mts::MTSLog::printMessage(mts::MTSLog::INFO_LEVEL, "%s:%s:%d| [%s] " format "\r\n", __CLASSNAME__, __func__, __LINE__, mts::MTSLog::INFO_LABEL, ##__VA_ARGS__) |
Matt Briggs | 63:e1efbe3402d9 | 29 | #define logDebug(format, ...) \ |
Matt Briggs | 63:e1efbe3402d9 | 30 | mts::MTSLog::printMessage(mts::MTSLog::DEBUG_LEVEL, "%s:%s:%d| [%s] " format "\r\n", __CLASSNAME__, __func__, __LINE__, mts::MTSLog::DEBUG_LABEL, ##__VA_ARGS__) |
Matt Briggs | 63:e1efbe3402d9 | 31 | #define logTrace(format, ...) \ |
Matt Briggs | 63:e1efbe3402d9 | 32 | mts::MTSLog::printMessage(mts::MTSLog::TRACE_LEVEL, "%s:%s:%d| [%s] " format "\r\n", __CLASSNAME__, __func__, __LINE__, mts::MTSLog::TRACE_LABEL, ##__VA_ARGS__) |
Matt Briggs | 63:e1efbe3402d9 | 33 | #else |
Matt Briggs | 63:e1efbe3402d9 | 34 | #define myLogFatal(format, ...) \ |
Matt Briggs | 63:e1efbe3402d9 | 35 | MyLog::printMessage(MyLog::FATAL_LEVEL, "[%s] " format "\r\n", MyLog::FATAL_LABEL, ##__VA_ARGS__) |
Matt Briggs | 63:e1efbe3402d9 | 36 | #define myLogError(format, ...) \ |
Matt Briggs | 63:e1efbe3402d9 | 37 | MyLog::printMessage(MyLog::ERROR_LEVEL, "[%s] " format "\r\n", MyLog::ERROR_LABEL, ##__VA_ARGS__) |
Matt Briggs | 63:e1efbe3402d9 | 38 | #define myLogWarning(format, ...) \ |
Matt Briggs | 63:e1efbe3402d9 | 39 | MyLog::printMessage(MyLog::WARNING_LEVEL, "[%s] " format "\r\n", MyLog::WARNING_LABEL, ##__VA_ARGS__) |
Matt Briggs | 63:e1efbe3402d9 | 40 | #define myLogInfo(format, ...) \ |
Matt Briggs | 63:e1efbe3402d9 | 41 | MyLog::printMessage(MyLog::INFO_LEVEL, "[%s] " format "\r\n", MyLog::INFO_LABEL, ##__VA_ARGS__) |
Matt Briggs | 63:e1efbe3402d9 | 42 | #define myLogDebug(format, ...) \ |
Matt Briggs | 63:e1efbe3402d9 | 43 | MyLog::printMessage(MyLog::DEBUG_LEVEL, "[%s] " format "\r\n", MyLog::DEBUG_LABEL, ##__VA_ARGS__) |
Matt Briggs | 63:e1efbe3402d9 | 44 | #define myLogTrace(format, ...) \ |
Matt Briggs | 63:e1efbe3402d9 | 45 | MyLog::printMessage(MyLog::TRACE_LEVEL, "[%s] " format "\r\n", MyLog::TRACE_LABEL, ##__VA_ARGS__) |
Matt Briggs | 63:e1efbe3402d9 | 46 | #endif |
Matt Briggs | 63:e1efbe3402d9 | 47 | |
Matt Briggs | 63:e1efbe3402d9 | 48 | class MyLog |
Matt Briggs | 63:e1efbe3402d9 | 49 | { |
Matt Briggs | 63:e1efbe3402d9 | 50 | public: |
Matt Briggs | 63:e1efbe3402d9 | 51 | |
Matt Briggs | 63:e1efbe3402d9 | 52 | /** Enum of log levels. |
Matt Briggs | 63:e1efbe3402d9 | 53 | */ |
Matt Briggs | 63:e1efbe3402d9 | 54 | enum logLevel { |
Matt Briggs | 63:e1efbe3402d9 | 55 | NONE_LEVEL = 0, |
Matt Briggs | 63:e1efbe3402d9 | 56 | FATAL_LEVEL = 1, |
Matt Briggs | 63:e1efbe3402d9 | 57 | ERROR_LEVEL = 2, |
Matt Briggs | 63:e1efbe3402d9 | 58 | WARNING_LEVEL = 3, |
Matt Briggs | 63:e1efbe3402d9 | 59 | INFO_LEVEL = 4, |
Matt Briggs | 63:e1efbe3402d9 | 60 | DEBUG_LEVEL = 5, |
Matt Briggs | 63:e1efbe3402d9 | 61 | TRACE_LEVEL = 6 |
Matt Briggs | 63:e1efbe3402d9 | 62 | }; |
Matt Briggs | 63:e1efbe3402d9 | 63 | |
Matt Briggs | 63:e1efbe3402d9 | 64 | /** Print log message. |
Matt Briggs | 63:e1efbe3402d9 | 65 | */ |
Matt Briggs | 63:e1efbe3402d9 | 66 | static void printMessage(int level, const char* format, ...); |
Matt Briggs | 63:e1efbe3402d9 | 67 | |
Matt Briggs | 63:e1efbe3402d9 | 68 | /** Determine if the given level is currently printable. |
Matt Briggs | 63:e1efbe3402d9 | 69 | */ |
Matt Briggs | 63:e1efbe3402d9 | 70 | static bool printable(int level); |
Matt Briggs | 63:e1efbe3402d9 | 71 | |
Matt Briggs | 63:e1efbe3402d9 | 72 | /** Set log level |
Matt Briggs | 63:e1efbe3402d9 | 73 | * Messages with lower priority than the current level will not be printed. |
Matt Briggs | 63:e1efbe3402d9 | 74 | * If the level is set to NONE, no messages will print. |
Matt Briggs | 63:e1efbe3402d9 | 75 | */ |
Matt Briggs | 63:e1efbe3402d9 | 76 | static void setLogLevel(int level); |
Matt Briggs | 63:e1efbe3402d9 | 77 | |
Matt Briggs | 63:e1efbe3402d9 | 78 | /** Get the current log level. |
Matt Briggs | 63:e1efbe3402d9 | 79 | */ |
Matt Briggs | 63:e1efbe3402d9 | 80 | static int getLogLevel(); |
Matt Briggs | 63:e1efbe3402d9 | 81 | |
Matt Briggs | 63:e1efbe3402d9 | 82 | /** Get string representation of the current log level. |
Matt Briggs | 63:e1efbe3402d9 | 83 | */ |
Matt Briggs | 63:e1efbe3402d9 | 84 | static const char* getLogLevelString(); |
Matt Briggs | 63:e1efbe3402d9 | 85 | |
Matt Briggs | 63:e1efbe3402d9 | 86 | static const char* NONE_LABEL; |
Matt Briggs | 63:e1efbe3402d9 | 87 | static const char* FATAL_LABEL; |
Matt Briggs | 63:e1efbe3402d9 | 88 | static const char* ERROR_LABEL; |
Matt Briggs | 63:e1efbe3402d9 | 89 | static const char* WARNING_LABEL; |
Matt Briggs | 63:e1efbe3402d9 | 90 | static const char* INFO_LABEL; |
Matt Briggs | 63:e1efbe3402d9 | 91 | static const char* DEBUG_LABEL; |
Matt Briggs | 63:e1efbe3402d9 | 92 | static const char* TRACE_LABEL; |
Matt Briggs | 63:e1efbe3402d9 | 93 | |
Matt Briggs | 63:e1efbe3402d9 | 94 | private: |
Matt Briggs | 63:e1efbe3402d9 | 95 | |
Matt Briggs | 63:e1efbe3402d9 | 96 | /** Constructor |
Matt Briggs | 63:e1efbe3402d9 | 97 | */ |
Matt Briggs | 63:e1efbe3402d9 | 98 | MyLog(); |
Matt Briggs | 63:e1efbe3402d9 | 99 | |
Matt Briggs | 63:e1efbe3402d9 | 100 | static int currentLevel; |
Matt Briggs | 63:e1efbe3402d9 | 101 | |
Matt Briggs | 63:e1efbe3402d9 | 102 | }; |
Matt Briggs | 63:e1efbe3402d9 | 103 | |
Matt Briggs | 63:e1efbe3402d9 | 104 | #endif |