From 4c9c547e62f4b204f737a30dae984842fa44e0e2 Mon Sep 17 00:00:00 2001 From: max Date: Sun, 27 Oct 2024 22:58:20 +0100 Subject: [PATCH] [ADD] Double linked list for holding app loggers. --- src/net/CMakeLists.txt | 1 + src/net/collections/CMakeLists.txt | 2 + src/net/collections/list.c | 63 ++++++++++++++++++++++++++++++ src/net/include/list.h | 14 +++++++ src/net/include/log.h | 4 +- src/net/logging/log.c | 15 ++++++- 6 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 src/net/collections/CMakeLists.txt create mode 100644 src/net/collections/list.c create mode 100644 src/net/include/list.h diff --git a/src/net/CMakeLists.txt b/src/net/CMakeLists.txt index a7e3e83..7509e99 100755 --- a/src/net/CMakeLists.txt +++ b/src/net/CMakeLists.txt @@ -14,6 +14,7 @@ target_link_directories(netex PUBLIC ${CURSES_INCLUDE_DIR}) target_include_directories(netex PRIVATE ${CMAKE_SOURCE_DIR}/src/net/include) +add_subdirectory(collections) add_subdirectory(include) add_subdirectory(logging) add_subdirectory(str) diff --git a/src/net/collections/CMakeLists.txt b/src/net/collections/CMakeLists.txt new file mode 100644 index 0000000..570c302 --- /dev/null +++ b/src/net/collections/CMakeLists.txt @@ -0,0 +1,2 @@ +target_sources(netex PUBLIC + "list.c") \ No newline at end of file diff --git a/src/net/collections/list.c b/src/net/collections/list.c new file mode 100644 index 0000000..e8474f9 --- /dev/null +++ b/src/net/collections/list.c @@ -0,0 +1,63 @@ +#include +#include "netex.h" +#include "stdlib.h" + +#include "list.h" + +list_node * insert_end(list_node* list, void* data) +{ + list_node* node = (list_node*)malloc(sizeof(list_node)); + if (node == NULL) + { + int error = errno; + //Failed -> log + return NULL; + } + node->data = data; + node->previous = NULL; + node->next = NULL; + + if (list != NULL) + { + if (list->next == NULL) + list->next = node; + list_node* current = list->next; + for (;;) + { + if (current->next == NULL) + { + current->next = node; + node->previous = current; + break; + } + current = current->next; + } + } + + return node; +} + +void delete_node(list_node* node) +{ + if (node == NULL) + return; + node->previous->next = node->next; + node->next->previous = node->previous; + free(node); +} + +void iterate_list(list_node* list, bool forward, int (*iterFunc)(const void*)) +{ + if (list == NULL || iterFunc == NULL) + return; + list_node * current = list; + while (current != NULL) + { + if (iterFunc(current->data) == 1) + break; + if (forward) + current = current->next; + else + current = current->previous; + } +} \ No newline at end of file diff --git a/src/net/include/list.h b/src/net/include/list.h new file mode 100644 index 0000000..b83ca71 --- /dev/null +++ b/src/net/include/list.h @@ -0,0 +1,14 @@ +#ifndef NETEX_LIST_H +#define NETEX_LIST_H + + +typedef struct list_node +{ + void* data; + struct list_node* previous; + struct list_node* next; +} list_node; + +void iterate_list(list_node* list, bool forward, int (*iterFunc)(const void*)); + +#endif //NETEX_LIST_H diff --git a/src/net/include/log.h b/src/net/include/log.h index 932dfad..1cbebdb 100755 --- a/src/net/include/log.h +++ b/src/net/include/log.h @@ -1,5 +1,6 @@ #ifndef LOG_H #define LOG_H +#include "list.h" enum log_level { @@ -16,7 +17,8 @@ typedef struct app_logger_info char* executable_name; int log_file_max_size; enum log_level max_log_level; - /// logger list/linked list/array? + list_node* loggers; + } app_logger_info; typedef struct logger diff --git a/src/net/logging/log.c b/src/net/logging/log.c index 7f1b61f..8631714 100755 --- a/src/net/logging/log.c +++ b/src/net/logging/log.c @@ -1,6 +1,8 @@ +#include + #include "netex.h" #include "log.h" -#include + bool canLog = true; app_logger_info* default_app_log = NULL; @@ -10,11 +12,22 @@ app_logger_info* initialize_log(char* logPath, char* executable, int maxFileSize return NULL; } +int shutdown_logger(const void* data) +{ + logger* logger = (struct logger*)data; + logger->app_logger = NULL; + logger->name = NULL; + free(logger); + return 0; +} void shutdown_log(app_logger_info* appLogger) { canLog = false; + iterate_list(appLogger->loggers, true, &shutdown_logger); } + + logger* create_logger(app_logger_info* appLogger, char* name) { return NULL;