Advertisement
Guest User

dbg.h

a guest
Aug 6th, 2014
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.71 KB | None | 0 0
  1. /** @file dbg.h
  2.  *  @brief Modified "Zed's Awesome Debug Macros".
  3.  *
  4.  *  Originally from:
  5.  *  http://c.learncodethehardway.org/book/ex20.html
  6.  *
  7.  *  @author Zed (http://c.learncodethehardway.org/book/ex20.html)
  8.  *  @author Luke
  9.  */
  10.  
  11. #ifndef __dbg_h__
  12. #define __dbg_h__
  13.  
  14. #include <stdio.h>
  15. #include <errno.h>
  16. #include <string.h>
  17.  
  18. #define clean_errno() (errno == 0 ? "None" : strerror(errno))
  19.  
  20. #ifdef __WIN32
  21. #include <windows.h>
  22.  
  23. struct DBG {
  24.     int error;
  25.     HANDLE h_console;
  26.     CONSOLE_SCREEN_BUFFER_INFO console_info;
  27.     WORD saved_attributes;
  28. };
  29.  
  30. #define DBG_INIT()  g_dbg.error = 0;\
  31.                     g_dbg.h_console = GetStdHandle(STD_OUTPUT_HANDLE);\
  32.                     GetConsoleScreenBufferInfo(g_dbg.h_console, &g_dbg.console_info);\
  33.                     g_dbg.saved_attributes = g_dbg.console_info.wAttributes
  34.  
  35.  
  36. #define DBG_COLOR_DEBUG_WIN32 GetConsoleScreenBufferInfo(g_dbg.h_console, &g_dbg.console_info);\
  37.                               g_dbg.saved_attributes = g_dbg.console_info.wAttributes;\
  38.                               SetConsoleTextAttribute(g_dbg.h_console, g_dbg.saved_attributes);
  39.  
  40. #define DBG_COLOR_ERR_WIN32 GetConsoleScreenBufferInfo(g_dbg.h_console, &g_dbg.console_info);\
  41.                             g_dbg.saved_attributes = g_dbg.console_info.wAttributes;\
  42.                             SetConsoleTextAttribute(g_dbg.h_console,\
  43.                                 FOREGROUND_INTENSITY | FOREGROUND_RED);
  44.  
  45. #define DBG_COLOR_WARN_WIN32 GetConsoleScreenBufferInfo(g_dbg.h_console, &g_dbg.console_info);\
  46.                              g_dbg.saved_attributes = g_dbg.console_info.wAttributes;\
  47.                              SetConsoleTextAttribute(g_dbg.h_console,\
  48.                                 FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
  49.  
  50. #define DBG_COLOR_INFO_WIN32 GetConsoleScreenBufferInfo(g_dbg.h_console, &g_dbg.console_info);\
  51.                              g_dbg.saved_attributes = g_dbg.console_info.wAttributes;\
  52.                              SetConsoleTextAttribute(g_dbg.h_console,\
  53.                                 FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
  54.  
  55. #define DBG_COLOR_RESET_WIN32 SetConsoleTextAttribute(g_dbg.h_console, g_dbg.saved_attributes)
  56.  
  57. #define DBG_COLOR_DEBUG
  58. #define DBG_COLOR_ERR
  59. #define DBG_COLOR_WARN
  60. #define DBG_COLOR_INFO
  61. #define DBG_COLOR_RESET
  62.  
  63. #else
  64.  
  65.  
  66. /**
  67.  * @struct DBG
  68.  * @brief Struct to store debug/logging vars.
  69.  */
  70. struct DBG {
  71.     int error;
  72. };
  73.  
  74. #define DBG_INIT() g_dbg.error = 0;
  75.  
  76. #define DBG_COLOR_DEBUG "\x1B[35m"
  77. #define DBG_COLOR_ERR "\x1B[31m"
  78. #define DBG_COLOR_WARN "\x1B[33m"
  79. #define DBG_COLOR_INFO "\x1B[34m"
  80. #define DBG_COLOR_RESET "\033[0m"
  81.  
  82. #define DBG_COLOR_DEBUG_WIN32
  83. #define DBG_COLOR_ERR_WIN32
  84. #define DBG_COLOR_WARN_WIN32
  85. #define DBG_COLOR_INFO_WIN32
  86. #define DBG_COLOR_RESET_WIN32
  87.  
  88. #endif /* __WIN32 */
  89.  
  90. extern struct DBG g_dbg;
  91.  
  92.  
  93. #ifdef NDEBUG
  94. #define debug(M, ...)
  95. #else
  96. #define debug(M, ...) DBG_COLOR_DEBUG_WIN32;\
  97.                       fprintf(stderr, DBG_COLOR_DEBUG "DEBUG %s:%d: " M "\n"\
  98.                               DBG_COLOR_RESET, __FILE__, __LINE__, ##__VA_ARGS__);\
  99.                       DBG_COLOR_RESET_WIN32
  100. #endif
  101.  
  102.  
  103. #define log_err(M, ...) DBG_COLOR_ERR_WIN32;\
  104.                         g_dbg.error = 1;\
  105.                         fprintf(stderr, DBG_COLOR_ERR "[ERROR] (%s:%d: errno: %s) " M "\n"\
  106.                                 DBG_COLOR_RESET, __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__);\
  107.                         DBG_COLOR_RESET_WIN32
  108.  
  109. #define log_warn(M, ...) DBG_COLOR_WARN_WIN32;\
  110.                          fprintf(stderr, DBG_COLOR_WARN "[WARN] (%s:%d: errno: %s) " M "\n"\
  111.                                  DBG_COLOR_RESET, __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__);\
  112.                          DBG_COLOR_RESET_WIN32
  113.  
  114. #define log_info(M, ...) DBG_COLOR_INFO_WIN32;\
  115.                          fprintf(stderr, DBG_COLOR_INFO "[INFO] (%s:%d) " M "\n"\
  116.                                  DBG_COLOR_RESET, __FILE__, __LINE__, ##__VA_ARGS__);\
  117.                          DBG_COLOR_RESET_WIN32
  118.  
  119. #define check(A, M, ...) if( !(A) ) {\
  120.                             log_err(M, ##__VA_ARGS__);\
  121.                             errno=0;\
  122.                             goto error;\
  123.                          }
  124.  
  125. #define sentinel(M, ...)  { log_err(M, ##__VA_ARGS__);\
  126.                             errno=0;\
  127.                             goto error; }
  128.  
  129. #define check_mem(A) check((A), "Out of memory.")
  130.  
  131. #define check_debug(A, M, ...) if( !(A) ) {\
  132.                                    debug(M, ##__VA_ARGS__);\
  133.                                    errno=0;\
  134.                                    goto error;\
  135.                                }
  136.  
  137. #endif /* __dbg_h__ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement