From 27a36d7b62f9c620834db21c5743ac4e8dfe1150 Mon Sep 17 00:00:00 2001 From: fvanroie Date: Thu, 26 Mar 2020 23:18:04 +0100 Subject: [PATCH] Switch to ArduinoLog library --- {src => lib/ArduinoLog}/ArduinoLog.cpp | 51 +++++++++++++------------- {src => lib/ArduinoLog}/ArduinoLog.h | 26 ++++++++----- lib/lv_lib_zifont/lv_zifont.cpp | 23 +++++------- src/hasp_log.cpp | 5 ++- src/hasp_log.h | 4 +- 5 files changed, 60 insertions(+), 49 deletions(-) rename {src => lib/ArduinoLog}/ArduinoLog.cpp (74%) rename {src => lib/ArduinoLog}/ArduinoLog.h (90%) diff --git a/src/ArduinoLog.cpp b/lib/ArduinoLog/ArduinoLog.cpp similarity index 74% rename from src/ArduinoLog.cpp rename to lib/ArduinoLog/ArduinoLog.cpp index 51336c61..f8748b50 100644 --- a/src/ArduinoLog.cpp +++ b/lib/ArduinoLog/ArduinoLog.cpp @@ -86,7 +86,7 @@ void Logging::setSuffix(printfunction f) #endif } -void Logging::print(const __FlashStringHelper * format, va_list args) +void Logging::print(Print * logOutput, const __FlashStringHelper * format, va_list args) { #ifndef DISABLE_LOGGING PGM_P p = reinterpret_cast(format); @@ -94,70 +94,71 @@ void Logging::print(const __FlashStringHelper * format, va_list args) for(; c != 0; c = pgm_read_byte(p++)) { if(c == '%') { c = pgm_read_byte(p++); - printFormat(c, &args); + printFormat(logOutput, c, &args); } else { - _logOutput->print(c); + logOutput->print(c); } } #endif } -void Logging::print(const char * format, va_list args) +void Logging::print(Print * logOutput, const char * format, va_list args) { #ifndef DISABLE_LOGGING for(; *format != 0; ++format) { if(*format == '%') { ++format; - printFormat(*format, &args); + printFormat(logOutput, *format, &args); } else { - _logOutput->print(*format); + //_logOutput->print(*format); + logOutput->print(*format); } } #endif } -void Logging::printFormat(const char format, va_list * args) +void Logging::printFormat(Print * logOutput, const char format, va_list * args) { #ifndef DISABLE_LOGGING if(format == '%') { - _logOutput->print(format); + logOutput->print(format); } else if(format == 's') { register char * s = (char *)va_arg(*args, int); - _logOutput->print(s); + logOutput->print(s); } else if(format == 'S') { register __FlashStringHelper * s = (__FlashStringHelper *)va_arg(*args, int); - _logOutput->print(s); + logOutput->print(s); } else if(format == 'd' || format == 'i') { - _logOutput->print(va_arg(*args, int), DEC); + logOutput->print(va_arg(*args, int), DEC); } else if(format == 'u') { - _logOutput->print(va_arg(*args, unsigned int), DEC); + logOutput->print(va_arg(*args, unsigned int), DEC); } else if(format == 'D' || format == 'F') { - _logOutput->print(va_arg(*args, double)); + logOutput->print(va_arg(*args, double)); } else if(format == 'x') { - _logOutput->print(va_arg(*args, int), HEX); + logOutput->print(va_arg(*args, int), HEX); } else if(format == 'X') { - _logOutput->print("0x"); - _logOutput->print(va_arg(*args, int), HEX); + logOutput->print("0x"); + logOutput->print(va_arg(*args, int), HEX); } else if(format == 'b') { - _logOutput->print(va_arg(*args, int), BIN); + logOutput->print(va_arg(*args, int), BIN); } else if(format == 'B') { - _logOutput->print("0b"); - _logOutput->print(va_arg(*args, int), BIN); + logOutput->print("0b"); + logOutput->print(va_arg(*args, int), BIN); } else if(format == 'l') { - _logOutput->print(va_arg(*args, long), DEC); + logOutput->print(va_arg(*args, long), DEC); } else if(format == 'c') { - _logOutput->print((char)va_arg(*args, int)); + logOutput->print((char)va_arg(*args, int)); } else if(format == 't') { if(va_arg(*args, int) == 1) { - _logOutput->print("T"); + logOutput->print("T"); } else { - _logOutput->print("F"); + logOutput->print("F"); } } else if(format == 'T') { if(va_arg(*args, int) == 1) { - _logOutput->print(F("true")); + logOutput->print(F("true")); } else { - _logOutput->print(F("false")); + logOutput->print(F("false")); } } #endif diff --git a/src/ArduinoLog.h b/lib/ArduinoLog/ArduinoLog.h similarity index 90% rename from src/ArduinoLog.h rename to lib/ArduinoLog/ArduinoLog.h index 56082bea..5f7071ca 100644 --- a/src/ArduinoLog.h +++ b/lib/ArduinoLog/ArduinoLog.h @@ -21,7 +21,8 @@ Licensed under the MIT License . #else #include "WProgram.h" #endif -typedef void (*printfunction)(int level, Print *); +#include "StringStream.h" +typedef void (*printfunction)(int level, Print *, String &); //#include //#include @@ -251,11 +252,11 @@ class Logging { } private: - void print(const char * format, va_list args); + void print(Print * logOutput, const char * format, va_list args); - void print(const __FlashStringHelper * format, va_list args); + void print(Print * logOutput, const __FlashStringHelper * format, va_list args); - void printFormat(const char format, va_list * args); + void printFormat(Print * logOutput, const char format, va_list * args); template void printLevel(int level, T msg, ...) { @@ -264,23 +265,30 @@ class Logging { return; } + String debugOutput((char *)0); + StringStream debugStream((String &)debugOutput); + debugOutput.reserve(5 * 128); + if(_prefix != NULL) { - _prefix(level, _logOutput); + _prefix(level, &debugStream, debugOutput); } if(_showLevel) { static const char levels[] = "FEWNTV"; - _logOutput->print(levels[level - 1]); - _logOutput->print(": "); + debugStream.print(levels[level - 1]); + debugStream.print(": "); } va_list args; va_start(args, msg); - print(msg, args); + print(&debugStream, msg, args); if(_suffix != NULL) { - _suffix(level, _logOutput); + _suffix(level, &debugStream, debugOutput); } + + //_logOutput->print(debugOutput); + debugOutput.clear(); #endif } diff --git a/lib/lv_lib_zifont/lv_zifont.cpp b/lib/lv_lib_zifont/lv_zifont.cpp index 2056407f..4e0877e8 100644 --- a/lib/lv_lib_zifont/lv_zifont.cpp +++ b/lib/lv_lib_zifont/lv_zifont.cpp @@ -10,7 +10,8 @@ #include "lvgl.h" #include "lv_zifont.h" -#include "../src/hasp_log.h" +// #include "../src/hasp_log.h" +#include "ArduinoLog.h" /********************* * DEFINES @@ -101,9 +102,7 @@ bool openFont(File & file, const char * filename) { file = SPIFFS.open(filename, "r"); if(!file) { - String error = String(F("FONT: %sOpening font: ")); - error += String(filename); - errorPrintln(error); + Log.error(F("FONT: %sOpening font: %s"), filename); return false; } return true; @@ -150,14 +149,14 @@ int lv_zifont_font_init(lv_font_t ** font, const char * font_path, uint16_t size /* Check that we read the correct size */ if(readSize != sizeof(zi_font_header_t)) { - debugPrintln(PSTR("FONT: Error reading ziFont Header")); + Log.error(F("FONT: Error reading ziFont Header")); file.close(); return ZIFONT_ERROR_READING_DATA; } /* Check ziFile Header Format */ if(header.Password != 4 || header.Version != 5) { - debugPrintln(PSTR("FONT: Unknown font file format")); + Log.error(F("FONT: Unknown font file format")); file.close(); return ZIFONT_ERROR_UNKNOWN_HEADER; } @@ -186,15 +185,13 @@ int lv_zifont_font_init(lv_font_t ** font, const char * font_path, uint16_t size //* Check that we read the correct size if(readSize != sizeof(lv_zifont_char_t) * CHAR_CACHE_SIZE) { - debugPrintln(PSTR("FONT: Error reading ziFont character map")); + Log.error(F("FONT: Error reading ziFont character map")); file.close(); return ZIFONT_ERROR_READING_DATA; } - char msg[128]; - sprintf_P(msg, PSTR("FONT: Loaded V%d Font File: %s containing %d characters"), header.Version, font_path, - header.Maximumnumchars); - debugPrintln(msg); + Log.notice(F("FONT: Loaded V%d Font File: %s containing %d characters"), header.Version, font_path, + header.Maximumnumchars); file.close(); @@ -295,7 +292,7 @@ const uint8_t * lv_font_get_bitmap_fmt_zifont(const lv_font_t * font, uint32_t u if(readSize != sizeof(lv_zifont_char_t)) { file.close(); lv_mem_free(charInfo); - debugPrintln(PSTR("FONT: [ERROR] Wrong number of bytes read from flash")); + Log.error(F("FONT: Wrong number of bytes read from flash")); return NULL; } @@ -303,7 +300,7 @@ const uint8_t * lv_font_get_bitmap_fmt_zifont(const lv_font_t * font, uint32_t u if(charInfo->character != unicode_letter) { file.close(); lv_mem_free(charInfo); - debugPrintln(PSTR("FONT: [ERROR] Incorrect letter read from flash")); + Log.error(F("FONT: Incorrect letter read from flash")); return NULL; } } diff --git a/src/hasp_log.cpp b/src/hasp_log.cpp index 77108129..589b0ec1 100644 --- a/src/hasp_log.cpp +++ b/src/hasp_log.cpp @@ -1,3 +1,4 @@ +/* #include "hasp_conf.h" #include #include "ArduinoLog.h" @@ -16,6 +17,7 @@ #include "hasp_log.h" #include "hasp_debug.h" + void debugPrintln(String & debugText) { serialPrintln(debugText, LOG_LEVEL_NOTICE); @@ -62,4 +64,5 @@ void warningPrintln(String debugText) #if HASP_USE_SYSLOG != 0 syslogSend(LOG_WARNING, buffer); #endif -} \ No newline at end of file +} +*/ \ No newline at end of file diff --git a/src/hasp_log.h b/src/hasp_log.h index 6d6bde0d..4f29533f 100644 --- a/src/hasp_log.h +++ b/src/hasp_log.h @@ -1,3 +1,4 @@ +/* #ifndef HASP_LOG_H #define HASP_LOG_H @@ -8,4 +9,5 @@ void debugPrintln(const char * debugText); void errorPrintln(String debugText); void warningPrintln(String debugText); -#endif \ No newline at end of file +#endif +*/ \ No newline at end of file