Last active
January 14, 2016 07:53
-
-
Save melihovv/888ebe804139c6e5457e to your computer and use it in GitHub Desktop.
C debug macros
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef DBG_H | |
#define DBG_H | |
#include <stdio.h> | |
#include <errno.h> | |
#include <string.h> | |
#ifdef NDEBUG | |
# define DEBUG(M, ...) | |
#else | |
# define DEBUG(M, ...) fprintf( \ | |
stderr, \ | |
"DEBUG %s:%d: " M "\n", \ | |
__FILE__, \ | |
__LINE__, \ | |
##__VA_ARGS__ \ | |
) | |
#endif | |
#define CLEAN_ERRNO() (errno == 0 ? "None" : strerror(errno)) | |
#define LOG_ERR(M, ...) fprintf( \ | |
stderr, \ | |
"[ERROR] (%s:%d: errno: %s) " M "\n", \ | |
__FILE__, \ | |
__LINE__, \ | |
CLEAN_ERRNO(), \ | |
##__VA_ARGS__ \ | |
) | |
#define LOG_WARN(M, ...) fprintf( \ | |
stderr, \ | |
"[WARN] (%s:%d: errno: %s) " M "\n", \ | |
__FILE__, \ | |
__LINE__, \ | |
CLEAN_ERRNO(), \ | |
##__VA_ARGS__ \ | |
) | |
#define LOG_INFO(M, ...) fprintf( \ | |
stderr, \ | |
"[INFO] (%s:%d) " M "\n", \ | |
__FILE__, \ | |
__LINE__, \ | |
##__VA_ARGS__ \ | |
) | |
#define CHECK(A, M, ...) if(!(A)) { \ | |
LOG_ERR(M, ##__VA_ARGS__); \ | |
errno = 0; \ | |
goto error; \ | |
} | |
#define SENTINEL(M, ...) { \ | |
LOG_ERR(M, ##__VA_ARGS__); \ | |
errno = 0; \ | |
goto error; \ | |
} | |
#define CHECK_MEM(A) CHECK((A), "Out of memory.") | |
#define CHECK_DEBUG(A, M, ...) if(!(A)) { \ | |
DEBUG(M, ##__VA_ARGS__); \ | |
errno = 0; \ | |
goto error; \ | |
} | |
#endif // DBG_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment