diff --git a/src/net/collections/list.c b/src/net/collections/list.c index f7e85ab..6b1ff38 100644 --- a/src/net/collections/list.c +++ b/src/net/collections/list.c @@ -4,37 +4,72 @@ #include "list.h" -list_node * insert_node(list_node* list, void* data) +list_node* insert_node(list_node* node, void* data) { - list_node* node = (list_node*)malloc(sizeof(list_node)); + list_node* newNode = (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; + newNode->data = data; + newNode->previous = node; + newNode->next = node->next; + node->next = newNode; + return newNode; +} - if (list != NULL) +list_node* add_node(list_node* node, void* data, const bool add_end) +{ + list_node* new_node = (list_node*)malloc(sizeof(list_node)); + if (new_node == NULL) { - if (list->next == NULL) - list->next = node; - list_node* current = list->next; - for (;;) + int error = errno; + //Failed -> log + return NULL; + } + new_node->data = data; + new_node->previous = NULL; + new_node->next = NULL; + + if (node != NULL) + { + if (add_end) { - if (current->next == NULL) + if (node->next == NULL) + node->next = new_node; + list_node* current = node->next; + for (;;) { - current->next = node; - node->previous = current; - break; + if (current->next == NULL) + { + current->next = new_node; + new_node->previous = current; + break; + } + current = current->next; + } + } + else + { + if (node->previous == NULL) + node->previous = new_node; + list_node* current = node->previous; + for (;;) + { + if (current->previous == NULL) + { + current->previous = new_node; + new_node->next = current; + break; + } + current = current->previous; } - current = current->next; } } - return node; + return new_node; } void delete_node(list_node* node) @@ -48,7 +83,7 @@ void delete_node(list_node* node) free(node); } -void iterate_list(list_node* list, bool forward, int (*iterFunc)(const void*)) +void iterate_list(list_node* list, const bool forward, iterate_state (*iterFunc)(const void*)) { if (list == NULL || iterFunc == NULL) return; @@ -59,7 +94,7 @@ void iterate_list(list_node* list, bool forward, int (*iterFunc)(const void*)) list_node* temp_previous = current->previous; // State - int iter_state = iterFunc(current->data); + iterate_state iter_state = iterFunc(current->data); if (iter_state & DELETE) delete_node(current); if (iter_state & BREAK) @@ -72,14 +107,14 @@ void iterate_list(list_node* list, bool forward, int (*iterFunc)(const void*)) } } -list_node* find_node(list_node* list_start, bool forward, int (*findFunc)(const void*)) +list_node* find_node(list_node* list_start, const bool forward, found_state (*findFunc)(const void*)) { if (list_start == NULL || findFunc == NULL) return NULL; list_node* current = list_start; while (current != NULL) { - if (findFunc(current->data) == 1) + if (findFunc(current->data) == FOUND) return current; if (forward) current = current->next; diff --git a/src/net/include/list.h b/src/net/include/list.h index fccdfd3..8b47a9f 100644 --- a/src/net/include/list.h +++ b/src/net/include/list.h @@ -7,7 +7,13 @@ typedef enum list_iterate_states CONTINUE = 0 << 0, BREAK = 1 << 0, DELETE = 1 << 1 -}iterate_state; +} iterate_state; + +typedef enum list_found_states +{ + FOUND = 0, + NOT_FOUND = 1 +} found_state; typedef struct list_node { @@ -16,7 +22,11 @@ typedef struct list_node struct list_node* next; } list_node; + + +list_node* insert_node(list_node* node, void* data); +list_node* add_node(list_node* node, void* data, bool add_end); void delete_node(list_node* node); -void iterate_list(list_node* list, bool forward, int (*iterFunc)(const void*)); +void iterate_list(list_node* list, bool forward, iterate_state (*iterFunc)(const void*)); #endif //NETEX_LIST_H diff --git a/src/net/include/log.h b/src/net/include/log.h index 1cbebdb..8720f8b 100755 --- a/src/net/include/log.h +++ b/src/net/include/log.h @@ -11,21 +11,25 @@ enum log_level Debug = 4, Verbose = 5 }; -typedef struct app_logger_info +typedef struct log_info_base { char* log_file; char* executable_name; int log_file_max_size; enum log_level max_log_level; list_node* loggers; - -} app_logger_info; +} log_base; typedef struct logger { - app_logger_info* app_logger; + log_base* app_logger; char* name; + void (*log_info)(const char* format, ...); } logger; +log_base* initialize_log_base(const char* logPath, const char* executable, int max_file_size_bytes, enum log_level maxLogLevel); +void shutdown_log(log_base* base); +logger* create_logger(log_base* base, const char* name); + #endif //LOG_H diff --git a/src/net/logging/log.c b/src/net/logging/log.c index 358aef8..271936f 100755 --- a/src/net/logging/log.c +++ b/src/net/logging/log.c @@ -1,47 +1,69 @@ #include +#include +#include #include "netex.h" #include "log.h" +bool canLog = false; -bool canLog = true; -app_logger_info* default_app_log = NULL; - -app_logger_info* initialize_log(char* logPath, char* executable, int maxFileSize, enum log_level maxLogLevel) +log_base* initialize_log_base(const char* logPath, const char* executable, const int max_file_size_bytes, const enum log_level maxLogLevel) { - return NULL; + log_base* app_log = (log_base*) malloc(sizeof(log_base)); + if (app_log == NULL) + return NULL; + app_log->log_file = strdup(logPath); + app_log->executable_name = strdup(executable); + app_log->log_file_max_size = max_file_size_bytes; + app_log->max_log_level = maxLogLevel; + canLog = true; + return app_log; } -int shutdown_logger(const void* data) +iterate_state destroy_logger(const void* data) { logger* logger = (struct logger*)data; logger->app_logger = NULL; - logger->name = NULL; + free(logger->name); free(logger); return DELETE; } -void shutdown_log(app_logger_info* appLogger) +void shutdown_log(log_base* base) { canLog = false; - iterate_list(appLogger->loggers, true, &shutdown_logger); - free(appLogger); + iterate_list(base->loggers, true, &destroy_logger); + free(base->log_file); + free(base->executable_name); + free(base); } -logger* create_logger(app_logger_info* appLogger, char* name) +logger* create_logger(log_base* base, const char* name) { - return NULL; + logger* new_logger = (logger*)malloc(sizeof(logger)); + if (new_logger == NULL) + return NULL; + new_logger->name = strdup(name); + if (new_logger->name == NULL) + { + int err = errno; + //TODO: log!? + free(new_logger); + return NULL; + } + add_node(base->loggers, new_logger, false); + return new_logger; } -void log_to_file(logger* logger, enum log_level logLevel, char* format, ...) +void log_to_file(const logger* logger, const enum log_level logLevel, const char* format, ...) { if (!canLog || logger->app_logger->max_log_level <= logLevel) return; char* logMessage; va_list args; va_start(args, format); int formatedStringSize = vasprintf(&logMessage, format, args); - if (0 > formatedStringSize) logMessage = NULL; va_end(args); + if (0 > formatedStringSize) logMessage = NULL; //TODO: Handle logging to file/queue free(logMessage); } diff --git a/src/server/main.c b/src/server/main.c index 6e2931b..adabc6b 100755 --- a/src/server/main.c +++ b/src/server/main.c @@ -6,24 +6,36 @@ // https://stackoverflow.com/questions/40812836/how-to-create-chat-server-using-openssl-in-c // https://www.cs.cmu.edu/~srini/15-441/F02/Projects/lab01/reference/part1.pdf +log_base* srv_log_base; int main(int argc, char *argv[]) { + // Setup base logger + srv_log_base = initialize_log_base("logs", "netex_server", 100000, Debug); + if (srv_log_base == NULL) + { + ERROR("Failed to initialize internal logger!"); + return 0; + } netex_init(); if (!setup_srv_curses()) { - PRINT_LINE("Failed to setup srv windows!"); + shutdown_log(srv_log_base); + PRINT_LINE("Failed to setup srv windows! Press any key to exit."); getch(); return 0; } - getch(); // For debuging! - const int init_result = initialize_server(argc, argv); - if (init_result) + getch(); // For debugging! + if (initialize_server(argc, argv)) { - WARN("Failed to initialize server. Exiting..."); - return init_result; + WARN("Failed to initialize server! Exiting..."); + netex_shutdown(); + shutdown_log(srv_log_base); + return 0; } const int srv_exec_result = server_execute(); netex_shutdown(); - return srv_exec_result; + int server_result = srv_exec_result; + shutdown_log(srv_log_base); + return server_result; } \ No newline at end of file diff --git a/src/server/server.c b/src/server/server.c index 1e19bc6..9e4078d 100755 --- a/src/server/server.c +++ b/src/server/server.c @@ -20,11 +20,13 @@ CONFIGURATION* srv_configuration; int server_sockfd; struct sockaddr_in server_sockaddr; pthread_t listen_thread; +logger* lgr; void server_listen(); int initialize_server(int argc, char* argv[]) { + lgr = create_logger(srv_log_base, "server.c"); // Config srv_configuration = config_load_from_path(CONFIG_PATH); if (srv_configuration == NULL) diff --git a/src/server/server.h b/src/server/server.h index 036e0f8..105e11b 100755 --- a/src/server/server.h +++ b/src/server/server.h @@ -1,6 +1,9 @@ #ifndef SERVER_H #define SERVER_H #include "nx_curses.h" +#include "log.h" + +extern log_base* srv_log_base; int initialize_server(int argc, char* argv[]); int server_execute(void);