Thread-safe logger library for debugging purposes in C99. More...
#include <ayaztub/utils/util_attributes.h>#include <stdbool.h>#include <stdio.h>#include <stdlib.h>Macros | |
| #define | SOURCE_PATH_SIZE 0 |
| Macro to truncate the root file path. | |
| #define | __FILENAME__ ((__FILE__) + (SOURCE_PATH_SIZE)) |
| Macro to adjust the file path based on SOURCE_PATH_SIZE. | |
| #define | LOG(lvl, ...) |
| Logs a message using the default log macro. | |
| #define | LOG_FATAL(...) |
| Logs a fatal message and exits the program. | |
Typedefs | |
| typedef void(* | logger_cb_t) (enum log_level lvl, const char *const colored_message, const char *const raw_message) |
| Logger callback type. | |
Enumerations | |
| enum | log_level { LOG_QUITE , LOG_FATAL , LOG_ERROR , LOG_TIMEOUT , LOG_WARN , LOG_INFO , LOG_TRACE , LOG_DEBUG , LOG_FULL } |
| Log levels supported by the logger. More... | |
Functions | |
| CONSTRUCTOR void | logger_init (void) |
| Initializes the logger. | |
| DESTRUCTOR void | logger_deinit (void) |
| Deinitializes the logger. | |
| void | logger_set_format_options (bool show_date, bool show_thread, bool log_trace_on_fatal) |
| Configures log formatting options. | |
| void | logger_set_log_level (enum log_level level) |
| Sets the current log level. | |
| void | logger_set_log_level_from_string (const char *const log_level) NONNULL NULL_TERMINATED_STRING_ARG(1) |
| Sets the log level based on a string. | |
| void | logger_set_log_level_from_env (void) |
| Sets the log level based on an environment variable. | |
| bool | logger_set_log_file (const char *const filename) NONNULL NULL_TERMINATED_STRING_ARG(1) |
| Sets the output file for log messages. | |
| bool | logger_set_log_file_from_env (const char *const default_filename) |
| Sets the output file for log messages based on an environment variable. | |
| bool | logger_set_log_fileno (FILE *file) NONNULL FD_ARG_WRITE(1) |
| Sets the file descriptor for log output. | |
| void | logger_close_file (void) |
| Closes the current log file, if any. | |
| void | logger_set_callback (logger_cb_t callback) |
| Sets a callback function to handle log messages. | |
| void | log_message (enum log_level level, const char *const file, size_t line, const char *const func, const char *const fmt,...) NONNULL NULL_TERMINATED_STRING_ARG(2) NULL_TERMINATED_STRING_ARG(4) |
| Logs a message with a specified log level. | |
| void | log_on_stdout (enum log_level, const char *const, const char *const) NONNULL NULL_TERMINATED_STRING_ARG(2) NULL_TERMINATED_STRING_ARG(3) |
| Logs a message to stdout using the logger callback. | |
| void | log_on_stderr (enum log_level, const char *const, const char *const) NONNULL NULL_TERMINATED_STRING_ARG(2) NULL_TERMINATED_STRING_ARG(3) |
| Logs a message to stderr using the logger callback. | |
Thread-safe logger library for debugging purposes in C99.
This library provides logging functionality with various log levels, support for callbacks, and options to log to files or standard output. It is designed for debugging and includes features like backtrace logging on fatal errors.
./a.out(+0x21ae) [0x5620024571ae] can be retrieved using addr2line: addr2line -Cfspe ./a.out +0x21ae gives us the function, file and line (with debug flags -g)Usage example:
| #define __FILENAME__ ((__FILE__) + (SOURCE_PATH_SIZE)) |
Macro to adjust the file path based on SOURCE_PATH_SIZE.
This macro modifies the __FILE__ macro to remove a specified number of characters from the beginning, making the logged file paths more readable.
| #define LOG | ( | lvl, | |
| ... ) |
Logs a message using the default log macro.
Usage:
| lvl | Log level. |
| ... | Format string and arguments. |
| #define LOG_FATAL | ( | ... | ) |
Logs a fatal message and exits the program.
This macro is intended to exit the program even if logging is disabled (with NOLOG defined).
| ... | Format string and arguments. |
| #define SOURCE_PATH_SIZE 0 |
Macro to truncate the root file path.
This macro is useful when the project is compiled with CMake, allowing for a cleaner file path in logs by removing a fixed number of characters from the start of the __FILE__ macro. The default value if the macro wasn't defined earlier is 0.
| typedef void(* logger_cb_t) (enum log_level lvl, const char *const colored_message, const char *const raw_message) |
Logger callback type.
This function is called whenever a log message is generated. It must not call logger functions/macros directly to avoid recursion.
| lvl | Log level of the message. |
| colored_message | Log message with formatting (for TTY outputs like stdout). |
| raw_message | Log message without formatting (for file outputs). |
| enum log_level |
| void log_message | ( | enum log_level | level, |
| const char *const | file, | ||
| size_t | line, | ||
| const char *const | func, | ||
| const char *const | fmt, | ||
| ... ) |
Logs a message with a specified log level.
| level | Log level for the message. |
| file | Source file name (FILE). |
| line | Source line number (LINE). |
| func | Source function name (func). |
| fmt | Format string for the message. |
| ... | Additional arguments for the format string. |
| void log_on_stderr | ( | enum | log_level, |
| const char * const | , | ||
| const char * const | ) |
Logs a message to stderr using the logger callback.
| void log_on_stdout | ( | enum | log_level, |
| const char * const | , | ||
| const char * const | ) |
Logs a message to stdout using the logger callback.
| void logger_close_file | ( | void | ) |
Closes the current log file, if any.
| DESTRUCTOR void logger_deinit | ( | void | ) |
Deinitializes the logger.
This function is called automatically at program termination (destructor attribute). It must not be called directly.
| CONSTRUCTOR void logger_init | ( | void | ) |
Initializes the logger.
This function is called automatically at program startup (constructor attribute). It must not be called directly.
| void logger_set_callback | ( | logger_cb_t | callback | ) |
Sets a callback function to handle log messages.
| callback | Pointer to the callback function. |
| void logger_set_format_options | ( | bool | show_date, |
| bool | show_thread, | ||
| bool | log_trace_on_fatal ) |
Configures log formatting options.
| show_date | Whether to include the current date in log messages. |
| show_thread | Whether to include thread identifiers in log messages. |
| log_trace_on_fatal | Whether to log a backtrace on fatal errors or signals. |
| bool logger_set_log_file | ( | const char *const | filename | ) |
Sets the output file for log messages.
| filename | Path to the log file. |
true if the file was successfully opened, false otherwise. | bool logger_set_log_file_from_env | ( | const char *const | default_filename | ) |
Sets the output file for log messages based on an environment variable.
The logger will look for the environment variable LOG_FILE and configure the output file accordingly.
| default_filename | Path to the log path if the environment variable was not found. (can be NULL) |
true if environment variable LOG_FILE exists and was successfully opened OR if default_filename is not NULL and was successfully opened, false otherwise (1- LOG_FILE env variable exists but file cannot be opened, 2- LOG_FILE env variable does not exists and default_filename is NULL, 3- LOG_FILE env variable does not exists and default_filename cannot be opened) | bool logger_set_log_fileno | ( | FILE * | file | ) |
Sets the file descriptor for log output.
| file | A valid file pointer for output. |
true if the file descriptor is valid, false otherwise.| void logger_set_log_level | ( | enum log_level | level | ) |
Sets the current log level.
Messages below the specified level will not be logged.
| level | The desired log level. |
| void logger_set_log_level_from_env | ( | void | ) |
Sets the log level based on an environment variable.
The logger will look for the environment variable LOG_LEVEL and configure the level accordingly. The environment variable LOG_LEVEL value must follow the same format as the string parameter accepted by logger_set_log_level_from_string().
| void logger_set_log_level_from_string | ( | const char *const | log_level | ) |
Sets the log level based on a string.
| log_level | String representation of the log level (e.g., "INFO", "DEBUG", "LOG_INFO", "LOG_DEBUG"). |