C-libs 0.1.0
Some C utils libraries
 
Loading...
Searching...
No Matches
example_logger.c
#include <ayaztub/logger.h>
static void segv_func(int *ptr) {
if (!ptr)
LOG_FATAL("ptr is NULL...");
*ptr = 0;
}
int main(void) {
// Initialize the logger (happens automatically via constructor)
logger_set_format_options(true, true, true);
// If LOG_LEVEL env variable not found or invalid, th log_level is not
// modified from the previous set.
// Set logging to a file
if (!logger_set_log_file_from_env("logfile.txt")) {
fprintf(stderr, "Failed to open log file\n");
return 1;
}
// Log messages of various levels
LOG(LOG_INFO, "This is an informational message.");
LOG(LOG_WARN, "This is a warning message.");
LOG(LOG_ERROR, "This is an error message.");
LOG(LOG_DEBUG, "Debugging details: x=%d, y=%d", 69, 96);
// Demonstrate callback usage (log to stdout)
LOG(LOG_TRACE, "Trace message with callback active.");
// Demonstrate backtrace logging on fatal errors / signals
segv_func(NULL);
// Clean up (happens automatically via destructor)
// logger_close_file();
return 0;
}
Thread-safe logger library for debugging purposes in C99.
void logger_set_format_options(bool show_date, bool show_thread, bool log_trace_on_fatal)
Configures log formatting options.
bool logger_set_log_file_from_env(const char *const default_filename)
Sets the output file for log messages based on an environment variable.
void logger_set_log_level(enum log_level level)
Sets the current log level.
#define LOG_FATAL(...)
Logs a fatal message and exits the program.
Definition logger.h:318
void logger_set_log_level_from_env(void)
Sets the log level based on an environment variable.
#define LOG(lvl,...)
Logs a message using the default log macro.
Definition logger.h:306
void logger_set_callback(logger_cb_t callback)
Sets a callback function to handle log messages.
@ LOG_ERROR
Definition logger.h:120
@ LOG_TRACE
Definition logger.h:124
@ LOG_INFO
Definition logger.h:123
@ LOG_DEBUG
Definition logger.h:125
@ LOG_WARN
Definition logger.h:122
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.