master
c 105 lines 3.84 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "websocket-internal.h"
4
5 // Debug log function with client, message, and frame IDs
6 void websocket_debug(WS_CLIENT *wsc __maybe_unused, const char *format __maybe_unused, ...) {
7 #ifdef NETDATA_INTERNAL_CHECKS
8 if (!wsc || !format)
9 return;
10
11 char formatted_message[1024];
12 va_list args;
13 va_start(args, format);
14 vsnprintf(formatted_message, sizeof(formatted_message), format, args);
15 va_end(args);
16
17 // Format the debug message with client, message, and frame IDs
18 netdata_log_debug(D_WEBSOCKET, "WEBSOCKET: C=%u M=%zu F=%zu %s",
19 wsc->id, wsc->message_id, wsc->frame_id, formatted_message);
20 #endif /* NETDATA_INTERNAL_CHECKS */
21 }
22
23 // Info log function with client, message, and frame IDs
24 void websocket_info(WS_CLIENT *wsc, const char *format, ...) {
25 if (!wsc || !format)
26 return;
27
28 char formatted_message[1024];
29 va_list args;
30 va_start(args, format);
31 vsnprintf(formatted_message, sizeof(formatted_message), format, args);
32 va_end(args);
33
34 // Format the info message with client, message, and frame IDs
35 netdata_log_info("WEBSOCKET: C=%u M=%zu F=%zu %s",
36 wsc->id, wsc->message_id, wsc->frame_id, formatted_message);
37 }
38
39 // Error log function with client, message, and frame IDs
40 void websocket_error(WS_CLIENT *wsc, const char *format, ...) {
41 if (!wsc || !format)
42 return;
43
44 char formatted_message[1024];
45 va_list args;
46 va_start(args, format);
47 vsnprintf(formatted_message, sizeof(formatted_message), format, args);
48 va_end(args);
49
50 // Format the error message with client, message, and frame IDs
51 netdata_log_error("WEBSOCKET: C=%u M=%zu F=%zu %s",
52 wsc->id, wsc->message_id, wsc->frame_id, formatted_message);
53 }
54
55 // Debug function that logs a message and dumps payload data for debugging
56 void websocket_dump_debug(WS_CLIENT *wsc __maybe_unused, const char *payload __maybe_unused,
57 size_t payload_length __maybe_unused, const char *format __maybe_unused, ...) {
58 #ifdef NETDATA_INTERNAL_CHECKS
59 if (!wsc || !format)
60 return;
61
62 // Format the primary message
63 char formatted_message[1024];
64 va_list args;
65 va_start(args, format);
66 vsnprintf(formatted_message, sizeof(formatted_message), format, args);
67 va_end(args);
68
69 // Handle empty payloads explicitly (log message but no hex dump)
70 if (payload_length == 0) {
71 netdata_log_debug(D_WEBSOCKET, "WEBSOCKET: C=%u M=%zu F=%zu %s (EMPTY PAYLOAD - 0 bytes)",
72 wsc->id, wsc->message_id, wsc->frame_id, formatted_message);
73 return;
74 }
75
76 // If payload is provided and not empty, create and log a hex dump
77 if (payload && payload_length > 0) {
78 size_t bytes_to_dump = (payload_length < WS_DEBUG_DUMP_BYTES) ? payload_length : WS_DEBUG_DUMP_BYTES;
79
80 char hex_dump[(WS_DEBUG_DUMP_BYTES * 2) + 1];
81 char ascii_dump[WS_DEBUG_DUMP_BYTES + 1];
82
83 // Payload check is redundant as we already have it in the outer if
84
85 // Create the hex dump
86 size_t i = 0;
87 for (i = 0; i < bytes_to_dump; i++) {
88 unsigned char c = (unsigned char)payload[i];
89 sprintf(hex_dump + i * 2, "%02x", c);
90 ascii_dump[i] = (isprint(c) ? c : '.');
91 }
92
93 hex_dump[i * 2] = '\0';
94 ascii_dump[i] = '\0';
95
96 // Log the hex dump
97 netdata_log_debug(D_WEBSOCKET, "WEBSOCKET: C=%u M=%zu F=%zu %s DUMP %zu/%zu: HEX:[%s]%s, ASCII:[%s]%s",
98 wsc->id, wsc->message_id, wsc->frame_id,
99 formatted_message,
100 bytes_to_dump, payload_length,
101 hex_dump, payload_length > bytes_to_dump ? "..." : "",
102 ascii_dump, payload_length > bytes_to_dump ? "..." : "");
103 }
104 #endif /* NETDATA_INTERNAL_CHECKS */
105 }