Skip to content

Commit

Permalink
Update log message output
Browse files Browse the repository at this point in the history
  • Loading branch information
BlockoS committed Sep 1, 2024
1 parent 44d82c9 commit 5c3a57d
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 253 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ find_package(Jansson)
set(CMAKE_C_STANDARDS 11)

add_subdirectory(externals)

#[[
set(etripator_SRC
message.c
message/file.c
Expand Down Expand Up @@ -84,7 +84,7 @@ set_target_properties(etripator_cli
)
target_include_directories(etripator_cli PRIVATE ${CMAKE_SOURCE_DIR})
target_link_libraries(etripator_cli etripator argparse)

#]]
set(DOXYFILE_IN ${CMAKE_CURRENT_SOURCE_DIR}/doxyfile.in)
set(DOXYFILE ${CMAKE_CURRENT_BINARY_DIR}/doxyfile)

Expand All @@ -100,4 +100,4 @@ add_custom_target(doc

add_subdirectory(test)

install(TARGETS etripator_cli DESTINATION bin)
# install(TARGETS etripator_cli DESTINATION bin)
44 changes: 26 additions & 18 deletions message.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,43 +38,51 @@

#include <cwalk.h>

static msg_printer_t* g_msg_printer = NULL;
static MessagePrinter* g_message_printer_head = NULL;

/* Setup global message printer list. */
void msg_printer_init() {
g_msg_printer = NULL;
void message_printer_init() {
g_message_printer_head = NULL;
// nothing much atm...
}

/* Releases the resources used by message printers. */
void msg_printer_destroy() {
msg_printer_t* printer;
for(printer=g_msg_printer; NULL != printer; printer=printer->next) {
printer->close(printer);
void message_printer_destroy() {
for(MessagePrinter *it = g_message_printer_head; it != NULL; it = it->next) {
if(it->close) {
it->close(it);
}
}
g_message_printer_head = NULL;
}

/* Adds a new message printer to the global list. */
int msg_printer_add(msg_printer_t *printer) {
if(printer->open(printer)) {
return 1;
bool message_printer_add(MessagePrinter *printer) {
bool ret = false;
if((printer != NULL) && (printer->open != NULL)) {
ret = printer->open(printer);
printer->next = g_message_printer_head;
g_message_printer_head = printer;
ret = true;
}
printer->next = g_msg_printer;
g_msg_printer = printer;
return 0;
return ret;
}

/* Dispatch messages to printers. */
void print_msg(msg_type_t type, const char* file, size_t line, const char* function, const char* format, ...) {
msg_printer_t* printer;
void message_print(MessageType type, const char* file, size_t line, const char* function, const char* format, ...) {
assert(file != NULL);
assert(function != NULL);
const char* filename;
size_t length;
cwk_path_get_basename(file, &filename, &length);
if(filename == NULL) {
filename = file;
}
for(printer=g_msg_printer; NULL != printer; printer=printer->next) {
if(printer->output) {
for(MessagePrinter *it=g_message_printer_head; it != NULL; it = it->next) {
if(it->output != NULL) {
va_list args;
va_start(args, format);
printer->output(printer, type, filename, line, function, format, args);
(void)it->output(it, type, filename, line, function, format, args);
va_end(args);
}
}
Expand Down
151 changes: 77 additions & 74 deletions message.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,80 +38,83 @@

#include "config.h"

/**
* \brief Message types
*/
/// @defgroup Message Message printing
///@{

// Message types.
typedef enum {
MSG_TYPE_ERROR=0,
MSG_TYPE_WARNING,
MSG_TYPE_INFO
} msg_type_t;

/**
* \brief Initializes and allocates any resources necessary for the message printer.
* \param [in] impl Message printer.
* \return 0 upon success.
*/
typedef int (*msg_printer_open_t)(void* impl);

/**
* \brief Deletes, clean up resources used by the message printer.
* \param [in] impl Message printer.
* \return 0 upon success.
*/
typedef int (*msg_printer_close_t)(void* impl);

/**
* \brief Prints message.
* \param [in] impl Message printer.
* \param [in] type Message type.
* \param [in] file Name of the file where the print message command was issued.
* \param [in] line Line number in the file where the print message command was issued.
* \param [in] function Function where the print message command was issued.
* \param [in] format Format string.
* \param [in] args Argument lists.
* \return 0 upon success.
*/
typedef int (*msg_printer_output_t)(void* impl, msg_type_t type, const char* file, size_t line, const char* function, const char* format, va_list args);

/**
* \brief
*/
typedef struct msg_printer_t_ {
msg_printer_open_t open;
msg_printer_close_t close;
msg_printer_output_t output;
struct msg_printer_t_* next;
} msg_printer_t;

#define ERROR_MSG(format, ...) print_msg(MSG_TYPE_ERROR, __FILE__, __LINE__, __FUNCTION__, format, ##__VA_ARGS__)

#define WARNING_MSG(format, ...) print_msg(MSG_TYPE_WARNING, __FILE__, __LINE__, __FUNCTION__, format, ##__VA_ARGS__)

#define INFO_MSG(format, ...) print_msg(MSG_TYPE_INFO, __FILE__, __LINE__, __FUNCTION__, format, ##__VA_ARGS__)

/**
* Setup global message printer list.
*/
void msg_printer_init();
/**
* Releases the resources used by message printers.
*/
void msg_printer_destroy();
/**
* Adds a new message printer to the global list.
* \param [in] printer Message printer to be added to the list.
* \return 0 upon success.
*/
int msg_printer_add(msg_printer_t *printer);
/**
* Dispatch messages to printers.
* \param type Message type.
* \param file Name of the file where the print message command was issued.
* \param line Line number in the file where the print message command was issued.
* \param function Function where the print message command was issued.
* \param format Format string.
*/
void print_msg(msg_type_t type, const char* file, size_t line, const char* function, const char* format, ...);
MESSAGE_TYPE_ERROR = 0,
MESSAGE_TYPE_WARNING,
MESSAGE_TYPE_INFO,
MESSAGE_TYPE_DEBUG,
MESSAGE_TYPE_COUNT,
} MessageType;

struct MessagePrinter;

/// Initializes and allocates any resources necessary for the message printer.
/// \param [in out] printer Message printer.
/// \return true if the message printer was successfully opened.
/// \return false if an error occured.
typedef bool (*MessagePrinterOpen)(struct MessagePrinter* printer);

/// Releases resources used by the message printer.
/// \param [in] printer Message printer.
/// \return true if the resources used by the message printer were successfully released.
/// \return false if an error occured.
typedef bool (*MessagePrinterClose)(struct MessagePrinter* printer);

/// \brief Prints message.
/// \param [in] printer Message printer.
/// \param [in] type Message type.
/// \param [in] file Name of the file where the print message command was issued.
/// \param [in] line Line number in the file where the print message command was issued.
/// \param [in] function Function where the print message command was issued.
/// \param [in] format Format string.
/// \param [in] args Argument lists.
/// \return true if the message was successfully formatted and printed.
/// \return false if an error occured.
typedef bool (*MessagePrinterOutput)(struct MessagePrinter* printer, MessageType type, const char* file, size_t line, const char* function, const char* format, va_list args);

/// Message printer implementation.
typedef struct MessagePrinter {
MessagePrinterOpen open;
MessagePrinterClose close;
MessagePrinterOutput output;
struct MessagePrinter* next;
} MessagePrinter;

#define ERROR_MSG(format, ...) message_print(MESSAGE_TYPE_ERROR, __FILE__, __LINE__, __FUNCTION__, format, ##__VA_ARGS__)

#define WARNING_MSG(format, ...) message_print(MESSAGE_TYPE_WARNING, __FILE__, __LINE__, __FUNCTION__, format, ##__VA_ARGS__)

#define INFO_MSG(format, ...) message_print(MESSAGE_TYPE_INFO, __FILE__, __LINE__, __FUNCTION__, format, ##__VA_ARGS__)

#if NDEBUG
# define DEBUG_MSG(format, ...)
#else
# define DEBUG_MSG(format, ...) message_print(MESSAGE_TYPE_DEBUG, __FILE__, __LINE__, __FUNCTION__, format, ##__VA_ARGS__)
#endif

/// Setup global message printer list.
void message_printer_init();
/// Releases resources used by message printers.
void message_printer_destroy();

/// Opens and adds a new message printer to the global list.
/// \param [in] printer Message printer to be added to the list.
/// \return true if the message printer was successfully added to the list.
/// \return false if the message printer failed to open and could not be added to the message printer list.
bool message_printer_add(MessagePrinter *printer);

/// Dispatch message to printers.
/// \param type Message type.
/// \param file Name of the file where the print message command was issued.
/// \param line Line number in the file where the print message command was issued.
/// \param function Function where the print message command was issued.
/// \param format Format string.
void message_print(MessageType type, const char* file, size_t line, const char* function, const char* format, ...);

/// @}

#endif // ETRIPATOR_MESSAGE_H
Loading

0 comments on commit 5c3a57d

Please sign in to comment.