diff --git a/platformio.ini b/platformio.ini index f9518ff..a1880c0 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,6 +15,7 @@ lib_deps = mikalhart/TinyGPSPlus @ 1.0.2 paulstoffregen/Time @ 1.6 shaggydog/OneButton @ 1.5.0 + peterus/esp-logger @ 0.0.1 check_tool = cppcheck check_flags = cppcheck: --suppress=*:*.pio\* --inline-suppr -DCPPCHECK diff --git a/src/logger.cpp b/src/logger.cpp deleted file mode 100644 index 0e63503..0000000 --- a/src/logger.cpp +++ /dev/null @@ -1,248 +0,0 @@ -#include "logger.h" - -#undef LOG_RESET_COLOR -#undef LOG_COLOR_E -#undef LOG_COLOR_W -#undef LOG_COLOR_I -#undef LOG_COLOR_D -#undef LOG_COLOR_V - -#define LOG_COLOR_BLACK "30" -#define LOG_COLOR_RED "31" -#define LOG_COLOR_GREEN "32" -#define LOG_COLOR_BROWN "33" -#define LOG_COLOR_BLUE "34" -#define LOG_COLOR_PURPLE "35" -#define LOG_COLOR_CYAN "36" -#define LOG_COLOR(COLOR) "\033[0;" COLOR "m" -#define LOG_BOLD(COLOR) "\033[1;" COLOR "m" -#define LOG_RESET_COLOR "\033[0m" -#define LOG_COLOR_E LOG_COLOR(LOG_COLOR_RED) -#define LOG_COLOR_W LOG_COLOR(LOG_COLOR_BROWN) -#define LOG_COLOR_I LOG_COLOR(LOG_COLOR_GREEN) -#define LOG_COLOR_D LOG_COLOR(LOG_COLOR_BLUE) -#define LOG_COLOR_V LOG_COLOR(LOG_COLOR_CYAN) - -Logger::Logger() - : _serial(Serial), _level(DEBUG_LEVEL_DEBUG), _printIsNewline(true) -{ -} - -// cppcheck-suppress unusedFunction -void Logger::setSerial(const HardwareSerial & serial) -{ - _serial = serial; -} - -// cppcheck-suppress unusedFunction -void Logger::setDebugLevel(debug_level_t level) -{ - _level = level; -} - -// cppcheck-suppress unusedFunction -void Logger::printA(const String & text, const char * file, uint32_t line) -{ - printStartColor(DEBUG_LEVEL_NONE); - printHeader(DEBUG_LEVEL_NONE, file, line, false); - _serial.print(text); - printEndColor(DEBUG_LEVEL_NONE); -} - -// cppcheck-suppress unusedFunction -void Logger::printE(const String & text, const char * file, uint32_t line) -{ - printStartColor(DEBUG_LEVEL_ERROR); - printHeader(DEBUG_LEVEL_ERROR, file, line, false); - _serial.print(text); - printEndColor(DEBUG_LEVEL_ERROR); -} - -// cppcheck-suppress unusedFunction -void Logger::printlnA(const String & text, const char * file, uint32_t line) -{ - printStartColor(DEBUG_LEVEL_NONE); - printHeader(DEBUG_LEVEL_NONE, file, line, true); - _serial.println(text); - printEndColor(DEBUG_LEVEL_NONE); -} - -// cppcheck-suppress unusedFunction -void Logger::printlnE(const String & text, const char * file, uint32_t line) -{ - printStartColor(DEBUG_LEVEL_ERROR); - printHeader(DEBUG_LEVEL_ERROR, file, line, true); - _serial.println(text); - printEndColor(DEBUG_LEVEL_ERROR); -} - -// cppcheck-suppress unusedFunction -void Logger::printV(const String & text, const char * file, uint32_t line) -{ - if (_level >= DEBUG_LEVEL_VERBOSE) - { - printStartColor(DEBUG_LEVEL_VERBOSE); - printHeader(DEBUG_LEVEL_VERBOSE, file, line, false); - _serial.print(text); - printEndColor(DEBUG_LEVEL_VERBOSE); - } -} - -// cppcheck-suppress unusedFunction -void Logger::printD(const String & text, const char * file, uint32_t line) -{ - if (_level >= DEBUG_LEVEL_DEBUG) - { - printStartColor(DEBUG_LEVEL_DEBUG); - printHeader(DEBUG_LEVEL_DEBUG, file, line, false); - _serial.print(text); - printEndColor(DEBUG_LEVEL_DEBUG); - } -} - -// cppcheck-suppress unusedFunction -void Logger::printI(const String & text, const char * file, uint32_t line) -{ - if (_level >= DEBUG_LEVEL_INFO) - { - printStartColor(DEBUG_LEVEL_INFO); - printHeader(DEBUG_LEVEL_INFO, file, line, false); - _serial.print(text); - printEndColor(DEBUG_LEVEL_INFO); - } -} - -// cppcheck-suppress unusedFunction -void Logger::printW(const String & text, const char * file, uint32_t line) -{ - if (_level >= DEBUG_LEVEL_WARN) - { - printStartColor(DEBUG_LEVEL_WARN); - printHeader(DEBUG_LEVEL_WARN, file, line, false); - _serial.print(text); - printEndColor(DEBUG_LEVEL_WARN); - } -} - -// cppcheck-suppress unusedFunction -void Logger::printlnV(const String & text, const char * file, uint32_t line) -{ - if (_level >= DEBUG_LEVEL_VERBOSE) - { - printStartColor(DEBUG_LEVEL_VERBOSE); - printHeader(DEBUG_LEVEL_VERBOSE, file, line, true); - _serial.println(text); - printEndColor(DEBUG_LEVEL_VERBOSE); - } -} - -// cppcheck-suppress unusedFunction -void Logger::printlnD(const String & text, const char * file, uint32_t line) -{ - if (_level >= DEBUG_LEVEL_DEBUG) - { - printStartColor(DEBUG_LEVEL_DEBUG); - printHeader(DEBUG_LEVEL_DEBUG, file, line, true); - _serial.println(text); - printEndColor(DEBUG_LEVEL_DEBUG); - } -} - -// cppcheck-suppress unusedFunction -void Logger::printlnI(const String & text, const char * file, uint32_t line) -{ - if (_level >= DEBUG_LEVEL_INFO) - { - printStartColor(DEBUG_LEVEL_INFO); - printHeader(DEBUG_LEVEL_INFO, file, line, true); - _serial.println(text); - printEndColor(DEBUG_LEVEL_INFO); - } -} - -// cppcheck-suppress unusedFunction -void Logger::printlnW(const String & text, const char * file, uint32_t line) -{ - if (_level >= DEBUG_LEVEL_WARN) - { - printStartColor(DEBUG_LEVEL_WARN); - printHeader(DEBUG_LEVEL_WARN, file, line, true); - _serial.println(text); - printEndColor(DEBUG_LEVEL_WARN); - } -} - -void Logger::printStartColor(debug_level_t level) -{ - switch (level) - { - case DEBUG_LEVEL_ERROR: - _serial.print(LOG_COLOR_E); - break; - case DEBUG_LEVEL_WARN: - _serial.print(LOG_COLOR_W); - break; - case DEBUG_LEVEL_INFO: - _serial.print(LOG_COLOR_I); - break; - case DEBUG_LEVEL_DEBUG: - _serial.print(LOG_COLOR_D); - break; - case DEBUG_LEVEL_VERBOSE: - _serial.print(LOG_COLOR_V); - break; - default: - break; - } -} - -void Logger::printHeader(debug_level_t level, const char * file, uint32_t line, bool isln) -{ - if (_printIsNewline) - { - Serial.printf("%c %25s %4d : ", levelToChar(level), file, line); - if(!isln) - { - _printIsNewline = false; - } - } - else - { - _printIsNewline = isln; - } -} - -void Logger::printEndColor(debug_level_t level) -{ - switch (level) - { - case DEBUG_LEVEL_ERROR: - case DEBUG_LEVEL_WARN: - case DEBUG_LEVEL_INFO: - case DEBUG_LEVEL_DEBUG: - case DEBUG_LEVEL_VERBOSE: - _serial.print(LOG_RESET_COLOR); - break; - default: - break; - } -} - -char Logger::levelToChar(debug_level_t level) -{ - switch (level) - { - case DEBUG_LEVEL_ERROR: - return 'E'; - case DEBUG_LEVEL_WARN: - return 'W'; - case DEBUG_LEVEL_INFO: - return 'I'; - case DEBUG_LEVEL_DEBUG: - return 'D'; - case DEBUG_LEVEL_VERBOSE: - return 'V'; - default: - return ' '; - } -} diff --git a/src/logger.h b/src/logger.h deleted file mode 100644 index 8d7242b..0000000 --- a/src/logger.h +++ /dev/null @@ -1,75 +0,0 @@ -#ifndef _LOGGER_H_ -#define _LOGGER_H_ - -#include - -class Logger -{ -public: - enum debug_level_t { - DEBUG_LEVEL_NONE, // No debug output - DEBUG_LEVEL_ERROR, // Critical errors - DEBUG_LEVEL_WARN, // Error conditions but not critical - DEBUG_LEVEL_INFO, // Information messages - DEBUG_LEVEL_DEBUG, // Extra information - default level (if not changed) - DEBUG_LEVEL_VERBOSE, // More information than the usual - DEBUG_LEVELS_SIZE - }; - - static Logger & instance() - { - static Logger _instance; - return _instance; - } - - ~Logger() {} - - void setSerial(const HardwareSerial & serial = Serial); - void setDebugLevel(debug_level_t level); - - // print always: - void printA(const String & text, const char * file, uint32_t line); // always - void printE(const String & text, const char * file, uint32_t line); // error - void printlnA(const String & text, const char * file, uint32_t line); // always with new line - void printlnE(const String & text, const char * file, uint32_t line); // error with new line - - // depending on verbose level: - void printV(const String & text, const char * file, uint32_t line); // verbose - void printD(const String & text, const char * file, uint32_t line); // debug - void printI(const String & text, const char * file, uint32_t line); // information - void printW(const String & text, const char * file, uint32_t line); // warning - - void printlnV(const String & text, const char * file, uint32_t line); // verbose with new line - void printlnD(const String & text, const char * file, uint32_t line); // debug with new line - void printlnI(const String & text, const char * file, uint32_t line); // information with new line - void printlnW(const String & text, const char * file, uint32_t line); // warning with new line - -private: - HardwareSerial & _serial; - debug_level_t _level; - bool _printIsNewline; - - void printStartColor(debug_level_t level); - void printHeader(debug_level_t level, const char * file, uint32_t line, bool isln); - void printEndColor(debug_level_t level); - char levelToChar(debug_level_t level); - - Logger(); - Logger(const Logger &); - Logger & operator = (const Logger &); -}; - -#define logPrintA(text) Logger::instance().printA(text, __FILE__, __LINE__) -#define logPrintE(text) Logger::instance().printE(text, __FILE__, __LINE__) -#define logPrintlnA(text) Logger::instance().printlnA(text, __FILE__, __LINE__) -#define logPrintlnE(text) Logger::instance().printlnE(text, __FILE__, __LINE__) -#define logPrintV(text) Logger::instance().printV(text, __FILE__, __LINE__) -#define logPrintD(text) Logger::instance().printD(text, __FILE__, __LINE__) -#define logPrintI(text) Logger::instance().printI(text, __FILE__, __LINE__) -#define logPrintW(text) Logger::instance().printW(text, __FILE__, __LINE__) -#define logPrintlnV(text) Logger::instance().printlnV(text, __FILE__, __LINE__) -#define logPrintlnD(text) Logger::instance().printlnD(text, __FILE__, __LINE__) -#define logPrintlnI(text) Logger::instance().printlnI(text, __FILE__, __LINE__) -#define logPrintlnW(text) Logger::instance().printlnW(text, __FILE__, __LINE__) - -#endif