mirror of
https://github.com/hmaxnl/netex.git
synced 2025-01-18 23:44:20 +01:00
[CHANGE] Base implementation logger
This commit is contained in:
parent
91b07bf433
commit
b536709fb5
|
@ -4,37 +4,72 @@
|
||||||
|
|
||||||
#include "list.h"
|
#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)
|
if (node == NULL)
|
||||||
{
|
{
|
||||||
int error = errno;
|
int error = errno;
|
||||||
//Failed -> log
|
//Failed -> log
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
node->data = data;
|
newNode->data = data;
|
||||||
node->previous = NULL;
|
newNode->previous = node;
|
||||||
node->next = NULL;
|
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)
|
int error = errno;
|
||||||
list->next = node;
|
//Failed -> log
|
||||||
list_node* current = list->next;
|
return NULL;
|
||||||
|
}
|
||||||
|
new_node->data = data;
|
||||||
|
new_node->previous = NULL;
|
||||||
|
new_node->next = NULL;
|
||||||
|
|
||||||
|
if (node != NULL)
|
||||||
|
{
|
||||||
|
if (add_end)
|
||||||
|
{
|
||||||
|
if (node->next == NULL)
|
||||||
|
node->next = new_node;
|
||||||
|
list_node* current = node->next;
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
if (current->next == NULL)
|
if (current->next == NULL)
|
||||||
{
|
{
|
||||||
current->next = node;
|
current->next = new_node;
|
||||||
node->previous = current;
|
new_node->previous = current;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
current = current->next;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return node;
|
return new_node;
|
||||||
}
|
}
|
||||||
|
|
||||||
void delete_node(list_node* node)
|
void delete_node(list_node* node)
|
||||||
|
@ -48,7 +83,7 @@ void delete_node(list_node* node)
|
||||||
free(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)
|
if (list == NULL || iterFunc == NULL)
|
||||||
return;
|
return;
|
||||||
|
@ -59,7 +94,7 @@ void iterate_list(list_node* list, bool forward, int (*iterFunc)(const void*))
|
||||||
list_node* temp_previous = current->previous;
|
list_node* temp_previous = current->previous;
|
||||||
|
|
||||||
// State
|
// State
|
||||||
int iter_state = iterFunc(current->data);
|
iterate_state iter_state = iterFunc(current->data);
|
||||||
if (iter_state & DELETE)
|
if (iter_state & DELETE)
|
||||||
delete_node(current);
|
delete_node(current);
|
||||||
if (iter_state & BREAK)
|
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)
|
if (list_start == NULL || findFunc == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
list_node* current = list_start;
|
list_node* current = list_start;
|
||||||
while (current != NULL)
|
while (current != NULL)
|
||||||
{
|
{
|
||||||
if (findFunc(current->data) == 1)
|
if (findFunc(current->data) == FOUND)
|
||||||
return current;
|
return current;
|
||||||
if (forward)
|
if (forward)
|
||||||
current = current->next;
|
current = current->next;
|
||||||
|
|
|
@ -7,7 +7,13 @@ typedef enum list_iterate_states
|
||||||
CONTINUE = 0 << 0,
|
CONTINUE = 0 << 0,
|
||||||
BREAK = 1 << 0,
|
BREAK = 1 << 0,
|
||||||
DELETE = 1 << 1
|
DELETE = 1 << 1
|
||||||
}iterate_state;
|
} iterate_state;
|
||||||
|
|
||||||
|
typedef enum list_found_states
|
||||||
|
{
|
||||||
|
FOUND = 0,
|
||||||
|
NOT_FOUND = 1
|
||||||
|
} found_state;
|
||||||
|
|
||||||
typedef struct list_node
|
typedef struct list_node
|
||||||
{
|
{
|
||||||
|
@ -16,7 +22,11 @@ typedef struct list_node
|
||||||
struct list_node* next;
|
struct list_node* next;
|
||||||
} list_node;
|
} 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 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
|
#endif //NETEX_LIST_H
|
||||||
|
|
|
@ -11,21 +11,25 @@ enum log_level
|
||||||
Debug = 4,
|
Debug = 4,
|
||||||
Verbose = 5
|
Verbose = 5
|
||||||
};
|
};
|
||||||
typedef struct app_logger_info
|
typedef struct log_info_base
|
||||||
{
|
{
|
||||||
char* log_file;
|
char* log_file;
|
||||||
char* executable_name;
|
char* executable_name;
|
||||||
int log_file_max_size;
|
int log_file_max_size;
|
||||||
enum log_level max_log_level;
|
enum log_level max_log_level;
|
||||||
list_node* loggers;
|
list_node* loggers;
|
||||||
|
} log_base;
|
||||||
} app_logger_info;
|
|
||||||
|
|
||||||
typedef struct logger
|
typedef struct logger
|
||||||
{
|
{
|
||||||
app_logger_info* app_logger;
|
log_base* app_logger;
|
||||||
char* name;
|
char* name;
|
||||||
|
void (*log_info)(const char* format, ...);
|
||||||
} logger;
|
} 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
|
#endif //LOG_H
|
||||||
|
|
|
@ -1,47 +1,69 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
#include "netex.h"
|
#include "netex.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
|
bool canLog = false;
|
||||||
|
|
||||||
bool canLog = true;
|
log_base* initialize_log_base(const char* logPath, const char* executable, const int max_file_size_bytes, const enum log_level maxLogLevel)
|
||||||
app_logger_info* default_app_log = NULL;
|
|
||||||
|
|
||||||
app_logger_info* initialize_log(char* logPath, char* executable, int maxFileSize, enum log_level maxLogLevel)
|
|
||||||
{
|
{
|
||||||
|
log_base* app_log = (log_base*) malloc(sizeof(log_base));
|
||||||
|
if (app_log == NULL)
|
||||||
return 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* logger = (struct logger*)data;
|
||||||
logger->app_logger = NULL;
|
logger->app_logger = NULL;
|
||||||
logger->name = NULL;
|
free(logger->name);
|
||||||
free(logger);
|
free(logger);
|
||||||
return DELETE;
|
return DELETE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void shutdown_log(app_logger_info* appLogger)
|
void shutdown_log(log_base* base)
|
||||||
{
|
{
|
||||||
canLog = false;
|
canLog = false;
|
||||||
iterate_list(appLogger->loggers, true, &shutdown_logger);
|
iterate_list(base->loggers, true, &destroy_logger);
|
||||||
free(appLogger);
|
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)
|
||||||
{
|
{
|
||||||
|
logger* new_logger = (logger*)malloc(sizeof(logger));
|
||||||
|
if (new_logger == NULL)
|
||||||
return 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;
|
if (!canLog || logger->app_logger->max_log_level <= logLevel) return;
|
||||||
char* logMessage;
|
char* logMessage;
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, format);
|
va_start(args, format);
|
||||||
int formatedStringSize = vasprintf(&logMessage, format, args);
|
int formatedStringSize = vasprintf(&logMessage, format, args);
|
||||||
if (0 > formatedStringSize) logMessage = NULL;
|
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
if (0 > formatedStringSize) logMessage = NULL;
|
||||||
//TODO: Handle logging to file/queue
|
//TODO: Handle logging to file/queue
|
||||||
free(logMessage);
|
free(logMessage);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,24 +6,36 @@
|
||||||
// https://stackoverflow.com/questions/40812836/how-to-create-chat-server-using-openssl-in-c
|
// 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
|
// 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[])
|
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();
|
netex_init();
|
||||||
if (!setup_srv_curses())
|
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();
|
getch();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
getch(); // For debuging!
|
getch(); // For debugging!
|
||||||
const int init_result = initialize_server(argc, argv);
|
if (initialize_server(argc, argv))
|
||||||
if (init_result)
|
|
||||||
{
|
{
|
||||||
WARN("Failed to initialize server. Exiting...");
|
WARN("Failed to initialize server! Exiting...");
|
||||||
return init_result;
|
netex_shutdown();
|
||||||
|
shutdown_log(srv_log_base);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
const int srv_exec_result = server_execute();
|
const int srv_exec_result = server_execute();
|
||||||
netex_shutdown();
|
netex_shutdown();
|
||||||
return srv_exec_result;
|
int server_result = srv_exec_result;
|
||||||
|
shutdown_log(srv_log_base);
|
||||||
|
return server_result;
|
||||||
}
|
}
|
|
@ -20,11 +20,13 @@ CONFIGURATION* srv_configuration;
|
||||||
int server_sockfd;
|
int server_sockfd;
|
||||||
struct sockaddr_in server_sockaddr;
|
struct sockaddr_in server_sockaddr;
|
||||||
pthread_t listen_thread;
|
pthread_t listen_thread;
|
||||||
|
logger* lgr;
|
||||||
|
|
||||||
void server_listen();
|
void server_listen();
|
||||||
|
|
||||||
int initialize_server(int argc, char* argv[])
|
int initialize_server(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
|
lgr = create_logger(srv_log_base, "server.c");
|
||||||
// Config
|
// Config
|
||||||
srv_configuration = config_load_from_path(CONFIG_PATH);
|
srv_configuration = config_load_from_path(CONFIG_PATH);
|
||||||
if (srv_configuration == NULL)
|
if (srv_configuration == NULL)
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
#ifndef SERVER_H
|
#ifndef SERVER_H
|
||||||
#define SERVER_H
|
#define SERVER_H
|
||||||
#include "nx_curses.h"
|
#include "nx_curses.h"
|
||||||
|
#include "log.h"
|
||||||
|
|
||||||
|
extern log_base* srv_log_base;
|
||||||
|
|
||||||
int initialize_server(int argc, char* argv[]);
|
int initialize_server(int argc, char* argv[]);
|
||||||
int server_execute(void);
|
int server_execute(void);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user