DungeonCrawl
Loading...
Searching...
No Matches
logger.h File Reference

Header file for logging functionality of the game. More...

Go to the source code of this file.

Macros

#define DEBUG_STATE   1
#define DEBUG_LOG(modul, format, ...)
#define RETURN_WHEN_NULL(ptr, ret, modul, format, ...)
#define RETURN_WHEN_TRUE(expr, ret, modul, format, ...)

Enumerations

enum  log_level_t {
  DEBUG , FINE , INFO , WARNING ,
  ERROR , MAX_LOG_LEVEL
}

Functions

void init_logger (void)
 Initializes the logging system for the application.
void log_msg (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.

Detailed Description

Header file for logging functionality of the game.

Definition in file logger.h.

Macro Definition Documentation

◆ DEBUG_LOG

#define DEBUG_LOG ( modul,
format,
... )
Value:
if (DEBUG_STATE == 1) { \
log_msg(DEBUG, modul, format, ##__VA_ARGS__); \
}

Definition at line 11 of file logger.h.

11#define DEBUG_LOG(modul, format, ...) \
12 if (DEBUG_STATE == 1) { \
13 log_msg(DEBUG, modul, format, ##__VA_ARGS__); \
14 }

◆ DEBUG_STATE

#define DEBUG_STATE   1

Definition at line 9 of file logger.h.

◆ RETURN_WHEN_NULL

#define RETURN_WHEN_NULL ( ptr,
ret,
modul,
format,
... )
Value:
if (ptr == NULL) { \
log_msg(ERROR, modul, format, ##__VA_ARGS__); \
return ret; \
}

Definition at line 16 of file logger.h.

16#define RETURN_WHEN_NULL(ptr, ret, modul, format, ...) \
17 if (ptr == NULL) { \
18 log_msg(ERROR, modul, format, ##__VA_ARGS__); \
19 return ret; \
20 }

◆ RETURN_WHEN_TRUE

#define RETURN_WHEN_TRUE ( expr,
ret,
modul,
format,
... )
Value:
if (expr) { \
log_msg(ERROR, modul, format, ##__VA_ARGS__); \
return ret; \
}

Definition at line 22 of file logger.h.

22#define RETURN_WHEN_TRUE(expr, ret, modul, format, ...) \
23 if (expr) { \
24 log_msg(ERROR, modul, format, ##__VA_ARGS__); \
25 return ret; \
26 }

Enumeration Type Documentation

◆ log_level_t

enum log_level_t

Definition at line 28 of file logger.h.

28 {
29 DEBUG,
30 FINE,
31 INFO,
32 WARNING,
33 ERROR,
34 MAX_LOG_LEVEL
35} log_level_t;

Function Documentation

◆ init_logger()

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:

  • This function can only be called once during the application's lifecycle. Subsequent calls will have no effect if the logger is already initialized.
  • All required resources for logging, such as the log file and ring buffer, are prepared within this initialization process.
  • If any of the initialization steps fail (e.g., ring buffer initialization or file operations), the function ensures cleanup of allocated resources.

Definition at line 232 of file logger.c.

232 {
233 // ensures the init_logger can only be called when the file is null
234 if (log_file == NULL) {
235 if (init_ringbuffer(&log_buffer) == 0) {
236 file_id = get_latest_file_id();
237 if (file_id == -1 || open_log_file(0) != 0) {
238 fclose(log_file);
239 } else {
241 }
242 }
243 }
244}
int open_log_file(int is_init)
Opens the log file with the current saved file id in appended modus.
Definition logger.c:117
int get_latest_file_id(void)
This function gets the latest file id from the log directory.
Definition logger.c:162
void start_log_writer_thread(void)
Starts the log writer thread.
Definition logger.c:201

◆ log_msg()

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:

  • The logging system must be initialized and running before using this function. Otherwise, the function will return without performing any operations.
  • The log level determines the severity or importance of the message.
Parameters
levelThe 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.
moduleA string identifying the module or component generating the log message.
formatA 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.

246 {
247 if (log_file == NULL || !logger_is_running) {
248 // logger is not initialized or not running
249 return;
250 }
251
252 //get timestamp
253 const time_t now = time(NULL);
254 const struct tm* tm = localtime(&now);
255
256 char timestamp[32];
257 strftime(timestamp, sizeof(timestamp), TIMESTAMP_FORMAT, tm);
258
259 const char* log_level;
260 if (level >= MAX_LOG_LEVEL) {
261 log_level = log_level_str[INFO];
262 } else {
263 log_level = log_level_str[level];
264 }
265
266 va_list args;
267 va_start(args, format);
268 char msg[MAX_MSG_SIZE - MAX_HEADER_SIZE];
269 vsnprintf(msg, sizeof(msg), format, args);
270
271 char log_msg[MAX_MSG_SIZE];
272 snprintf(log_msg, MAX_MSG_SIZE, MSG_FORMAT, timestamp, log_level, module, msg);
273 va_end(args);
274
275 write_to_ringbuffer(&log_buffer, log_msg);
276}
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.
Definition logger.c:246

◆ shutdown_logger()

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:

  • Once this function is called, logging within the application will cease to function.
  • This function should be called after all dependent systems and processes using the logger are finalized.

Definition at line 278 of file logger.c.

278 {
279 logger_is_running = false;
280}