Skip to content

Instantly share code, notes, and snippets.

@okurka12
Created February 26, 2025 17:04
Show Gist options
  • Save okurka12/fa0458495fabf5cdd06e8fed52926851 to your computer and use it in GitHub Desktop.
Save okurka12/fa0458495fabf5cdd06e8fed52926851 to your computer and use it in GitHub Desktop.
Logging macros for ISO C
/*****************
** Vit Pavlik **
** xpavli0a **
** 251301 **
*****************/
/**
* Logging macros
*/
#ifndef _U_T_I_L_S_H_
#define _U_T_I_L_S_H_
#include <stdio.h> // fprintf
#include <string.h> // strcmp
/* log levels */
#define DEBUG 0
#define INFO 1
#define WARNING 2
#define ERROR 3
#define FATAL 4 // fatal error
#ifndef NDEBUG
/* use colors? tip: use when outputting to terminal, don't use when outputting
to file */
#define UTILS_USE_COLORS 1
/* https://gist.github.com/RabaDabaDoba/145049536f815903c79944599c6f952a */
#if UTILS_USE_COLORS
#define BLK "\033[0;30m"
#define RED "\033[0;31m"
#define GRN "\033[0;32m"
#define YEL "\033[0;33m"
#define BLU "\033[0;34m"
#define MAG "\033[0;35m"
#define CYN "\033[0;36m"
#define WHT "\033[0;37m"
#define RST "\033[0m" // reset
#else // if UTILS_USE_COLORS
#define BLK ""
#define RED ""
#define GRN ""
#define YEL ""
#define BLU ""
#define MAG ""
#define CYN ""
#define WHT ""
#define RST "" // reset
#endif // if UTILS_USE_COLORS
#define utils_prefix(lvl) ( \
lvl == DEBUG ? "DEBUG" : \
lvl == INFO ? WHT "INFO" RST : \
lvl == WARNING ? YEL "WARNING" RST : \
lvl == ERROR ? RED "ERROR" RST : \
lvl == FATAL ? MAG "FATAL" RST : \
"unknown-log-level" \
)
/*-----------------------------LOGGING----------------------------------------*/
/* set to 1 if you want to log the function name */
#define LOG_FN_NAME 1
/* spaces to pad the function name */
#define FN_NAME_PAD 15
/* implicit log level (can be overridden by -DLOGLEVEL=lvl compiler flag) */
#ifndef LOGLEVEL
#define LOGLEVEL DEBUG
#endif // #ifndef LOGLEVEL
#if LOG_FN_NAME
/* logs plain string (like puts) or does nothing if NDEBUG is defined */
#define log(lvl, msg) do { \
if (lvl >= LOGLEVEL) { \
fprintf(stderr, "%s: ", utils_prefix(lvl)); \
fprintf(stderr, __FILE__ ":%04d (%-*s): %s\n", __LINE__, \
FN_NAME_PAD, __func__, msg); \
} \
} while (0)
/* logs format (like printf) or does nothing if NDEBUG is defined */
#define logf(lvl, msg, ...) do { \
if (lvl >= LOGLEVEL) { \
fprintf(stderr, "%s: ", utils_prefix(lvl)); \
fprintf(stderr, __FILE__ ":%04d (%-*s): " msg "\n", \
__LINE__, FN_NAME_PAD, __func__, __VA_ARGS__); \
} \
} while (0)
#else // if LOG_FN_NAME
/* logs plain string (like puts) or does nothing if NDEBUG is defined */
#define log(lvl, msg) do { \
if (lvl >= LOGLEVEL) { \
fprintf(stderr, "%s: ", utils_prefix(lvl)); \
fprintf(stderr, __FILE__ ":%04d: %s\n", __LINE__, msg); \
} \
} while (0)
/* logs format (like printf) or does nothing if NDEBUG is defined */
#define logf(lvl, msg, ...) do { \
if (lvl >= LOGLEVEL) { \
fprintf(stderr, "%s: ", utils_prefix(lvl)); \
fprintf(stderr, __FILE__ ":%04d: " msg "\n", __LINE__, \
__VA_ARGS__); \
} \
} while (0)
#endif // if LOG_FN_NAME
/*----------------------------------------------------------------------------*/
#else // ifndef NDEBUG
#define log(lvl, msg) {}
#define logf(lvl, msg, ...) {}
#endif // ifndef NDEBUG
#endif // ifndef _U_T_I_L_S_H_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment