From ab635dd013376a98f6349705d58c9daadf7db0ff Mon Sep 17 00:00:00 2001 From: Pablo2048 Date: Wed, 9 May 2018 19:01:48 +0200 Subject: [PATCH] BugFix-pri formatovani JSON a zkraceni textu v bufferu doslo k poskozeni --- src/trace.cpp | 79 ++++++++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 35 deletions(-) diff --git a/src/trace.cpp b/src/trace.cpp index c425de9..065c6da 100644 --- a/src/trace.cpp +++ b/src/trace.cpp @@ -52,40 +52,10 @@ static void print(uint8_t severity, const char *buffer, int length) while (0 != *buffer) { - switch (*buffer)// uprava escape sekvenci pro JSON/HTML - { - case '"': - lin[lineptr++] = '\\'; - lin[lineptr] = '"'; - break; - - case '\\': - lin[lineptr++] = '\\'; - lin[lineptr] = '\\'; - break; - - case '/': - lin[lineptr++] = '\\'; - lin[lineptr] = '/'; - break; - - case '<': - strcpy_P(&lin[lineptr], PSTR("<")); - lineptr += 3; - break; - - case '>': - strcpy_P(&lin[lineptr], PSTR(">")); - lineptr += 3; - break; - - default: - if (*buffer > 0x1f) - lin[lineptr] = *buffer; - else - lin[lineptr] = '?'; - break; - } + if (*buffer > 0x1f) + lin[lineptr] = *buffer; + else + lin[lineptr] = '?'; ++lineptr; ++buffer; } @@ -110,6 +80,45 @@ void trace_init(void) trace_print(TRACE_INFO, F("Trace: Starting...")); } +static String _getText(const char *buffer) +{ + String res; + + res.reserve(MAX_LINE_LEN * 4); + + while (0 != *buffer) + { + switch (*buffer)// uprava escape sekvenci pro JSON/HTML + { + case '"': + res.concat(F("\\\"")); + break; + + case '\\': + res.concat(F("\\\\")); + break; + + case '/': + res.concat(F("\\/")); + break; + + case '<': + res.concat(F("<")); + break; + + case '>': + res.concat(F(">")); + break; + + default: + res.concat(*buffer); + break; + } + ++buffer; + } + return res; +} + void trace_dumpJSON(String &str) { @@ -124,7 +133,7 @@ void trace_dumpJSON(String &str) str.concat(F(",\"s\":")); str.concat(line._severity); str.concat(F(",\"d\":\"")); - str.concat(line._text); + str.concat(_getText(line._text)); str.concat(F("\"}")); } }