Implement logger functionality and signal handling: add Logger structure with mutex, logging functions, and handle SIGINT in utils.
This commit is contained in:
@@ -8,8 +8,8 @@
|
||||
|
||||
#include "main.h"
|
||||
#include "users.h"
|
||||
#include "utils.h"
|
||||
#include <string.h>
|
||||
#include <libpq-fe.h>
|
||||
|
||||
int loadListFromCSV(char* _filename, list_t * _list);
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
#ifndef __LOGGER_H__
|
||||
#define __LOGGER_H__
|
||||
|
||||
#include "main.h"
|
||||
#include "rfid_handler.h"
|
||||
#include "utils.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
pthread_mutex_t lock;
|
||||
FILE *f_log;
|
||||
} Logger;
|
||||
|
||||
Logger *initLogger(const char *_filename);
|
||||
int logErrorEvent(Logger *_logger, const char *_message);
|
||||
int logWarningEvent(Logger *_logger, const char *_message);
|
||||
int logHardwareEvent(Logger *_logger, const char *_event_origin, const char *_message);
|
||||
int logInfoEvent(Logger *_logger, const char *_message);
|
||||
int endLogger(Logger *_logger);
|
||||
|
||||
#endif
|
||||
+21
-3
@@ -1,3 +1,23 @@
|
||||
/**
|
||||
* @file main.h
|
||||
* @brief Main header file for the application.
|
||||
*
|
||||
* This header includes all necessary standard and project-specific headers,
|
||||
* defines basic macros and types, and declares the main entry point of the program.
|
||||
*
|
||||
* Included headers:
|
||||
* - Standard C libraries (stdio, stdlib, stdint, time, unistd, pthread, ncurses, fcntl, errno, termios, string, signal)
|
||||
* - Project-specific modules (utils, logger, users, tui, rfid_handler, file_handler)
|
||||
*
|
||||
* Macros:
|
||||
* - TRUE, FALSE: Boolean values for logic operations.
|
||||
* - BYTE: Unsigned 8-bit integer type alias.
|
||||
* - MIN_X_TERMINAL_SIZE, MIN_Y_TERMINAL_SIZE: Minimum terminal dimensions for UI.
|
||||
*
|
||||
* Function Declarations:
|
||||
* - int main(int argc, char const **argv): Program entry point.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
@@ -14,7 +34,6 @@
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
#ifndef __MAIN_H__
|
||||
#define __MAIN_H__
|
||||
@@ -34,9 +53,8 @@
|
||||
#define MIN_X_TERMINAL_SIZE 50
|
||||
#define MIN_Y_TERMINAL_SIZE 20
|
||||
|
||||
void handle_sigint(int sig);
|
||||
#define JOURNAL_FILENAME "lighttam_journal"
|
||||
|
||||
int main(int argc, char const **argv);
|
||||
|
||||
|
||||
#endif
|
||||
@@ -7,4 +7,5 @@ void printTimeInString(time_t _input, char * _output);
|
||||
|
||||
void printDateAndTimeInString(time_t _input, char * _output);
|
||||
|
||||
void handle_sigint(int sig);
|
||||
#endif
|
||||
@@ -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