This repository has been archived on 2023-08-27. You can view files and clone it, but cannot push or open issues or pull requests.
tcpaste/include/log.h
Thorsten Schubert 64c89e9274
Some checks reported errors
continuous-integration/drone/push Build is passing
continuous-integration/drone Build encountered an error
Bug fixes
2021-11-08 18:39:10 +01:00

124 lines
5 KiB
C

/* vi:set ft=c ts=4 sw=4 noet noai: */
#pragma once
#include <stdio.h>
#include <string.h>
#ifndef LOG_LEVEL
#ifdef DEBUG
#define LOG_LEVEL 4
#else
#define LOG_LEVEL 3
#endif
#endif
#define FATAL_LEVEL 0
#define ERROR_LEVEL 1
#define WARN_LEVEL 2
#define INFO_LEVEL 3
#define DEBUG_LEVEL 4
#define MEM_LEVEL 5
#define FATAL_TAG "FATAL"
#define ERROR_TAG "ERROR"
#define WARN_TAG "WARN"
#define INFO_TAG "INFO"
#define DEBUG_TAG "DEBUG"
#define MEM_TAG "MEM"
#define LOG_TAG(level) \
(ERROR_LEVEL == (level) \
? ERROR_TAG \
: (WARN_LEVEL == (level) \
? WARN_TAG \
: (INFO_LEVEL == (level) ? INFO_TAG \
: (DEBUG_LEVEL == (level) \
? DEBUG_TAG \
: (MEM_LEVEL == (level) ? MEM_TAG : FATAL_TAG)))))
#define _NL "\n"
#define _FILE strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__
#define PRINTMSG(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__)
#ifdef DEBUG
#define _LOG_FMT "%5s | (%-8s:%s:%d) # "
#define LOG_ARGS(tag) tag, _FILE, __func__, __LINE__
#else
#define _LOG_FMT "%5s | "
#define LOG_ARGS(tags) tags
#endif
#if (LOG_LEVEL >= MEM_LEVEL)
#define LOG_MEM(msg, ...) \
PRINTMSG(_LOG_FMT msg _NL, LOG_ARGS(MEM_TAG) __VA_OPT__(, ) __VA_ARGS__)
#else
#define LOG_MEM(msg, ...)
#endif
#if (LOG_LEVEL >= DEBUG_LEVEL)
#define LOG_DEBUG(msg, ...) \
PRINTMSG(_LOG_FMT msg _NL, LOG_ARGS(DEBUG_TAG) __VA_OPT__(, ) __VA_ARGS__)
#else
#define LOG_DEBUG(msg, ...)
#endif
#if LOG_LEVEL >= INFO_LEVEL
#define LOG_INFO(msg, ...) \
PRINTMSG(_LOG_FMT msg _NL, LOG_ARGS(INFO_TAG) __VA_OPT__(, ) __VA_ARGS__)
#else
#define LOG_INFO(msg, ...)
#endif
#if LOG_LEVEL >= WARN_LEVEL
#define LOG_WARN(msg, ...) \
PRINTMSG(_LOG_FMT msg _NL, LOG_ARGS(WARN_TAG) __VA_OPT__(, ) __VA_ARGS__)
#else
#define LOG_WARN(msg, ...)
#endif
#if LOG_LEVEL >= ERROR_LEVEL
#define LOG_ERROR(msg, ...) \
PRINTMSG(_LOG_FMT msg _NL, LOG_ARGS(ERROR_TAG) __VA_OPT__(, ) __VA_ARGS__)
#define LOG_PERROR(error) \
PRINTMSG(_LOG_FMT "%s: %s" _NL, LOG_ARGS(ERROR_TAG), error, strerror(errno))
#else
#define LOG_ERROR(msg, ...)
#define LOG_PERROR(error)
#endif
#if LOG_LEVEL >= FATAL_LEVEL
#define LOG_IF(cond, msg, ...) \
do { \
if (cond) \
PRINTMSG(_LOG_FMT msg _NL, LOG_ARGS(ERROR_TAG) __VA_OPT__(, ) __VA_ARGS__); \
} while (0)
#else
#define LOG_IF(cond, msg, ...)
#endif
#define FATAL(msg, ...) \
do { \
PRINTMSG(_LOG_FMT msg _NL, LOG_ARGS(FATAL_TAG) __VA_OPT__(, ) __VA_ARGS__); \
abort(); \
} while (0)
#define PFATAL(msg, ...) \
do { \
PRINTMSG(_LOG_FMT "%s: %s" _NL, LOG_ARGS(FATAL_TAG), error, strerror(errno)); \
abort(); \
} while (0)
#if HAS_BUILTIN(__builtin_expect)
#define COND_UNLIKELY(x) __builtin_expect(!!(x), 0)
#else
#define COND_UNLIKELY(x) (x)
#endif
#define LOG_PABORT_ON(cond, msg, ...) \
do { \
if (COND_UNLIKELY((cond) == -1)) { \
LOG_PERROR(msg __VA_OPT__(, ) __VA_ARGS__); \
abort(); \
} \
} while (0)