From d0c3f1ce6d2c5f46e8f0b1ba7eb7cb7e807b5d8f Mon Sep 17 00:00:00 2001 From: Zsolt Varga Date: Tue, 30 Apr 2024 14:54:26 +0200 Subject: [PATCH] refactor trace log to macros --- include/trace.h | 28 ++++++++++++++++++++++++++-- src/trace.c | 34 ++++++++++++++-------------------- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/include/trace.h b/include/trace.h index f957265c..aea36c97 100644 --- a/include/trace.h +++ b/include/trace.h @@ -28,8 +28,32 @@ void remove_trace_request(trace_request *params); void clear_trace_requests(void); trace_request *get_trace_request(int pid, int uid, const char *command_name); trace_request *get_trace_request_by_partial_match(int pid, int uid, const char *command_name); +char *compose_log_message(const char *message, int n, ...); -int trace_log(const tcp_connection_context *conn_ctx, const char *message, int log_level, int n, ...); +int send_trace(const tcp_connection_context *conn_ctx, const char *message, int log_level, int n, ...); + +#define trace_log(conn_ctx, message, log_level, n, ...) \ + char *log_message = compose_log_message(message, n, ##__VA_ARGS__); \ + if (!IS_ERR(log_message)) \ + { \ + switch (log_level) \ + { \ + case LOGLEVEL_ERR: \ + pr_err("%s", log_message); \ + break; \ + case LOGLEVEL_WARNING: \ + pr_warn("%s", log_message); \ + break; \ + case LOGLEVEL_INFO: \ + pr_info("%s", log_message); \ + break; \ + case LOGLEVEL_DEBUG: \ + pr_debug("%s", log_message); \ + break; \ + } \ + kfree(log_message); \ + } \ + send_trace(conn_ctx, message, log_level, n, ##__VA_ARGS__); #define trace_err(conn_ctx, message, n, ...) \ trace_log(conn_ctx, message, LOGLEVEL_ERR, n, ##__VA_ARGS__); @@ -44,6 +68,6 @@ int trace_log(const tcp_connection_context *conn_ctx, const char *message, int l trace_log(conn_ctx, message, LOGLEVEL_DEBUG, n, ##__VA_ARGS__); #define trace_msg(conn_ctx, message, n, ...) \ - trace_log(conn_ctx, message, -1, n, ##__VA_ARGS__); + send_trace(conn_ctx, message, -1, n, ##__VA_ARGS__); #endif diff --git a/src/trace.c b/src/trace.c index bf3adfc6..ad5ad3e4 100644 --- a/src/trace.c +++ b/src/trace.c @@ -174,7 +174,7 @@ void clear_trace_requests() unlock_trace_requests(); } -char *compose_log_message(const char *message, int n, va_list args) +static char *compose_log_message_with_args(const char *message, int n, va_list args) { int i; va_list args_copy; @@ -251,12 +251,22 @@ char *compose_log_message(const char *message, int n, va_list args) return retval; } -int trace_log(const tcp_connection_context *conn_ctx, const char *message, int log_level, int n, ...) +char *compose_log_message(const char *message, int n, ...) +{ + va_list args; + va_start(args, n); + char *log_message = compose_log_message_with_args(message, n, args); + va_end(args); + + return log_message; +} + +int send_trace(const tcp_connection_context *conn_ctx, const char *message, int log_level, int n, ...) { int ret = 0; unsigned int i; - va_list args, args_copy; - char *level = NULL; + va_list args; + char *level = "default"; task_context *task_ctx = get_task_context(); if (IS_ERR(task_ctx)) @@ -269,37 +279,21 @@ int trace_log(const tcp_connection_context *conn_ctx, const char *message, int l if (log_level > 0) { - va_start(args, n); - va_copy(args_copy, args); - char *log_message = compose_log_message(message, n, args_copy); - if (IS_ERR(log_message)) - return PTR_ERR(log_message); - switch (log_level) { case LOGLEVEL_ERR: - pr_err("%s", log_message); level = "error"; break; case LOGLEVEL_WARNING: - pr_warn("%s", log_message); level = "warning"; break; case LOGLEVEL_INFO: - pr_info("%s", log_message); level = "info"; break; case LOGLEVEL_DEBUG: - pr_debug("%s", log_message); level = "debug"; break; - default: - printk("%s", log_message); } - - kfree(log_message); - va_end(args_copy); - va_end(args); } trace_request *tr = get_trace_request_by_partial_match(task_ctx->pid, task_ctx->uid.val, task_ctx->command_name);