clibs
Tiny but handy utility libraries for C
Loading...
Searching...
No Matches
clibs_logging.h File Reference

Thread-local lightweight logging macros for C. More...

#include <stdio.h>
Include dependency graph for clibs_logging.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define CLIBS_LOG_LEVEL_NONE   0
#define CLIBS_LOG_LEVEL_FATAL   1
#define CLIBS_LOG_LEVEL_ERROR   2
#define CLIBS_LOG_LEVEL_WARN   3
#define CLIBS_LOG_LEVEL_INFO   4
#define CLIBS_LOG_LEVEL_DEBUG   5
#define CLIBS_LOG_LEVEL   CLIBS_LOG_LEVEL_NONE
 Compile-time logging verbosity level.
#define CLIBS_LOG_DEBUG(fmt, ...)
 Logs a debug message with file and line information.
#define CLIBS_LOG_INFO(fmt, ...)
 Logs an informational message.
#define CLIBS_LOG_WARN(fmt, ...)
 Logs a warning message.
#define CLIBS_LOG_ERROR(fmt, ...)
 Logs an error message.
#define CLIBS_LOG_FATAL(fmt, ...)
 Logs a fatal message with file and line information.

Functions

void clibs_set_output (FILE *out, FILE *err)
 Sets thread-local output streams.
FILE * clibs_get_out (void)
 Returns the current thread-local output stream.
FILE * clibs_get_err (void)
 Returns the current thread-local error stream.

Detailed Description

Thread-local lightweight logging macros for C.

This module provides lightweight, compile-time filtered logging macros for C programs. Log output is written to the current thread-local error stream returned by clibs_get_err(), with fallback to stderr.

Users may set CLIBS_LOG_LEVEL before including this header:

#define CLIBS_LOG_LEVEL CLIBS_LOG_LEVEL_DEBUG #include "clibs_logging.h"

If CLIBS_LOG_LEVEL is not defined, logging is disabled by default.

This header intentionally does not enforce any build flag convention such as DEBUG or NDEBUG, and it does not terminate the process in any logging macro. Process control remains the caller's responsibility.

Author
ByteBard

Macro Definition Documentation

◆ CLIBS_LOG_DEBUG

#define CLIBS_LOG_DEBUG ( fmt,
... )
Value:
do { \
fprintf(clibs_get_err(), "[DEBUG] (%s:%d) " fmt "\n", \
__FILE__, __LINE__, ##__VA_ARGS__); \
} while (0)
FILE * clibs_get_err(void)
Returns the current thread-local error stream.
Definition clibs_logging.c:35

Logs a debug message with file and line information.

◆ CLIBS_LOG_ERROR

#define CLIBS_LOG_ERROR ( fmt,
... )
Value:
do { \
fprintf(clibs_get_err(), "[ERROR] " fmt "\n", ##__VA_ARGS__); \
} while (0)

Logs an error message.

This macro only logs. It does not terminate the process.

◆ CLIBS_LOG_FATAL

#define CLIBS_LOG_FATAL ( fmt,
... )
Value:
do { \
fprintf(clibs_get_err(), "[FATAL] (%s:%d) " fmt "\n", \
__FILE__, __LINE__, ##__VA_ARGS__); \
} while (0)

Logs a fatal message with file and line information.

This macro only logs. It does not abort the process.

◆ CLIBS_LOG_INFO

#define CLIBS_LOG_INFO ( fmt,
... )
Value:
do { \
fprintf(clibs_get_err(), "[INFO] " fmt "\n", ##__VA_ARGS__); \
} while (0)

Logs an informational message.

◆ CLIBS_LOG_LEVEL

#define CLIBS_LOG_LEVEL   CLIBS_LOG_LEVEL_NONE

Compile-time logging verbosity level.

Define this macro before including this header, for example:

#define CLIBS_LOG_LEVEL CLIBS_LOG_LEVEL_DEBUG

If not defined, CLIBS_LOG_LEVEL defaults to CLIBS_LOG_LEVEL_NONE.

◆ CLIBS_LOG_WARN

#define CLIBS_LOG_WARN ( fmt,
... )
Value:
do { \
fprintf(clibs_get_err(), "[WARN] " fmt "\n", ##__VA_ARGS__); \
} while (0)

Logs a warning message.

Function Documentation

◆ clibs_get_err()

FILE * clibs_get_err ( void )

Returns the current thread-local error stream.

Falls back to stderr if no thread-local error stream is set.

Returns
FILE* current error stream.

◆ clibs_get_out()

FILE * clibs_get_out ( void )

Returns the current thread-local output stream.

Falls back to stdout if no thread-local output stream is set.

Returns
FILE* current output stream.

◆ clibs_set_output()

void clibs_set_output ( FILE * out,
FILE * err )

Sets thread-local output streams.

Parameters
outOutput stream for general messages (default fallback: stdout).
errOutput stream for log messages (default fallback: stderr).