Workbench Library  0.1b
workbench_logger.h File Reference

A logging utility for C programs with configurable verbosity levels and optional context information. More...

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

Go to the source code of this file.

Macros

#define log_format(file, tag, message, ...)
 Formats and logs a message to a specified file with a tag and optional arguments. More...
 
Config
#define LOG_LEVEL   4
 Default log level if not defined elsewhere. More...
 
#define PRINT_FUNC   (LOG_LEVEL > 3)
 Controls whether to include the function name in log messages.
 
#define PRINT_FILE   (LOG_LEVEL > 2)
 Controls whether to include the file name and line number in log messages.
 
Color function-like macros
#define COLOR(text, color, style)   "\033[" #style ";" #color "m" text "\033[0m"
 Adds ANSI color and style codes to a text string. More...
 
#define RED(text)   COLOR(text, 31, 0)
 Colors the text red.
 
#define GRN(text)   COLOR(text, 32, 0)
 Colors the text green.
 
#define YLW(text)   COLOR(text, 33, 0)
 Colors the text yellow.
 
#define PRL(text)   COLOR(text, 35, 0)
 Colors the text purple.
 
#define CYN(text)   COLOR(text, 36, 0)
 Colors the text cyan.
 
Logging function-like macros
Parameters
messageThe debug message string. Can include format specifiers.
...Optional additional arguments corresponding to the format specifiers.
#define log_d(message, ...)
 Logs a debug message. More...
 
#define log_i(message, ...)
 Logs an info message. More...
 
#define log_w(message, ...)
 Logs a warning message. More...
 
#define log_e(message, ...)
 Logs an error message. More...
 

Detailed Description

A logging utility for C programs with configurable verbosity levels and optional context information.

This header file provides macros for logging messages with different severity levels (debug, info, warning, error). The log messages can include additional context information such as the function name, file name, and line number. The verbosity of the logging can be controlled through the LOG_LEVEL macro.

Features:

  • Configurable log levels: DEBUG, INFO, WARNING, ERROR
  • Optional inclusion of function names and file names in log messages
  • ANSI color codes for colored log messages in the terminal

Example usage:

#define LOG_LEVEL 3 // Set log level to INFO
void example_function() {
log_d("This is a debug message.");
log_i("This is an info message.");
log_w("This is a warning message.");
log_e("This is an error message.");
}
A logging utility for C programs with configurable verbosity levels and optional context information.
#define log_e(message,...)
Logs an error message.
Definition: workbench_logger.h:148
#define log_w(message,...)
Logs a warning message.
Definition: workbench_logger.h:142
#define log_i(message,...)
Logs an info message.
Definition: workbench_logger.h:135
#define log_d(message,...)
Logs a debug message.
Definition: workbench_logger.h:129

Macro Definition Documentation

◆ COLOR

#define COLOR (   text,
  color,
  style 
)    "\033[" #style ";" #color "m" text "\033[0m"

Adds ANSI color and style codes to a text string.

This macro wraps a text string with ANSI color and style codes to display the text in a specific color and style in the terminal. Common style codes include:

  • 0: Regular
  • 1: Bold
  • 2: Dim
  • 3: Italic
  • 4: Underline

Common color codes include:

  • 30: Black
  • 31: Red
  • 32: Green
  • 33: Yellow
  • 34: Blue
  • 35: Magenta
  • 36: Cyan
  • 37: White
Parameters
textThe text to be colored and styled.
colorThe ANSI color code.
styleThe ANSI style code.
Returns
The colored and styled text string.

◆ log_d

#define log_d (   message,
  ... 
)
Value:
if (get_log_level() > 3) \
log_format(__stdoutp, CYN("dbg"), message __VA_OPT__(, ) __VA_ARGS__);
uint8_t get_log_level()
Retrieves the log level.
Definition: workbench_config.c:34
#define CYN(text)
Colors the text cyan.
Definition: workbench_logger.h:117

Logs a debug message.

◆ log_e

#define log_e (   message,
  ... 
)
Value:
if (get_log_level() > 0) \
log_format(__stderrp, RED("err"), RED(message) __VA_OPT__(, ) __VA_ARGS__);
#define RED(text)
Colors the text red.
Definition: workbench_logger.h:113

Logs an error message.

◆ log_format

#define log_format (   file,
  tag,
  message,
  ... 
)
Value:
fprintf(file, "[%s] " _fun_print_tmpl _file_print_tmpl message "\n", \
tag _fun_print_arg _file_print_arg __VA_OPT__(, ) __VA_ARGS__);

Formats and logs a message to a specified file with a tag and optional arguments.

This macro writes a formatted log message to the provided file. The log message includes a tag and can incorporate additional formatted arguments similar to printf.

The macro supports optional arguments which are included in the formatted message if provided.

Parameters
fileThe file stream to write the log message to (e.g., stderr, stdout, or a file pointer).
tagA string tag to categorize or label the log message (e.g., "INFO", "ERROR").
messageThe main message string to be logged. This can include format specifiers like printf.
...Optional additional arguments that correspond to the format specifiers in the message.

Example usage:

log_format(stderr, "ERROR", "Failed to open file: %s", filename);
// This would log: [ERROR] Failed to open file: example.txt
#define log_format(file, tag, message,...)
Formats and logs a message to a specified file with a tag and optional arguments.
Definition: workbench_logger.h:194

◆ log_i

#define log_i (   message,
  ... 
)
Value:
if (get_log_level() > 2) \
log_format(__stdoutp, GRN("inf"), message __VA_OPT__(, ) __VA_ARGS__);
#define GRN(text)
Colors the text green.
Definition: workbench_logger.h:114

Logs an info message.

◆ LOG_LEVEL

#define LOG_LEVEL   4

Default log level if not defined elsewhere.

LOG_LEVEL determines the verbosity of the logging. The higher the value, the more verbose the logging:

  • 0: No logging
  • 1: Error logging
  • 2: Warning and Error logging
  • 3: Info, Warning, and Error logging
  • 4: Debug, Info, Warning, and Error logging

◆ log_w

#define log_w (   message,
  ... 
)
Value:
if (get_log_level() > 1) \
log_format(__stderrp, YLW("wrn"), YLW(message) __VA_OPT__(, ) __VA_ARGS__);
#define YLW(text)
Colors the text yellow.
Definition: workbench_logger.h:115

Logs a warning message.