Browse Source

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
sensors
Max Prokhorov 5 years ago
parent
commit
b2ad29a660
4 changed files with 50 additions and 27 deletions
  1. +29
    -20
      code/espurna/debug.ino
  2. +8
    -4
      code/espurna/telnet.ino
  3. +7
    -2
      code/espurna/ws.ino
  4. +6
    -1
      code/html/custom.js

+ 29
- 20
code/espurna/debug.ino View File

@ -16,35 +16,43 @@ char _udp_syslog_header[40] = {0};
#endif #endif
#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); add_timestamp = (message[msg_len - 1] == 10) || (message[msg_len - 1] == 13);
#else
char* buffer = message;
#endif #endif
#if DEBUG_SERIAL_SUPPORT #if DEBUG_SERIAL_SUPPORT
DEBUG_PORT.print(buffer);
_debugSendSerial(timestamp, message);
#endif #endif
#if DEBUG_UDP_SUPPORT #if DEBUG_UDP_SUPPORT
@ -64,12 +72,13 @@ void _debugSend(char * message) {
#endif #endif
#if DEBUG_TELNET_SUPPORT #if DEBUG_TELNET_SUPPORT
_telnetWrite(buffer, strlen(buffer));
_debugSendTelnet(timestamp, message);
pause = true; pause = true;
#endif #endif
#if DEBUG_WEB_SUPPORT #if DEBUG_WEB_SUPPORT
wsDebugSend(buffer);
wsDebugSend(timestamp, message);
pause = true;
#endif #endif
if (pause) optimistic_yield(100); if (pause) optimistic_yield(100);


+ 8
- 4
code/espurna/telnet.ino View File

@ -45,14 +45,14 @@ void _telnetDisconnect(unsigned char clientId) {
DEBUG_MSG_P(PSTR("[TELNET] Client #%d disconnected\n"), 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()) { if (_telnetClients[clientId] && _telnetClients[clientId]->connected()) {
return (_telnetClients[clientId]->write((const char*) data, len) > 0);
return (_telnetClients[clientId]->write(data, len) > 0);
} }
return false; return false;
} }
unsigned char _telnetWrite(void *data, size_t len) {
unsigned char _telnetWrite(const char *data, size_t len) {
unsigned char count = 0; unsigned char count = 0;
for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) { for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
// Do not send broadcast messages to unauthenticated clients // Do not send broadcast messages to unauthenticated clients
@ -65,8 +65,12 @@ unsigned char _telnetWrite(void *data, size_t len) {
return count; return count;
} }
unsigned char _telnetWrite(const char *data) {
return _telnetWrite(data, strlen(data));
}
bool _telnetWrite(unsigned char clientId, const char * message) { 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) { void _telnetData(unsigned char clientId, void *data, size_t len) {


+ 7
- 2
code/espurna/ws.ino View File

@ -75,13 +75,18 @@ bool _wsAuth(AsyncWebSocketClient * client) {
#if DEBUG_WEB_SUPPORT #if DEBUG_WEB_SUPPORT
bool wsDebugSend(const char* message) {
bool wsDebugSend(const char* prefix, const char* message) {
if (!wsConnected()) return false; if (!wsConnected()) return false;
if (getFreeHeap() < (strlen(message) * 3)) return false; if (getFreeHeap() < (strlen(message) * 3)) return false;
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonObject &root = jsonBuffer.createObject(); 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); wsSend(root);


+ 6
- 1
code/html/custom.js View File

@ -1478,7 +1478,12 @@ function processData(data) {
// Web log // Web log
if ("weblog" === key) { if ("weblog" === key) {
websock.send("{}"); 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()); $("#weblog").scrollTop($("#weblog")[0].scrollHeight - $("#weblog").height());
return; return;
} }


Loading…
Cancel
Save