Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions include/utils/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@
#include <stdbool.h>
#include <string.h>

#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#define LOG_INFO(thl, fd, msg) \
thl->log(thl, fd, msg, LOG_LEVELS_INFO, __FILENAME__, __LINE__);
#define LOG_WARN(thl, fd, msg) \
thl->log(thl, fd, msg, LOG_LEVELS_WARN, __FILENAME__, __LINE__);
#define LOG_ERROR(thl, fd, msg) \
thl->log(thl, fd, msg, LOG_LEVELS_ERROR, __FILENAME__, __LINE__);
#define LOG_DEBUG(thl, fd, msg) \
thl->log(thl, fd, msg, LOG_LEVELS_DEBUG, __FILENAME__, __LINE__);
#define LOGF_INFO(thl, fd, msg, ...) \
thl->logf(thl, fd, LOG_LEVELS_INFO, __FILENAME__, __LINE__, msg, __VA_ARGS__);
#define LOGF_WARN(thl, fd, msg, ...) \
thl->logf(thl, fd, LOG_LEVELS_WARN, __FILENAME__, __LINE__, msg, __VA_ARGS__);
#define LOGF_ERROR(thl, fd, msg, ...) \
thl->logf(thl, fd, LOG_LEVELS_ERROR, __FILENAME__, __LINE__, msg, __VA_ARGS__);
#define LOGF_DEBUG(thl, fd, msg, ...) \
thl->logf(thl, fd, LOG_LEVELS_DEBUG, __FILENAME__, __LINE__, msg, __VA_ARGS__);

/*! @struct base struct used by the thread_logger
*/
struct thread_logger;
Expand Down Expand Up @@ -51,7 +69,8 @@ typedef int (*mutex_fn)(pthread_mutex_t *mx);
* @param level the log level to use (effects color used)
*/
typedef void (*log_fn)(struct thread_logger *thl, int file_descriptor, char *message,
LOG_LEVELS level);
LOG_LEVELS level, char *file, int line);

/*! @typedef signatured used by the thread_logger for printf style log_fn calls
* @param thl pointer to an instance of thread_logger
* @param file_descriptor file descriptor to write log messages to, if 0 then only
Expand All @@ -61,7 +80,7 @@ typedef void (*log_fn)(struct thread_logger *thl, int file_descriptor, char *mes
* @param ... values to supply to message
*/
typedef void (*log_fnf)(struct thread_logger *thl, int file_descriptor,
LOG_LEVELS level, char *message, ...);
LOG_LEVELS level, char *file, int line, char *message, ...);

/*! @typedef a thread safe logger
* @brief guards all log calls with a mutex lock/unlock
Expand Down Expand Up @@ -123,7 +142,7 @@ void clear_file_logger(file_logger *fhl);
* @param level the log level to use (effects color used)
*/
void log_func(thread_logger *thl, int file_descriptor, char *message,
LOG_LEVELS level);
LOG_LEVELS level, char *file, int line);

/*! @brief like log_func but for formatted logs
* @param thl pointer to an instance of thread_logger
Expand All @@ -133,8 +152,8 @@ void log_func(thread_logger *thl, int file_descriptor, char *message,
* @param message format string like `<percent-sign>sFOO<percent-sign>sBAR`
* @param ... values to supply to message
*/
void logf_func(thread_logger *thl, int file_descriptor, LOG_LEVELS level,
char *message, ...);
void logf_func(thread_logger *thl, int file_descriptor, LOG_LEVELS level, char *file,
int line, char *message, ...);

/*! @brief logs a debug styled message - called by log_fn
* @param thl pointer to an instance of thread_logger
Expand Down
25 changes: 16 additions & 9 deletions src/utils/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* - handling system signals (exit, kill, etc...)
*/

#include "utils/logger.h"
#include "thirdparty/logger/logger.h"
#include <fcntl.h>
#include <pthread.h>
#include <stdarg.h>
Expand Down Expand Up @@ -97,8 +97,8 @@ int write_file_log(int file_descriptor, char *message) {
return response;
}

void logf_func(thread_logger *thl, int file_descriptor, LOG_LEVELS level,
char *message, ...) {
void logf_func(thread_logger *thl, int file_descriptor, LOG_LEVELS level, char *file,
int line, char *message, ...) {
va_list args;
va_start(args, message);
char msg[sizeof(args) + (strlen(message) * 2)];
Expand All @@ -110,21 +110,30 @@ void logf_func(thread_logger *thl, int file_descriptor, LOG_LEVELS level,
printf("failed to vsprintf\n");
return;
}
log_func(thl, file_descriptor, msg, level);
log_func(thl, file_descriptor, msg, level, file, line);
}

#pragma GCC diagnostic ignored "-Wunused-parameter"
void log_func(thread_logger *thl, int file_descriptor, char *message,
LOG_LEVELS level) {
LOG_LEVELS level, char *file, int line) {
char *time_str = get_time_string();
if (time_str == NULL) {
// dont printf log as get_time_str does that
return;
}
char location_info[strlen(file) + sizeof(line) + 4];
memset(location_info, 0, sizeof(location_info));

char date_msg[strlen(time_str) + strlen(message) + 2];
sprintf(location_info, " %s:%i", file, line);

char
date_msg[strlen(time_str) + strlen(message) + 2 + sizeof(location_info) + 6];
memset(date_msg, 0, sizeof(date_msg));

strcat(date_msg, time_str);
strcat(date_msg, " -");
strcat(date_msg, location_info);
strcat(date_msg, "] ");
strcat(date_msg, message);

switch (level) {
Expand Down Expand Up @@ -239,14 +248,12 @@ void clear_file_logger(file_logger *fhl) {
char *get_time_string() {
char date[75];
strftime(date, sizeof date, "%b %d %r", localtime(&(time_t){time(NULL)}));
// 4 for [ ] and 1 for \0
char *msg = calloc(1, sizeof(date) + 2);
char *msg = calloc(1, sizeof(date) + 1);
if (msg == NULL) {
printf("failed to calloc get_time_string\n");
return NULL;
}
strcat(msg, "");
strcat(msg, date);
strcat(msg, "] ");
return msg;
}