From dedfc42c676421338e72bba83a6b4857fb9715cb Mon Sep 17 00:00:00 2001 From: Jessie Murray Date: Sun, 1 Aug 2021 13:19:40 -0700 Subject: [PATCH] WS: Log commands WS client commands were not being logged, they are now with a "WS: " prefix. This is done at debug level like for HTTP commands. --- src/cmd.c | 1 - src/slog.c | 4 ++-- src/slog.h | 2 ++ src/websocket.c | 26 +++++++++++++++++++++++++- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/cmd.c b/src/cmd.c index a206eac..df268a3 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -6,7 +6,6 @@ #include "worker.h" #include "http.h" #include "server.h" -#include "slog.h" #include "formats/json.h" #include "formats/raw.h" diff --git a/src/slog.c b/src/slog.c index 9bae415..c48489d 100644 --- a/src/slog.c +++ b/src/slog.c @@ -95,8 +95,8 @@ slog_internal(struct server *s, log_level level, time_t now; struct tm now_tm, *lt_ret; char time_buf[64]; - char msg[124]; - char line[256]; /* bounds are checked. */ + char msg[1 + SLOG_MSG_MAX_LEN]; + char line[2 * SLOG_MSG_MAX_LEN]; /* bounds are checked. */ int line_sz, ret; if(!s->log.fd) return; diff --git a/src/slog.h b/src/slog.h index 14d9c66..d4b47e8 100644 --- a/src/slog.h +++ b/src/slog.h @@ -1,6 +1,8 @@ #ifndef SLOG_H #define SLOG_H +#define SLOG_MSG_MAX_LEN 124 + typedef enum { WEBDIS_ERROR = 0, WEBDIS_WARNING, diff --git a/src/websocket.c b/src/websocket.c index c8ac76c..af31577 100644 --- a/src/websocket.c +++ b/src/websocket.c @@ -235,6 +235,29 @@ ws_handshake_reply(struct ws_client *ws) { return 0; } +static void +ws_log_cmd(struct ws_client *ws, struct cmd *cmd) { + char log_msg[SLOG_MSG_MAX_LEN]; + char *p = log_msg, *eom = log_msg + sizeof(log_msg) - 1; + if(!slog_enabled(ws->http_client->s, WEBDIS_DEBUG)) { + return; + } + + memset(log_msg, 0, sizeof(log_msg)); + memcpy(p, "WS: ", 4); /* WS prefix */ + p += 4; + + for(int i = 0; p < eom && i < cmd->count; i++) { + *p++ = '/'; + char *arg = cmd->argv[i]; + size_t arg_sz = cmd->argv_len[i]; + size_t copy_sz = arg_sz < (size_t)(eom - p) ? arg_sz : (size_t)(eom - p); + memcpy(p, arg, copy_sz); + p += copy_sz; + } + slog(ws->http_client->s, WEBDIS_DEBUG, log_msg, p - log_msg); +} + static int ws_execute(struct ws_client *ws, struct ws_msg *msg) { @@ -288,7 +311,8 @@ ws_execute(struct ws_client *ws, struct ws_msg *msg) { cmd->pub_sub_client = c; } - /* send it off */ + /* log and execute */ + ws_log_cmd(ws, cmd); cmd_send(cmd, fun_reply); return 0;