dtlog
Lightweight logging with pluggable sink and lazy setup
This group of functions provides logging and message formatting for C modules. It is intended for small to medium codebases that need tagged, leveled output with minimal setup.
Mini-guide
- Initialize a logger explicitly with
dtlogger_initwhen you need control over the output sink. - Use
dtlogor the level-specific helpers to emit messages by passing a severity and tag. - Rely on lazy initialization of the default logger when no explicit setup is required.
- Attach a hook function to filter or suppress messages by returning false.
- Reset the default logger with
dtlogger_reset_defaultto force reinitialization on next use.
Example
#define TAG "example"
dtlog_info(TAG, "starting operation");
dtlog_debug(TAG, "value=%d", value);
if (error != NULL)
{
dtlog_dterr(TAG, error);
}
Data structures
dtlog_level_t
Defines the severity level associated with a log message.
dtlog_emitfunc_t
Defines the function signature used to emit a formatted log line.
dtlog_hookfunc_t
Defines the function signature used to filter log messages before emission.
dtlogger_t
Holds function pointers and state for formatting and emitting log messages.
Members:
void (*emit)(void*, const char*)Sink for fully formatted lines.
void (*format_variadic)(void*, dtlog_level_t, const char*, const char*, ...)Variadic formatting entry point.
void (*format_va_args)(void*, dtlog_level_t, const char*, const char*, va_list)Formatting entry point using ava_list.
const char* (*level_to_string)(struct dtlogger_t*, dtlog_level_t level)Converts a severity level to text.
void (*dispose)(struct dtlogger_t*)Resets the logger state without closing the file.
dtlog_hookfunc_t hookfuncOptional pre-emit hook.
void* hookfunc_contextContext pointer passed to the hook function.
FILE* fileOutput stream used by the emitter.
Macros
DTLOG_HERE
Emits a debug message that identifies the current source location.
Functions
dtlog
Emits a formatted log message at the specified severity level.
Params:
dtlog_level_tSeverity level to associate with the message.
const char* tagShort tag identifying the message source.
const char* formatprintf-style format string.
...Format arguments.
Return: void No return value.
dtlog_debug
Emits a formatted log message at debug level.
Params:
const char* tagShort tag identifying the message source.
const char* formatprintf-style format string.
...Format arguments.
Return: void No return value.
dtlog_dterr
Emits each entry of an error chain as an error-level message.
Params:
const char* tagShort tag identifying the message source.
dterr_t* dterrError chain to emit.
Return: void No return value.
dtlog_error
Emits a formatted log message at error level.
Params:
const char* tagShort tag identifying the message source.
const char* formatprintf-style format string.
...Format arguments.
Return: void No return value.
dtlog_info
Emits a formatted log message at informational level.
Params:
const char* tagShort tag identifying the message source.
const char* formatprintf-style format string.
...Format arguments.
Return: void No return value.
dtlog_va
Emits a formatted log message using an existing va_list.
Params:
dtlog_level_tSeverity level to associate with the message.
const char* tagShort tag identifying the message source.
const char* formatprintf-style format string.
va_list argsArgument list to consume.
Return: void No return value.
dtlogger_init
Initializes a logger instance for subsequent use.
Params:
dtlogger_t* thisLogger instance to initialize.
Return: dterr_t* Error object on failure or NULL on success.
dtlogger_reset_default
Resets the default logger so it will be reinitialized on next use.
Param: void No parameters.
Return: void No return value.