Implement logger functionality and signal handling: add Logger structure with mutex, logging functions, and handle SIGINT in utils.
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
#include "logger.h"
|
||||
|
||||
Logger *initLogger(const char *_filename)
|
||||
{
|
||||
Logger *logger_struct = malloc(sizeof(Logger));
|
||||
if (logger_struct == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
logger_struct->f_log = fopen(_filename, "w");
|
||||
if (logger_struct->f_log == NULL)
|
||||
{
|
||||
free(logger_struct);
|
||||
return NULL;
|
||||
}
|
||||
pthread_mutex_init(&logger_struct->lock, NULL);
|
||||
}
|
||||
|
||||
int logErrorEvent(Logger *_logger, const char *_message)
|
||||
{
|
||||
if (_logger == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
char current_time_str[32];
|
||||
printDateAndTimeInString(time(NULL), current_time_str);
|
||||
|
||||
fprintf(_logger->f_log, "%s [ERROR] %s", current_time_str, _message);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int logWarningEvent(Logger *_logger, const char *_message)
|
||||
{
|
||||
if (_logger == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
char current_time_str[32];
|
||||
printDateAndTimeInString(time(NULL), current_time_str);
|
||||
|
||||
fprintf(_logger->f_log, "%s [WARNING] %s", current_time_str, _message);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int logHardwareEvent(Logger *_logger, const char *_event_origin, const char *_message)
|
||||
{
|
||||
if (_logger == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
char current_time_str[32];
|
||||
printDateAndTimeInString(time(NULL), current_time_str);
|
||||
|
||||
fprintf(_logger->f_log, "%s [%s] %s", current_time_str, _event_origin, _message);
|
||||
return 1;
|
||||
}
|
||||
int logInfoEvent(Logger *_logger, const char *_message)
|
||||
{
|
||||
if (_logger == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
char current_time_str[32];
|
||||
printDateAndTimeInString(time(NULL), current_time_str);
|
||||
|
||||
fprintf(_logger->f_log, "%s [INFO] %s", current_time_str, _message);
|
||||
return 1;
|
||||
}
|
||||
int endLogger(Logger *_logger)
|
||||
{
|
||||
fclose(_logger->f_log);
|
||||
free(_logger);
|
||||
_logger = NULL;
|
||||
return 0;
|
||||
}
|
||||
+3
-7
@@ -1,16 +1,12 @@
|
||||
#include "main.h"
|
||||
|
||||
#include "utils.h"
|
||||
#include "logger.h"
|
||||
#include "users.h"
|
||||
#include "tui.h"
|
||||
#include "rfid_handler.h"
|
||||
#include "file_handler.h"
|
||||
|
||||
void handle_sigint(int sig)
|
||||
{
|
||||
endwin();
|
||||
clear();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int main(int argc, char const **argv)
|
||||
{
|
||||
signal(SIGINT, handle_sigint);
|
||||
|
||||
@@ -26,3 +26,10 @@ void printDateAndTimeInString(time_t _input, char *_output)
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void handle_sigint(int sig)
|
||||
{
|
||||
endwin();
|
||||
clear();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user