Skip to content

Commit

Permalink
♻️ refactoring logging code
Browse files Browse the repository at this point in the history
  • Loading branch information
froz42 committed Oct 15, 2023
1 parent 8cfc35d commit 58ffcc1
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 21 deletions.
3 changes: 3 additions & 0 deletions includes/syscall_strace.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
#define POLL_FDS 9
#define POLL_FDS_AFTER 10
#define SEEK_WHENCE 11
#define PTR 12
#define MMAP_PROT 13
#define MMAP_FLAGS 14

/**
* @brief Negative if printed before the syscall, positive if printed after the
Expand Down
15 changes: 15 additions & 0 deletions srcs/syscall/param_log/log_hex.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "param_log.h"
#include <ft_printf.h>

/**
* @brief log a hexadecimal value
*
* @param value
* @return int
*/
int log_HEX(uint64_t value)
{
if (value == 0)
return ft_dprintf(STDERR_FILENO, "0");
return ft_dprintf(STDERR_FILENO, "%#llx", value);
}
13 changes: 13 additions & 0 deletions srcs/syscall/param_log/log_int.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "param_log.h"
#include <ft_printf.h>

/**
* @brief Log an unsigned integer
*
* @param value value to log
* @return int number of bytes written
*/
int log_INT(uint64_t value)
{
return ft_dprintf(STDERR_FILENO, "%llu", value);
}
7 changes: 7 additions & 0 deletions srcs/syscall/param_log/log_none.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "param_log.h"
#include <ft_printf.h>

int log_NONE(void)
{
return ft_dprintf(STDERR_FILENO, "?");
}
9 changes: 9 additions & 0 deletions srcs/syscall/param_log/log_ptr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "param_log.h"
#include <ft_printf.h>

int log_PTR(uint64_t value)
{
if (value == 0)
return ft_dprintf(STDERR_FILENO, "NULL");
return ft_dprintf(STDERR_FILENO, "%p", (void *)value);
}
7 changes: 7 additions & 0 deletions srcs/syscall/param_log/log_signed_int.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "param_log.h"
#include <ft_printf.h>

int log_SIGNED_INT(int64_t value)
{
return ft_dprintf(STDERR_FILENO, "%d", value);
}
39 changes: 39 additions & 0 deletions srcs/syscall/param_log/param_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,45 @@ typedef struct

typedef int (*log_function_t)();

/**
* @brief log a hexadecimal value
*
* @param value
* @return int
*/
int log_HEX(uint64_t value);

/**
* @brief Log an unsigned integer
*
* @param value value to log
* @return int number of bytes written
*/
int log_INT(uint64_t value);

/**
* @brief Log an unsigned integer
*
* @param value value to log
* @return int number of bytes written
*/
int log_SIGNED_INT(int64_t value);

/**
* @brief Log an unsigned integer
*
* @param value value to log
* @return int number of bytes written
*/
int log_PTR(uint64_t value);

/**
* @brief Log ?
*
* @return int number of bytes written
*/
int log_NONE(void);

/**
* @brief log memory segment
*
Expand Down
22 changes: 1 addition & 21 deletions srcs/syscall/syscall_log_param.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,6 @@
#include <sys/types.h>
#include <syscall_strace.h>

static int log_INT(uint64_t value)
{
return ft_dprintf(STDERR_FILENO, "%llu", value);
}

static int log_SIGNED_INT(int64_t value)
{
return ft_dprintf(STDERR_FILENO, "%d", value);
}

static int log_HEX(uint64_t value)
{
return ft_dprintf(STDERR_FILENO, "%#llx", value);
}

static int log_NONE(uint64_t value)
{
(void)value;
return ft_dprintf(STDERR_FILENO, "?");
}

#define ADD_LOGGER(name) [name] = log_##name

static const log_function_t log_functions[] = {
Expand All @@ -41,6 +20,7 @@ static const log_function_t log_functions[] = {
ADD_LOGGER(POLL_FDS),
ADD_LOGGER(POLL_FDS_AFTER),
ADD_LOGGER(SEEK_WHENCE),
ADD_LOGGER(PTR),
};

typedef int (*log_function_with_param_t)(uint64_t value, syscall_log_param_t *context);
Expand Down

0 comments on commit 58ffcc1

Please sign in to comment.