How to Integrate the Minimalist C Logging Library log.c into Your Projects
This guide introduces the lightweight C logging library log.c, explains its core structures, configuration functions, and thread‑safe usage, then walks through downloading the source, compiling, running a sample program, and inspecting the generated log files.
Overview
log.c is a minimal C logging library (single .c and .h, under 200 lines) that provides multiple log levels, file output, and optional thread‑safety.
Repository
git clone https://github.com/akstuki/log.c.gitInternal State
static struct {
void *udata; /* user data for lock callback */
log_LockFn lock; /* custom lock function */
FILE *fp; /* file handle for log output */
int level; /* current log level */
int quiet; /* non‑zero suppresses console output */
} L;Configuration macros
LOG_USE_COLOR – enable ANSI colour codes in terminal output.
API Functions
log_set_fp(FILE *fp)– set output file. log_set_lock(log_LockFn fn) – register lock callback. log_set_udata(void *udata) – set user data passed to lock. log_set_quiet(int enable) – 0 = console output, 1 = silent. log_set_level(int level) – change active log level (e.g., LOG_TRACE, LOG_ERROR).
Example
main.c demonstrates initializing a pthread mutex as the lock, opening a log file, and emitting logs at different levels.
/* main.c */
#include <stdio.h>
#include <pthread.h>
#include "log.h"
#define LOG_FILE_NAME "peng.log"
pthread_mutex_t log_mutex;
void log_mutex_fn(void *udata, int lock) {
if (lock) pthread_mutex_lock(&log_mutex);
else pthread_mutex_unlock(&log_mutex);
}
void my_log_lock_init(const char *info) {
pthread_mutex_init(&log_mutex, NULL);
log_set_lock(&log_mutex_fn);
log_set_udata((void *)info);
}
void my_fp_init(const char *filename) {
FILE *fp = fopen(filename, "w+");
if (!fp) {
printf("%s open fail
", filename);
return;
}
log_set_fp(fp);
}
int main(void) {
my_log_lock_init("peng");
my_fp_init(LOG_FILE_NAME);
log_set_quiet(0);
printf("Setting log level to LOG_TRACE.
");
log_set_level(LOG_TRACE);
log_trace("Sample log message.");
log_debug("Sample log message.");
log_info ("Sample log message.");
log_warn ("Sample log message.");
log_error("Sample log message.");
log_fatal("Sample log message.");
printf("Setting log level to LOG_ERROR.
");
log_set_level(LOG_ERROR);
log_trace("Sample log message."); /* ignored */
log_debug("Sample log message."); /* ignored */
log_info ("Sample log message."); /* ignored */
log_warn ("Sample log message."); /* ignored */
log_error("Sample log message.");
log_fatal("Sample log message.");
return 0;
}Compilation
gcc main.c log.c -o runRunning
Executing ./run prints coloured logs to the terminal and writes the same entries to peng.log. Adjust log_set_level and log_set_quiet to control verbosity and output destination.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
