From b2ad29a660e191bed58b1eef98b17373cab1d8c6 Mon Sep 17 00:00:00 2001 From: Max Prokhorov Date: Fri, 8 Feb 2019 00:48:38 +0300 Subject: [PATCH] Revert timestamp prepending, separate debug destinations * Simply print timestamp for supported destinations * Update ws destination to support 'prefix' sub-key * Avoid void casting in telnet module --- code/espurna/debug.ino | 49 ++++++++++++++++++++++++----------------- code/espurna/telnet.ino | 12 ++++++---- code/espurna/ws.ino | 9 ++++++-- code/html/custom.js | 7 +++++- 4 files changed, 50 insertions(+), 27 deletions(-) diff --git a/code/espurna/debug.ino b/code/espurna/debug.ino index d8937f70..2969d0ee 100644 --- a/code/espurna/debug.ino +++ b/code/espurna/debug.ino @@ -16,35 +16,43 @@ char _udp_syslog_header[40] = {0}; #endif #endif -void _debugSend(char * message) { +#if DEBUG_SERIAL_SUPPORT + void _debugSendSerial(const char* prefix, const char* data) { + if (prefix && (prefix[0] != '\0')) { + Serial.print(prefix); + } + Serial.print(data); - size_t msg_len = strlen(message); - bool pause = false; + } +#endif - #if DEBUG_ADD_TIMESTAMP - const char TIMESTAMP_FMT[] = "[%06lu] "; - const uint8_t TIMESTAMP_SIZE = 10; +#if DEBUG_TELNET_SUPPORT + void _debugSendTelnet(const char* prefix, const char* data) { + if (prefix && (prefix[0] != '\0')) { + _telnetWrite(prefix); + } + _telnetWrite(data); - static bool add_timestamp = true; + } +#endif - size_t offset = 0; - char buffer[TIMESTAMP_SIZE + msg_len]; +void _debugSend(const char * message) { - if (add_timestamp) { - snprintf(buffer, TIMESTAMP_SIZE, TIMESTAMP_FMT, millis() % 1000000); - offset = TIMESTAMP_SIZE - 1; - } + const size_t msg_len = strlen(message); - memcpy(buffer + offset, message, msg_len); - buffer[msg_len + offset] = '\0'; + bool pause = false; + char timestamp[10] = {0}; + #if DEBUG_ADD_TIMESTAMP + static bool add_timestamp = true; + if (add_timestamp) { + snprintf(timestamp, sizeof(timestamp), "[%06lu] ", millis() % 1000000); + } add_timestamp = (message[msg_len - 1] == 10) || (message[msg_len - 1] == 13); - #else - char* buffer = message; #endif #if DEBUG_SERIAL_SUPPORT - DEBUG_PORT.print(buffer); + _debugSendSerial(timestamp, message); #endif #if DEBUG_UDP_SUPPORT @@ -64,12 +72,13 @@ void _debugSend(char * message) { #endif #if DEBUG_TELNET_SUPPORT - _telnetWrite(buffer, strlen(buffer)); + _debugSendTelnet(timestamp, message); pause = true; #endif #if DEBUG_WEB_SUPPORT - wsDebugSend(buffer); + wsDebugSend(timestamp, message); + pause = true; #endif if (pause) optimistic_yield(100); diff --git a/code/espurna/telnet.ino b/code/espurna/telnet.ino index b4b53c7c..3108089f 100644 --- a/code/espurna/telnet.ino +++ b/code/espurna/telnet.ino @@ -45,14 +45,14 @@ void _telnetDisconnect(unsigned char clientId) { DEBUG_MSG_P(PSTR("[TELNET] Client #%d disconnected\n"), clientId); } -bool _telnetWrite(unsigned char clientId, void *data, size_t len) { +bool _telnetWrite(unsigned char clientId, const char *data, size_t len) { if (_telnetClients[clientId] && _telnetClients[clientId]->connected()) { - return (_telnetClients[clientId]->write((const char*) data, len) > 0); + return (_telnetClients[clientId]->write(data, len) > 0); } return false; } -unsigned char _telnetWrite(void *data, size_t len) { +unsigned char _telnetWrite(const char *data, size_t len) { unsigned char count = 0; for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) { // Do not send broadcast messages to unauthenticated clients @@ -65,8 +65,12 @@ unsigned char _telnetWrite(void *data, size_t len) { return count; } +unsigned char _telnetWrite(const char *data) { + return _telnetWrite(data, strlen(data)); +} + bool _telnetWrite(unsigned char clientId, const char * message) { - return _telnetWrite(clientId, (void *) message, strlen(message)); + return _telnetWrite(clientId, message, strlen(message)); } void _telnetData(unsigned char clientId, void *data, size_t len) { diff --git a/code/espurna/ws.ino b/code/espurna/ws.ino index 4ed0a899..89a9b20c 100644 --- a/code/espurna/ws.ino +++ b/code/espurna/ws.ino @@ -75,13 +75,18 @@ bool _wsAuth(AsyncWebSocketClient * client) { #if DEBUG_WEB_SUPPORT -bool wsDebugSend(const char* message) { +bool wsDebugSend(const char* prefix, const char* message) { if (!wsConnected()) return false; if (getFreeHeap() < (strlen(message) * 3)) return false; DynamicJsonBuffer jsonBuffer; JsonObject &root = jsonBuffer.createObject(); - root.set("weblog", message); + JsonObject &weblog = root.createNestedObject("weblog"); + + weblog.set("message", message); + if (prefix && (prefix[0] != '\0')) { + weblog.set("prefix", prefix); + } wsSend(root); diff --git a/code/html/custom.js b/code/html/custom.js index 810be461..fd194603 100644 --- a/code/html/custom.js +++ b/code/html/custom.js @@ -1478,7 +1478,12 @@ function processData(data) { // Web log if ("weblog" === key) { websock.send("{}"); - $("#weblog").append(new Text(value)); + + if (value.prefix) { + $("#weblog").append(new Text(value.prefix)); + } + $("#weblog").append(new Text(value.message)); + $("#weblog").scrollTop($("#weblog")[0].scrollHeight - $("#weblog").height()); return; }