From 02b5ec62c759cdcb6bcd234f8ea3b84717d022af Mon Sep 17 00:00:00 2001 From: Robert Lubos Date: Thu, 9 Jan 2025 15:11:41 +0100 Subject: [PATCH] log: websocket: Avoid byte drop for long messages In case the log message length exceeded the websocket buffer size, the backend would drop the overflowing character, instead of sending it as a part of the next message. Rework the ws_console_out() logic a bit to prevent that. Signed-off-by: Robert Lubos --- subsys/logging/backends/log_backend_ws.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/subsys/logging/backends/log_backend_ws.c b/subsys/logging/backends/log_backend_ws.c index 5f3958b0e25..0a50594c089 100644 --- a/subsys/logging/backends/log_backend_ws.c +++ b/subsys/logging/backends/log_backend_ws.c @@ -66,17 +66,19 @@ static int ws_console_out(struct log_backend_ws_ctx *ctx, int c) unsigned int cnt = 0; int ret = 0; - if (pos >= (sizeof(output_buf) - 1)) { - printnow = true; + __ASSERT_NO_MSG(pos < sizeof(output_buf)); + + if ((c != '\n') && (c != '\r')) { + output_buf[pos++] = c; } else { - if ((c != '\n') && (c != '\r')) { - output_buf[pos++] = c; - } else { - printnow = true; - } + printnow = true; } - if (printnow) { + if (pos >= sizeof(output_buf)) { + printnow = true; + } + + if (pos > 0 && printnow) { while (ctx->sock >= 0 && cnt < max_cnt) { ret = ws_send_all(ctx->sock, output_buf, pos); if (ret < 0) {