Logging Library#

Header-only log library.

Just macros that evaluate to fprintf calls, nothing more.

Configuration

These settings can be overridden using the compiler’s -D flag or using a #define before #include

LOG_FILE#

Choose the file to output logs to.

What is expected is a FILE* like stdout or stderr.

Defaults to stderr

LOG_LEVEL#

Choose the compile-time log verbosity level.

Logs that are strictly less critical that this level are removed before compilation.

The default value is LOG_LEVEL_TRACE for debug builds and LOG_LEVEL_INFO for release builds that define NDEBUG.

LOG_FORMAT#

Choose the logs output format.

Defaults to LOG_FORMAT_COLOR

typedef void (*logging_callback)(unsigned level, const char *level_name, const char *file, const char *function, int line, const char *message, ...)#

Expected signature of the function designated by the LOG_FUNCTION macro.

If users want to be able to capture all logs, to have fancy features whose cost isn’t paid by default, the special LOG_FUNCTION macro can be set to a function of this type. All logs will become calls to this function, instead of calls to fprintf.

Possible usecases include being able to:

  • integrate with another logging utility (e.g. from another language)

  • have a run-time log level threshold (e.g. settable by command line)

  • have a run-time output file (e.g. settable by config)

  • have multiple writers

  • perform conditional formatting (e.g. only print the location of errors and warnings)

  • add the thread name in the log

  • perform formatting and I/O in a separate thread

Note that logs discarded by LOG_LEVEL are completely removed at compile-time, and will NOT resolve to calls to the provided function. So if you want to have a run-time threshold in your custom function, be sure to set the compile-time threshold high enough, possibly to LOG_LEVEL_ALL

Log levels

Possible values of LOG_LEVEL

LOG_LEVEL_NONE#

Only output the header and forced logs.

LOG_LEVEL_FATAL#

The program will stop.

LOG_LEVEL_ERROR#

The current operation will abort.

LOG_LEVEL_WARNING#

Abnormal situation.

LOG_LEVEL_INFO#

Significant information.

LOG_LEVEL_DEBUG#

Only relevant to the developpers.

LOG_LEVEL_TRACE#

Spam.

LOG_LEVEL_ALL#

Output all logs.

Log formats

Possible values of LOG_FORMAT

LOG_FORMAT_NONE#

Disable outputting logs.

LOG_FORMAT_CONSOLE#

Print logs in a human readable format without colors.

LOG_FORMAT_COLOR#

Print logs in a human readable format with colors.

LOG_FORMAT_MARKDOWN#

Print logs as a markdown table.

LOG_FORMAT_JSON#

Print logs as a stream of JSON objects.

Defines

log_header()#

Place this macro once before any log.

log_fatal(MESSAGE, ...)#

Report a condition that forces the program to terminate.

log_error(MESSAGE, ...)#

Report a condition that forces the current operation to be aborted.

log_warning(MESSAGE, ...)#

Report an abnormal condition.

log_info(MESSAGE, ...)#

Report significant information.

log_debug(MESSAGE, ...)#

Report information relevant to the developpers.

log_trace(MESSAGE, ...)#

Spam.

log_force(MESSAGE, ...)#

Log regardless of level.