From 2357b290a5807812e27e922cedd81519fee62cf9 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Wed, 13 Jun 2018 15:03:07 +0300 Subject: [PATCH 1/2] Use arduinojson to print to weblog --- code/espurna/debug.ino | 19 +++++++++++-------- code/html/custom.js | 2 +- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/code/espurna/debug.ino b/code/espurna/debug.ino index 641d28a7..98192d7c 100644 --- a/code/espurna/debug.ino +++ b/code/espurna/debug.ino @@ -68,17 +68,20 @@ void _debugSend(char * message) { #if DEBUG_WEB_SUPPORT if (wsConnected() && (getFreeHeap() > 10000)) { - String m = String(message); - m.replace("\"", """); - m.replace("{", "{"); - m.replace("}", "}"); - char buffer[m.length() + 24]; + DynamicJsonBuffer jsonBuffer(JSON_OBJECT_SIZE(1) + strlen(message) + 17); + JsonObject &root = jsonBuffer.createObject(); #if DEBUG_ADD_TIMESTAMP - snprintf_P(buffer, sizeof(buffer), PSTR("{\"weblog\": \"%s%s\"}"), timestamp, m.c_str()); + char buffer[strlen(timestamp) + strlen(message) + 1]; + snprintf_P(buffer, sizeof(buffer), "%s%s", timestamp, message); + root["weblog"] = buffer; #else - snprintf_P(buffer, sizeof(buffer), PSTR("{\"weblog\": \"%s\"}"), m.c_str()); + root["weblog"] = message; #endif - wsSend(buffer); + String out; + root.printTo(out); + jsonBuffer.clear(); + + wsSend(out.c_str()); pause = true; } #endif diff --git a/code/html/custom.js b/code/html/custom.js index 49d2ab34..78e7a077 100644 --- a/code/html/custom.js +++ b/code/html/custom.js @@ -1217,7 +1217,7 @@ function processData(data) { // Web log if ("weblog" === key) { - $("#weblog").append(value); + $("#weblog").append(new Text(value)); $("#weblog").scrollTop($("#weblog")[0].scrollHeight - $("#weblog").height()); return; } From 415649fabbb9578b98990015a86a290262986621 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Wed, 13 Jun 2018 15:58:53 +0300 Subject: [PATCH 2/2] Check if codacy likes this --- code/espurna/debug.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/espurna/debug.ino b/code/espurna/debug.ino index 98192d7c..7eea7ccb 100644 --- a/code/espurna/debug.ino +++ b/code/espurna/debug.ino @@ -73,9 +73,9 @@ void _debugSend(char * message) { #if DEBUG_ADD_TIMESTAMP char buffer[strlen(timestamp) + strlen(message) + 1]; snprintf_P(buffer, sizeof(buffer), "%s%s", timestamp, message); - root["weblog"] = buffer; + root.set("weblog", buffer); #else - root["weblog"] = message; + root.set("weblog", message); #endif String out; root.printTo(out);