Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** @file dbg.h
- * @brief Modified "Zed's Awesome Debug Macros".
- *
- * Originally from:
- * http://c.learncodethehardway.org/book/ex20.html
- *
- * @author Zed (http://c.learncodethehardway.org/book/ex20.html)
- * @author Luke
- */
- #ifndef __dbg_h__
- #define __dbg_h__
- #include <stdio.h>
- #include <errno.h>
- #include <string.h>
- #define clean_errno() (errno == 0 ? "None" : strerror(errno))
- #ifdef __WIN32
- #include <windows.h>
- struct DBG {
- int error;
- HANDLE h_console;
- CONSOLE_SCREEN_BUFFER_INFO console_info;
- WORD saved_attributes;
- };
- #define DBG_INIT() g_dbg.error = 0;\
- g_dbg.h_console = GetStdHandle(STD_OUTPUT_HANDLE);\
- GetConsoleScreenBufferInfo(g_dbg.h_console, &g_dbg.console_info);\
- g_dbg.saved_attributes = g_dbg.console_info.wAttributes
- #define DBG_COLOR_DEBUG_WIN32 GetConsoleScreenBufferInfo(g_dbg.h_console, &g_dbg.console_info);\
- g_dbg.saved_attributes = g_dbg.console_info.wAttributes;\
- SetConsoleTextAttribute(g_dbg.h_console, g_dbg.saved_attributes);
- #define DBG_COLOR_ERR_WIN32 GetConsoleScreenBufferInfo(g_dbg.h_console, &g_dbg.console_info);\
- g_dbg.saved_attributes = g_dbg.console_info.wAttributes;\
- SetConsoleTextAttribute(g_dbg.h_console,\
- FOREGROUND_INTENSITY | FOREGROUND_RED);
- #define DBG_COLOR_WARN_WIN32 GetConsoleScreenBufferInfo(g_dbg.h_console, &g_dbg.console_info);\
- g_dbg.saved_attributes = g_dbg.console_info.wAttributes;\
- SetConsoleTextAttribute(g_dbg.h_console,\
- FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
- #define DBG_COLOR_INFO_WIN32 GetConsoleScreenBufferInfo(g_dbg.h_console, &g_dbg.console_info);\
- g_dbg.saved_attributes = g_dbg.console_info.wAttributes;\
- SetConsoleTextAttribute(g_dbg.h_console,\
- FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
- #define DBG_COLOR_RESET_WIN32 SetConsoleTextAttribute(g_dbg.h_console, g_dbg.saved_attributes)
- #define DBG_COLOR_DEBUG
- #define DBG_COLOR_ERR
- #define DBG_COLOR_WARN
- #define DBG_COLOR_INFO
- #define DBG_COLOR_RESET
- #else
- /**
- * @struct DBG
- * @brief Struct to store debug/logging vars.
- */
- struct DBG {
- int error;
- };
- #define DBG_INIT() g_dbg.error = 0;
- #define DBG_COLOR_DEBUG "\x1B[35m"
- #define DBG_COLOR_ERR "\x1B[31m"
- #define DBG_COLOR_WARN "\x1B[33m"
- #define DBG_COLOR_INFO "\x1B[34m"
- #define DBG_COLOR_RESET "\033[0m"
- #define DBG_COLOR_DEBUG_WIN32
- #define DBG_COLOR_ERR_WIN32
- #define DBG_COLOR_WARN_WIN32
- #define DBG_COLOR_INFO_WIN32
- #define DBG_COLOR_RESET_WIN32
- #endif /* __WIN32 */
- extern struct DBG g_dbg;
- #ifdef NDEBUG
- #define debug(M, ...)
- #else
- #define debug(M, ...) DBG_COLOR_DEBUG_WIN32;\
- fprintf(stderr, DBG_COLOR_DEBUG "DEBUG %s:%d: " M "\n"\
- DBG_COLOR_RESET, __FILE__, __LINE__, ##__VA_ARGS__);\
- DBG_COLOR_RESET_WIN32
- #endif
- #define log_err(M, ...) DBG_COLOR_ERR_WIN32;\
- g_dbg.error = 1;\
- fprintf(stderr, DBG_COLOR_ERR "[ERROR] (%s:%d: errno: %s) " M "\n"\
- DBG_COLOR_RESET, __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__);\
- DBG_COLOR_RESET_WIN32
- #define log_warn(M, ...) DBG_COLOR_WARN_WIN32;\
- fprintf(stderr, DBG_COLOR_WARN "[WARN] (%s:%d: errno: %s) " M "\n"\
- DBG_COLOR_RESET, __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__);\
- DBG_COLOR_RESET_WIN32
- #define log_info(M, ...) DBG_COLOR_INFO_WIN32;\
- fprintf(stderr, DBG_COLOR_INFO "[INFO] (%s:%d) " M "\n"\
- DBG_COLOR_RESET, __FILE__, __LINE__, ##__VA_ARGS__);\
- DBG_COLOR_RESET_WIN32
- #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__ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement