log-e-sappho/include/spho/err.h

103 lines
2 KiB
C

#ifndef _SPHO_ERR_H
#define _SPHO_ERR_H
#include <errno.h>
#ifdef SPHO_DEBUG
#include <stdio.h>
#endif
#define SPHO_ERR_SYS 0x000001
#define SPHO_ERR_INTERNAL 0x010001
#define SPHO_ERR_TOOBIG 0x010002
#define SPHO_ERR_ARGINVAL 0x010003
#define SPHO_ERR_NAMEINUSE 0x010004
#define SPHO_ERR_IS_SYSERR(err) (SPHO_ERR_SYS == err)
#define SPHO_ERR_BUF_LEN 2048
#ifdef SPHO_DEBUG
#define SPHO_ERR_FILEBUF_LEN 128
#define SPHO_ERR(ctx, e) \
do { \
(ctx)->err = (e); \
if ((e) == SPHO_ERR_SYS) \
(ctx)->err_info = errno; \
snprintf((ctx)->filebuf, sizeof((ctx)->filebuf), \
__FILE__); \
(ctx)->errline = __LINE__; \
} while (0)
#define SPHO_STRINGIFY(a) #a
#define SPHO_MACRO_STR(b) SPHO_STRINGIFY(b)
#define __LINE__S SPHO_MACRO_STR(__LINE__)
#define SPHO_PRECOND(cond) \
do { \
if (! (cond) ) { \
fprintf(stderr, "SPHO_PRECOND(" #cond ")@" \
__FILE__ ":" __LINE__S \
" failed. Aborting.\n"); \
abort(); \
} \
} while (0)
#define SPHO_ASSERT(cond) \
do { \
if (! (cond) ) { \
fprintf(stderr, "SPHO_ASSERT(" #cond ")@" \
__FILE__ ":" __LINE__S \
" failed. Aborting.\n"); \
abort(); \
} \
} while (0)
#define SPHO_POSTCOND(cond) \
do { \
if (! (cond) ) { \
fprintf(stderr, "SPHO_POSTCOND(" #cond ")@" \
__FILE__ ":" __LINE__S \
" failed. Aborting.\n"); \
abort(); \
} \
} while (0)
#ifdef SPHO_ENABLE_DEBUG_PRINT
#define SPHO_DEBUG_PRINT(fmt, ...) \
do { \
fprintf(stderr, fmt __VA_OPT__(,) __VA_ARGS__); \
} while (0)
#else /* SPHO_ENABLE_DEBUG_PRINT */
#define SPHO_DEBUG_PRINT(fmt, ...)
#endif /* SPHO_ENABLE_DEBUG_PRINT */
#else /* SPHO_DEBUG */
#define SPHO_ERR(ctx, e) \
do { \
(ctx)->err = (e); \
if ((e) == SPHO_ERR_SYS) \
(ctx)->err_info = errno; \
} while (0)
#define SPHO_PRECOND(cond)
#define SPHO_ASSERT(cond)
#define SPHO_POSTCOND(cond)
#define SPHO_DBG_PRINT(fmt, ...)
#endif /* SPHO_DEBUG */
#endif /* _SPHO_ERR_H */