Merge pull request #1357 from jonny5532/feature/improve-web-log

Make web log output show in correct order.
This commit is contained in:
Daniel Öster 2025-07-27 12:54:47 +03:00 committed by GitHub
commit ff0ca99051
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4,8 +4,45 @@
#include "index_html.h"
#if defined(DEBUG_VIA_WEB) || defined(LOG_TO_SD)
char* strnchr(const char* s, int c, size_t n) {
// Like strchr, but only searches the first 'n' bytes of the string.
if (s == NULL) {
return NULL;
}
// Iterate through the string up to 'n' characters or until a null terminator is found.
for (size_t i = 0; i < n && s[i] != '\0'; ++i) {
if (s[i] == c) {
// Character found, return a pointer to it.
return (char*)&s[i];
}
}
// If the character to be found is the null terminator, and we haven't exceeded
// 'n' bytes, check if the null terminator is at the current position.
if (c == '\0') {
for (size_t i = 0; i < n; ++i) {
if (s[i] == '\0') {
return (char*)&s[i];
}
}
}
// Character not found within the first 'n' bytes.
return NULL;
}
String debug_logger_processor(void) {
String content = String(index_html_header);
String content = String();
// Reserve enough space for the content to avoid reallocations.
if (!content.reserve(1000 + sizeof(datalayer.system.info.logged_can_messages))) {
if (content.reserve(15)) {
content += "Out of memory.";
}
return content;
}
content += index_html_header;
// Page format
content += "<style>";
content += "body { background-color: black; color: white; font-family: Arial, sans-serif; }";
@ -28,7 +65,34 @@ String debug_logger_processor(void) {
// Start a new block for the debug log messages
content += "<PRE style='text-align: left'>";
content += String(datalayer.system.info.logged_can_messages);
size_t offset = datalayer.system.info.logged_can_messages_offset;
// If we're mid-buffer, print the older part first.
if (offset > 0 && offset < (sizeof(datalayer.system.info.logged_can_messages) - 1)) {
// Find the next newline after the current offset. The offset will always be
// before the penultimate character, so (offset + 1) will be the final '\0'
// or earlier.
char* next_newline = strnchr(&datalayer.system.info.logged_can_messages[offset + 1], '\n',
sizeof(datalayer.system.info.logged_can_messages) - offset - 1);
if (next_newline != NULL) {
// We found a newline, so append from the character after that. We check
// the string length to ensure we don't add any intermediate '\0'
// characters.
content.concat(next_newline + 1,
strnlen(next_newline + 1, sizeof(datalayer.system.info.logged_can_messages) - offset - 2));
} else {
// No newline found, so append from the next character after the offset to
// the end of the buffer. We check the string length to ensure we don't
// add any intermediate '\0' characters.
content.concat(&datalayer.system.info.logged_can_messages[offset + 1],
strnlen(&datalayer.system.info.logged_can_messages[offset + 1],
sizeof(datalayer.system.info.logged_can_messages) - offset - 1));
}
}
// Append the first part of the buffer up to the current write offset (which
// points to the first \0).
content.concat(datalayer.system.info.logged_can_messages, offset);
content += "</PRE>";
// Add JavaScript for navigation