DungeonCrawl
|
Implements logging functionality for the game. More...
#include "logger.h"
#include "../thread/thread_handler.h"
#include "logger_config.h"
#include "ringbuffer.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
#include <dirent.h>
Go to the source code of this file.
Macros | |
#define | STAT_STRUCT struct stat |
#define | STAT_FUNC stat |
#define | MKDIR(path) |
#define | PATH_SEP "/" |
#define | MAX_PATH_SIZE 4096 |
#define | LOG_DIRECTORY "log" |
#define | LOG_FILE_FORMAT "log-%d.txt" |
#define | TIMESTAMP_FORMAT "%Y-%m-%d %H:%M:%S" |
#define | MSG_FORMAT "[%s] [%s] [%s] : %s\n" |
Functions | |
int | ensure_log_dir (void) |
Ensures that the predefined log directory already exists, if not create a new one. | |
int | open_log_file (int is_init) |
Opens the log file with the current saved file id in appended modus. | |
int | check_log_file (void) |
This function should be called whenever a new log msg must be written. | |
int | get_latest_file_id (void) |
This function gets the latest file id from the log directory. | |
void | start_log_writer_thread (void) |
Starts the log writer thread. | |
void | log_writer_thread (void) |
This function will be called from a different thread to read from the ringbuffer and then write in the log file. | |
void | init_logger (void) |
Initializes the logging system for the application. | |
void | log_msg (const log_level_t level, const char *module, const char *format,...) |
Logs a formatted message with a specified log level and module. | |
void | shutdown_logger (void) |
Shuts down the logging system for the application. |
Variables | |
const char * | log_level_str [] = {"DEBUG", "FINE", "INFO", "WARNING", "ERROR"} |
FILE * | log_file = NULL |
ring_buffer_t | log_buffer |
bool | logger_is_running = false |
int | file_id = 0 |
Implements logging functionality for the game.
Definition in file logger.c.
int check_log_file | ( | void | ) |
This function should be called whenever a new log msg must be written.
Check if the log file is open, if not, a new file will be open.
Definition at line 144 of file logger.c.
int ensure_log_dir | ( | void | ) |
Ensures that the predefined log directory already exists, if not create a new one.
Definition at line 105 of file logger.c.
int get_latest_file_id | ( | void | ) |
This function gets the latest file id from the log directory.
Definition at line 162 of file logger.c.
void init_logger | ( | void | ) |
Initializes the logging system for the application.
This function sets up the logging system to be ready for recording log entries. It creates or opens the log file, initializes the ring buffer used for storing log entries temporarily, and starts the thread that asynchronously writes logs to the file. This ensures logging is operational and ready to use for the application's lifetime.
Notes:
Definition at line 232 of file logger.c.
void log_msg | ( | log_level_t | level, |
const char * | module, | ||
const char * | format, | ||
... ) |
Logs a formatted message with a specified log level and module.
This function generates a log entry that includes a timestamp, log level, module name, and a custom message formatted using a variable argument list. The log message is written to a ring buffer for asynchronous processing.
Notes:
level | The severity level of the log message (e.g., DEBUG, INFO, ERROR). If the provided log level exceeds the maximum defined levels, the INFO level will be used as a fallback. |
module | A string identifying the module or component generating the log message. |
format | A printf-style format string for the message content. |
... | Additional arguments to be formatted into the log message according to the format string. |
Definition at line 246 of file logger.c.
void log_writer_thread | ( | void | ) |
This function will be called from a different thread to read from the ringbuffer and then write in the log file.
Definition at line 207 of file logger.c.
int open_log_file | ( | int | is_init | ) |
Opens the log file with the current saved file id in appended modus.
If the file already exists, it will be first removed and then created. If no file is found, create a new file corresponding to fopen(...).
is_init | if 0, no existing file will be removed |
Definition at line 117 of file logger.c.
void shutdown_logger | ( | void | ) |
Shuts down the logging system for the application.
This function terminates the logging system, ensuring that no further log entries are recorded. It sets the logger's running state to false, effectively disabling any ongoing or future logging operations.
Use this function to cleanly release logging resources and mark the completion of logging operations at the end of the application's lifecycle or during application shutdown sequences.
Notes:
Definition at line 278 of file logger.c.
void start_log_writer_thread | ( | void | ) |
Starts the log writer thread.
This function will be called from the logger initialization function.
Definition at line 201 of file logger.c.
ring_buffer_t log_buffer |
const char* log_level_str[] = {"DEBUG", "FINE", "INFO", "WARNING", "ERROR"} |