Utility library for MTS Socket Modem Arduino Shield devices from Multi-Tech Systems
Dependents: mtsas mtsas thermostat_fan_demo--fan mtsas ... more
NOTE: MTS-Utils has moved to GitHub. This version will not be updated. For updates, go to the GitHub version.
Revision 1:92c0b062d84d, committed 2014-05-19
- Comitter:
- Mike Fiore
- Date:
- Mon May 19 08:22:30 2014 -0500
- Parent:
- 0:db0490588bc7
- Child:
- 2:7779ede60c3d
- Commit message:
- MTSLog: implement MTSLog
Changed in this revision
MTSLog.cpp | Show annotated file Show diff for this revision Revisions of this file |
MTSLog.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MTSLog.cpp Mon May 19 08:22:30 2014 -0500 @@ -0,0 +1,57 @@ +#include "mbed.h" +#include <stdarg.h> +#include "MTSLog.h" + +using namespace mts; + +int MTSLog::currentLevel = MTSLog::WARNING_LEVEL; + +const char* MTSLog::NONE_LABEL = "NONE"; +const char* MTSLog::FATAL_LABEL = "FATAL"; +const char* MTSLog::ERROR_LABEL = "ERROR"; +const char* MTSLog::WARNING_LABEL = "WARNING"; +const char* MTSLog::INFO_LABEL = "INFO"; +const char* MTSLog::DEBUG_LABEL = "DEBUG"; +const char* MTSLog::TRACE_LABEL = "TRACE"; + +void MTSLog::printMessage(MTSLog::logLevel level, const char* format, ...) { + if (printable(level)) { + va_list argptr; + va_start(argptr, format); + vprintf(format, argptr); + va_end(argptr); + } +} + +bool MTSLog::printable(MTSLog::logLevel level) { + return level <= currentLevel; +} + +void MTSLog::setLogLevel(MTSLog::logLevel level) { + currentLevel = level; +} + +int MTSLog::getLogLevel() { + return currentLevel; +} + +const char* MTSLog::getLogLevelString() { + switch (currentLevel) { + case NONE_LEVEL: + return NONE_LABEL; + case FATAL_LEVEL: + return FATAL_LABEL; + case ERROR_LEVEL: + return ERROR_LABEL; + case WARNING_LEVEL: + return WARNING_LABEL; + case INFO_LEVEL: + return INFO_LABEL; + case DEBUG_LEVEL: + return DEBUG_LABEL; + case TRACE_LEVEL: + return TRACE_LABEL; + default: + return "unknown"; + } +}
--- a/MTSLog.h Thu May 15 21:54:52 2014 +0000 +++ b/MTSLog.h Mon May 19 08:22:30 2014 -0500 @@ -1,4 +1,94 @@ #ifndef MTSLOG_H #define MTSLOG_H -#endif \ No newline at end of file +#include "mbed.h" + +#ifdef MTS_DEBUG +#define logFatal(format, ...) \ + MTSLog::printMessage(MTSLog::FATAL_LEVEL, "%s:%s:%s| [%s] " format "\r\n", __FILE__, __func__, __LINE__, MTSLog::FATAL_LABEL, ##__VA_ARGS__) +#define logError(format, ...) \ + MTSLog::printMessage(MTSLog::ERROR_LEVEL, "%s:%s:%s| [%s] " format "\r\n", __FILE__, __func__, __LINE__, MTSLog::ERROR_LABEL, ##__VA_ARGS__) +#define logWarning(format, ...) \ + MTSLog::printMessage(MTSLog::WARNING_LEVEL, "%s:%s:%s| [%s] " format "\r\n", __FILE__, __func__, __LINE__, MTSLog::WARNING_LABEL, ##__VA_ARGS__) +#define logInfo(format, ...) \ + MTSLog::printMessage(MTSLog::INFO_LEVEL, "%s:%s:%s| [%s] " format "\r\n", __FILE__, __func__, __LINE__, MTSLog::INFO_LABEL, ##__VA_ARGS__) +#define logDebug(format, ...) \ + MTSLog::printMessage(MTSLog::DEBUG_LEVEL, "%s:%s:%s| [%s] " format "\r\n", __FILE__, __func__, __LINE__, MTSLog::DEBUG_LABEL, ##__VA_ARGS__) +#define logTrace(format, ...) \ + MTSLog::printMessage(MTSLog::TRACE_LEVEL, "%s:%s:%s| [%s] " format "\r\n", __FILE__, __func__, __LINE__, MTSLog::TRACE_LABEL, ##__VA_ARGS__) +#else +#define logFatal(format, ...) \ + MTSLog::printMessage(MTSLog::FATAL_LEVEL, "[%s] " format "\r\n", MTSLog::FATAL_LABEL, ##__VA_ARGS__) +#define logError(format, ...) \ + MTSLog::printMessage(MTSLog::ERROR_LEVEL, "[%s] " format "\r\n", MTSLog::ERROR_LABEL, ##__VA_ARGS__) +#define logWarning(format, ...) \ + MTSLog::printMessage(MTSLog::WARNING_LEVEL, "[%s] " format "\r\n", MTSLog::WARNING_LABEL, ##__VA_ARGS__) +#define logInfo(format, ...) \ + MTSLog::printMessage(MTSLog::INFO_LEVEL, "[%s] " format "\r\n", MTSLog::INFO_LABEL, ##__VA_ARGS__) +#define logDebug(format, ...) \ + MTSLog::printMessage(MTSLog::DEBUG_LEVEL, "[%s] " format "\r\n", MTSLog::DEBUG_LABEL, ##__VA_ARGS__) +#define logTrace(format, ...) \ + MTSLog::printMessage(MTSLog::TRACE_LEVEL, "[%s] " format "\r\n", MTSLog::TRACE_LABEL, ##__VA_ARGS__) +#endif + +namespace mts { + +class MTSLog +{ +public: + + /** Enum of log levels. + */ + enum logLevel { + NONE_LEVEL = 0, + FATAL_LEVEL = 1, + ERROR_LEVEL = 2, + WARNING_LEVEL = 3, + INFO_LEVEL = 4, + DEBUG_LEVEL = 5, + TRACE_LEVEL = 6 + }; + + /** Print log message. + */ + static void printMessage(MTSLog::logLevel level, const char* format, ...); + + /** Determine if the given level is currently printable. + */ + static bool printable(MTSLog::logLevel level); + + /** Set log level + * Messages with lower priority than the current level will not be printed. + * If the level is set to NONE, no messages will print. + */ + static void setLogLevel(MTSLog::logLevel level); + + /** Get the current log level. + */ + static int getLogLevel(); + + /** Get string representation of the current log level. + */ + static const char* getLogLevelString(); + + static const char* NONE_LABEL; + static const char* FATAL_LABEL; + static const char* ERROR_LABEL; + static const char* WARNING_LABEL; + static const char* INFO_LABEL; + static const char* DEBUG_LABEL; + static const char* TRACE_LABEL; + +private: + + /** Constructor + */ + MTSLog(); + + static int currentLevel; + +}; + +} + +#endif