From 8778de1ffd416dcd517064647378e7255187eb1f Mon Sep 17 00:00:00 2001 From: Yuri Trikoz Date: Sun, 18 Oct 2020 10:17:24 +0300 Subject: [PATCH] -40b --- include/Errors.h | 4 ++-- include/Utils/PrintMessage.h | 18 ++++++++++++++++-- include/Utils/StringUtils.h | 4 +++- src/Utils/StringUtils.cpp | 20 +++++--------------- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/include/Errors.h b/include/Errors.h index cefac965..2b117273 100644 --- a/include/Errors.h +++ b/include/Errors.h @@ -2,7 +2,7 @@ #include -String getErrorLevelStr(ErrorLevel_t level); +#include "Utils/StringUtils.h" class Error : public Printable { public: @@ -37,7 +37,7 @@ class Error : public Printable { const String toString() const { char buf[128]; - sprintf(buf, "[%s] %s", getErrorLevelStr(_level).c_str(), _message); + sprintf(buf, "[%c] %s", getErrorLevelStr(_level), _message); return String(buf); } diff --git a/include/Utils/PrintMessage.h b/include/Utils/PrintMessage.h index 637b793f..f33f4252 100644 --- a/include/Utils/PrintMessage.h +++ b/include/Utils/PrintMessage.h @@ -6,7 +6,6 @@ #include "Errors.h" #include "Global.h" - #define pm PrintMessage(MODULE) class PrintMessage { @@ -24,8 +23,23 @@ class PrintMessage { } private: + void printErrorLevel(ErrorLevel_t level) { + Serial.printf("[%c] ", getErrorLevelStr(level)); + } + + void printUptime() { + Serial.printf("%lu ", ((unsigned long)millis() / 1000)); + } + + void printModule() { + Serial.printf("[%s] ", _module); + } + void print(const ErrorLevel_t level, const String& str) { - Serial.printf("%s [%s] [%s] %s\n", prettyMillis(millis()).c_str(), getErrorLevelStr(level).c_str(), _module, str.c_str()); + printUptime(); + printErrorLevel(level); + printModule(); + Serial.println(str.c_str()); } private: diff --git a/include/Utils/StringUtils.h b/include/Utils/StringUtils.h index 3c23ee4a..9dc91d6b 100644 --- a/include/Utils/StringUtils.h +++ b/include/Utils/StringUtils.h @@ -26,4 +26,6 @@ size_t itemsCount(String str, const String& separator); boolean isDigitStr(const String&); -String prettyBytes(size_t size); \ No newline at end of file +String prettyBytes(size_t size); + +const char getErrorLevelStr(uint8_t level); \ No newline at end of file diff --git a/src/Utils/StringUtils.cpp b/src/Utils/StringUtils.cpp index 969cd59e..bcb2351f 100644 --- a/src/Utils/StringUtils.cpp +++ b/src/Utils/StringUtils.cpp @@ -111,26 +111,16 @@ String prettyBytes(size_t size) { return String(size / 1024.0 / 1024.0 / 1024.0) + "GB"; } -static const char *str_info = "I"; -static const char *str_warn = "W"; -static const char *str_error = "E"; -static const char *str_unknown = "?"; - -String getErrorLevelStr(ErrorLevel_t level) { - const char *ptr; +const char getErrorLevelStr(uint8_t level) { switch (level) { case EL_INFO: - ptr = str_info; - break; + return 'I'; case EL_WARNING: - ptr = str_warn; - break; + return 'W'; case EL_ERROR: - ptr = str_error; - break; + return 'E'; default: - ptr = str_unknown; break; } - return String(ptr); + return '?'; } \ No newline at end of file