Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

Commit

Permalink
refactor trace log to macros
Browse files Browse the repository at this point in the history
  • Loading branch information
waynz0r committed Apr 30, 2024
1 parent deff17d commit d0c3f1c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
28 changes: 26 additions & 2 deletions include/trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__);
Expand All @@ -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
34 changes: 14 additions & 20 deletions src/trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand All @@ -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);
Expand Down

0 comments on commit d0c3f1c

Please sign in to comment.