Log.c2 is based on rxi/log.c with MIT LICENSE which is inactive now. Log.c has a very flexible and scalable architecture

log.c2

A simple logging library.

Log.c2 is based on rxi/log.c with MIT LICENSE which is inactive now. Log.c has a very flexible and scalable architecture. There are some enhancements added in log.c2.

Enhancements:

  • Print logs to stdout/stderr.
  • Backup log file by file line number.
  • Add more log levels.
  • Enhance time accuracy.
  • ...

Usage

log.c and log.h should be dropped into an existing project and compiled along with it. The library provides function-like macros for logging:

log_debug(const char *fmt, ...);
log_info(const char *fmt, ...);
log_notice(const char *fmt, ...);
log_warn(const char *fmt, ...);
log_error(const char *fmt, ...);
log_critical(const char *fmt, ...);
log_alert(const char *fmt, ...);
log_emergency(const char *fmt, ...);

Each function takes a printf format string followed by additional arguments:

log_debug("Hello %s", "world")

Resulting in a line with the given format printed to stdout/stderr/file:

2020-08-11_16:05:03_877 [EMERGE] [demo_stdout.c:5  ]: this is a log.

log_stdout_verbose(int level)

Print log to stdout. Any messages above the given level are ignored.

log_stderr_verbose(int level)

Print log to stderr. Any messages above the given level are ignored.

log_add_fp(FILE *fp, int level)

One or more file pointers where the log will be written can be provided to the library by using the log_add_fp() function. The data written to the file output is of the following format:

2020-08-11_16:05:03_877 [EMERGE] [demo_stdout.c:5  ]: this is a log.

Any messages above the given level are ignored. If the library failed to add a file pointer a value less-than-zero is returned.

log_add_file_backup(char *filename, size_t n, int level)

File where the log will be written can be backuped by file line number. The backup will be renamed with a timestamp tail. A new file will be opened to continue logging.

demo.log  demo.log_2021-08-10_16:31:18_857

log_add_callback(log_LogFn fn, void *udata, int level)

One or more callback functions which are called with the log data can be provided to the library by using the log_add_callback() function. A callback function is passed a log_Event structure containing the line number, filename, fmt string, va printf va_list, level and the given udata.

log_set_lock(log_LockFn fn, void *udata)

If the log will be written to from multiple threads a lock function can be set. The function is passed the boolean true if the lock should be acquired or false if the lock should be released and the given udata value.

const char* log_level_string(int level)

Returns the name of the given log level as a string.

LOG_USE_COLOR

If the library is compiled with -DLOG_USE_COLOR ANSI color escape codes will be used when printing.

Help

There are demos in demo/.

❯ make
❯ ./demo
===========================================================
  Usages: demo [demoname]
Demoname: stdout
Demoname: file
Demoname: file_backup
❯ ./demo stdout
...

License

This library is free software; you can redistribute it and/or modify it under the terms of the MIT license. See LICENSE for details.

Similar Resources

Colorful Logging is a simple and efficient library allowing for logging and benchmarking.

Colorful Logging is a simple and efficient library allowing for  logging and benchmarking.

Colorful-Logging "Colorful Logging" is a library allowing for simple and efficient logging as well for benchmarking. What can you use it for? -Obvious

Feb 17, 2022

Portable, simple and extensible C++ logging library

Portable, simple and extensible C++ logging library

Plog - portable, simple and extensible C++ logging library Pretty powerful logging library in about 1000 lines of code Introduction Hello log! Feature

Dec 29, 2022

A DC power monitor and data logger

A DC power monitor and data logger

Hoverboard Power Monitor I wanted to gain a better understanding of the power consumption of my hoverboard during different riding situations. For tha

May 1, 2021

An ATTiny85 implementation of the well known sleep aid. Includes circuit, software and 3d printed case design

An ATTiny85 implementation of the well known sleep aid. Includes circuit, software and 3d printed case design

dodowDIY An ATTiny85 implementation of the well known sleep aid. Includes circuit, software and 3d printed case design The STL shells are desiged arou

Sep 4, 2022

Netdata's distributed, real-time monitoring Agent collects thousands of metrics from systems, hardware, containers, and applications with zero configuration.

Netdata's distributed, real-time monitoring Agent collects thousands of metrics from systems, hardware, containers, and applications with zero configuration.

Netdata is high-fidelity infrastructure monitoring and troubleshooting. It runs permanently on all your physical/virtual servers, containers, cloud deployments, and edge/IoT devices, and is perfectly safe to install on your systems mid-incident without any preparation.

Jan 4, 2023

Receive and process logs from the Linux kernel.

Netconsd: The Netconsole Daemon This is a daemon for receiving and processing logs from the Linux Kernel, as emitted over a network by the kernel's ne

Oct 5, 2022
Related tags
log4cplus is a simple to use C++ logging API providing thread-safe, flexible, and arbitrarily granular control over log management and configuration. It is modelled after the Java log4j API.

% log4cplus README Short Description log4cplus is a simple to use C++17 logging API providing thread--safe, flexible, and arbitrarily granular control

Jan 4, 2023
A revised version of NanoLog which writes human readable log file, and is easier to use.
A revised version of NanoLog which writes human readable log file, and is easier to use.

NanoLogLite NanoLogLite is a revised version of NanoLog, and is easier to use without performance compromise. The major changes are: NanoLogLite write

Nov 22, 2022
Example program using eBPF to log data being based in using shell pipes

Example program using eBPF to log data being based in using shell pipes (|)

Oct 21, 2022
View and log aoe-api requests and responses

aoe4_socketspy View and log aoe-api requests and responses Part 1: https://www.codereversing.com/blog/archives/420 Part 2: https://www.codereversing.c

Nov 1, 2022
Compressed Log Processor (CLP) is a free tool capable of compressing text logs and searching the compressed logs without decompression.

CLP Compressed Log Processor (CLP) is a tool capable of losslessly compressing text logs and searching the compressed logs without decompression. To l

Dec 30, 2022
Log engine for c plus plus
Log engine for c plus plus

PTCLogs library PTCLogs is a library for pretty and configurable logs. Installation To install the library (headers and .so file), clone this repo and

May 20, 2022
Cute Log is a C++ Library that competes to be a unique logging tool.

Cute Log Cute Log is a C++ Library that competes to be a unique logging tool. Version: 2 Installation Click "Code" on the main repo page (This one.).

Oct 13, 2022
Sagan - a multi-threads, high performance log analysis engine

Sagan - Sagan is a multi-threads, high performance log analysis engine. At it's core, Sagan similar to Suricata/Snort but with logs rather than network packets.

Dec 22, 2022
A BSD-based OS project that aims to provide an experience like and some compatibility with macOS
A BSD-based OS project that aims to provide an experience like and some compatibility with macOS

What is Helium? Helium is a new open source OS project that aims to provide a similar experience and some compatibiilty with macOS on x86-64 sytems. I

Dec 30, 2022
An Ultra Low Power temperature logger based on the ESP8266 MCU.
An Ultra Low Power temperature logger based on the ESP8266 MCU.

Temperature logging IoT node Overview: The real node wired on a breadboard This is an ultra low power (ULP) temperature logging IoT node based on the

Nov 16, 2022