LogMod is a simple logging module for C applications. It provides functionality to initialize a logging context, retrieve loggers, and log messages with different severity levels.
- Initialize logging context with application ID and logger table.
- Retrieve or create loggers by context ID.
- Log messages with different severity levels (TRACE, DEBUG, INFO, WARN, ERROR, FATAL).
- Optionally log messages to a file.
To use LogMod in your project, include the logmod.h
header file and link against the logmod.c
source file.
To initialize the logging context, use the logmod_init
function:
#include "logmod.h"
struct logmod logmod;
struct logmod_logger table[5];
logmod_err code = logmod_init(&logmod, "APPLICATION_ID", table, 5);
if (code != LOGMOD_OK) {
// Handle error
}
To retrieve or create a logger by context ID, use the logmod_logger_get
function:
struct logmod_logger *LOGMOD_LOGGER = logmod_logger_get(&logmod, "CONTEXT_ID");
if (LOGMOD_LOGGER == NULL) {
// Handle error
}
The logger is stored in the LOGMOD_LOGGER
variable, which is a macro created to ensure ANSI C compatibility.
To log messages with different severity levels, use provided macros:
LOGMOD_TRACE(("This is a trace message"));
LOGMOD_DEBUG(("This is a debug message"));
LOGMOD_INFO(("This is an info message"));
LOGMOD_WARN(("This is a warning message"));
LOGMOD_ERROR(("This is an error message"));
LOGMOD_FATAL(("This is a fatal message"));
LogMod supports formatted logging messages similar to printf
. Here are some examples:
int value = 42;
const char *name = "example";
LOGMOD_INFO(("Integer value: %d", value));
LOGMOD_DEBUG(("String value: %s", name));
LOGMOD_WARN(("Combined values: %s = %d", name, value));
These macros allow you to include variable data in your log messages, making them more informative and useful for debugging.
To clean up the logging context, use the logmod_cleanup
function:
logmod_cleanup(&logmod);
logmod_err logmod_init(struct logmod *logmod, const char *const app_id, struct logmod_logger table[], unsigned length);
Initializes the logging context with the specified application ID and logger table.
logmod
: Pointer to the logging context structure.app_id
: Application ID string.table
: Logger table array.length
: Length of the logger table array.
Returns LOGMOD_OK
on success, or an error code on failure.
logmod_err logmod_cleanup(struct logmod *logmod);
Cleans up the logging context.
logmod
: Pointer to the logging context structure. ReturnsLOGMOD_OK
on success
struct logmod_logger *logmod_logger_get(struct logmod *logmod, const char *const context_id);
Retrieves or creates a logger by context ID.
logmod
: Pointer to the logging context structure.context_id
: Context ID string. Returns a pointer to the logger, orNULL
on failure. Note: The variable MUST be calledLOGMOD_LOGGER
(which is a macro created to ensure ANSI C compatibility).
logmod_err logmod_logger_set_lock(struct logmod_logger *logger, void (*lock)(int should_lock));
Sets the lock function for the specified logger.
logger
: Pointer to the logger structure.lock
: Lock function pointer.
logmod_err logmod_logger_set_options(struct logmod_logger *logger, struct logmod_logger_options options);
Sets the options for the logger.
logger
: Pointer to the logger structure.options
: Logger options structure.
logmod_err logmod_logger_set_quiet(struct logmod_logger *logger, int quiet);
Sets the quiet mode for the logger.
logger
: Pointer to the logger structure.quiet
: Quiet mode flag. ReturnsLOGMOD_OK
on success.
logmod_err logmod_logger_set_logfile(struct logmod_logger *logger, char *logfile);
Sets the logfile for the logger.
logger
: Pointer to the logger structure.logfile
: Logfile path string. ReturnsLOGMOD_OK
on success.
logmod_err _logmod_log(struct logmod_logger *logger, const char *fmt, ...);
Logs a message with the specified logger and format string.
logger
: Pointer to the logger structure.fmt
: Format string for the message. ReturnsLOGMOD_OK
on success, or an error code on failure.
This project is licensed under the MIT License - see the LICENSE file for details.