Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
7.1 KiB

  1. ////////////////////////////////////////////////////////////////////////////
  2. // FILE : dbgtrace.h //
  3. // //
  4. // DESCRIPTION : Define some macros and inline functions for debugging. //
  5. // //
  6. // AUTHOR : DanL. //
  7. // //
  8. // HISTORY : //
  9. // Oct 19 1999 DannyL Creation. //
  10. // //
  11. // Copyright (C) 1999 Microsoft Corporation All Rights Reserved //
  12. /////////////////////////////////////////////////////////////////////////////
  13. #ifndef __UTILS__DBGTRACE_H
  14. #define __UTILS__DBGTRACE_H
  15. #include <stdio.h>
  16. #include <stdarg.h>
  17. #include <winerror.h>
  18. #define MAX_PROC_NAME 30
  19. #define MAX_TRACE_LINE 200
  20. //
  21. // The following key is the parent of the Log key which containes the
  22. // log path in the default value.
  23. //
  24. #define HKEY_DBG "SOFTWARE\\Microsoft\\SharedFax\\9XDrvDbg"
  25. #ifdef DBG
  26. #define POPUPS
  27. #define DBG_DEBUG
  28. #endif //DEBUG
  29. #define NO_NULL_STR(_str) ((LPSTR)( (_str)? _str: "<NULL>" ))
  30. #define BOOL_VALUE(_f) ((LPSTR)( (_f)? _T("TRUE") : _T("FALSE") ))
  31. #ifndef WIN32
  32. #define wsprintfA wsprintf
  33. #define OutputDebugStringA OutputDebugString
  34. #define MessageBoxA MessageBox
  35. #endif //WIN32
  36. typedef struct tagDBG_CONTEXT_INFO
  37. {
  38. char szProcName[MAX_TRACE_LINE];
  39. BOOL fSilent;
  40. int iNumEntries;
  41. } DBG_CONTEXT_INFO;
  42. #ifdef DBG_DEBUG
  43. #define DBG_MESSAGE_BOX3(str,arg1,arg2,arg3)\
  44. {\
  45. char sz[MAX_TRACE_LINE];\
  46. wsprintfA(sz,"%s(): "str"\r\n",(LPSTR)__dbgContextInfo.szProcName,arg1,arg2,arg3);\
  47. MessageBoxA(NULL,(LPSTR)sz,(LPSTR)__dbgGlobalInfo.szModuleName,MB_OK);\
  48. }
  49. #define DBG_MESSAGE_BOX(str) DBG_MESSAGE_BOX3(str "%c%c%c",(' '),(' '),(' '))
  50. #define DBG_MESSAGE_BOX1(str,arg) DBG_MESSAGE_BOX3(str "%c%c",arg,(' '),(' '))
  51. #define DBG_MESSAGE_BOX2(str,arg1,arg2) DBG_MESSAGE_BOX3(str "%c",arg1,arg2,(' '));
  52. typedef struct tagDBG_GLOBAL_INFO
  53. {
  54. char szLogName[MAX_PATH];
  55. char szModuleName[MAX_PATH];
  56. BOOL bUseLog;
  57. BOOL bInitialized;
  58. } DBG_GLOBAL_INFO;
  59. extern DBG_GLOBAL_INFO __dbgGlobalInfo;
  60. #define OUTPUT_DEBUG_STRING(str) __dbgOutputDebugString(str,__dbgContextInfo)
  61. void __inline
  62. __dbgOutputDebugString(const PSTR str,DBG_CONTEXT_INFO __dbgContextInfo)
  63. {
  64. FILE* pfLog;
  65. OutputDebugStringA(str);
  66. if (!__dbgGlobalInfo.bInitialized)
  67. {
  68. HKEY hkey;
  69. LONG cbData = sizeof(__dbgGlobalInfo.szLogName);
  70. if ((ERROR_SUCCESS != RegCreateKey(HKEY_LOCAL_MACHINE, HKEY_DBG,&hkey)) ||
  71. (ERROR_SUCCESS != RegQueryValue(hkey, "Log", __dbgGlobalInfo.szLogName, &cbData)))
  72. {
  73. __dbgGlobalInfo.bUseLog = FALSE;
  74. }
  75. }
  76. if (__dbgGlobalInfo.bUseLog != FALSE)
  77. {
  78. if ( !(pfLog = fopen(__dbgGlobalInfo.szLogName,"a")))
  79. {
  80. DBG_MESSAGE_BOX2("DEBUG: Error: 0x%lx Failed to open Log file: %s",GetLastError(),(LPSTR)__dbgGlobalInfo.szLogName);
  81. __dbgGlobalInfo.bUseLog = FALSE;
  82. }
  83. else
  84. {
  85. fputs(str,pfLog);
  86. fclose(pfLog);
  87. }
  88. }
  89. }
  90. #define DBG_TRACE3(format,arg1,arg2,arg3)\
  91. {\
  92. static char sz[MAX_TRACE_LINE];\
  93. wsprintfA(sz,"[%s] %s(): "format"\r\n",(LPSTR)__dbgGlobalInfo.szModuleName,(LPSTR)__dbgContextInfo.szProcName,arg1,arg2,arg3);\
  94. OUTPUT_DEBUG_STRING(sz);\
  95. }
  96. #define DBG_TRACE(str) DBG_TRACE3(str "%c%c%c",(' '),(' '),(' '))
  97. #define DBG_TRACE1(format,arg) DBG_TRACE3(format "%c%c",arg,(' '),(' '))
  98. #define DBG_TRACE2(format,arg1,arg2) DBG_TRACE3(format "%c",arg1,arg2,(' '))
  99. #ifdef ASSERT_ON_REENTRANCY
  100. #define CHECK_REENTRANCY()\
  101. if (++ __dbgContextInfo.iNumEntries > 1)\
  102. {\
  103. DBG_TRACE1("WARNING: reentrancy occured",__dbgContextInfo.iNumEntries);\
  104. DBG_MESSAGE_BOX1("WARNING: reentrancy occured",__dbgContextInfo.iNumEntries);\
  105. }
  106. #else //ASSERT_ON_REENTRANCY
  107. #define CHECK_REENTRANCY()
  108. #endif //ASSERT_ON_REENTRANCY
  109. #define DBG_PROC_ENTRY(pname) static DBG_CONTEXT_INFO __dbgContextInfo = { pname , FALSE,0};\
  110. OUTPUT_DEBUG_STRING("> ");\
  111. DBG_TRACE("Enter");\
  112. CHECK_REENTRANCY();
  113. #define SDBG_PROC_ENTRY(pname) static DBG_CONTEXT_INFO __dbgContextInfo = { pname , TRUE,0 };\
  114. CHECK_REENTRANCY();
  115. #define RETURN for(__dbgProcExit(__dbgContextInfo),--__dbgContextInfo.iNumEntries;TRUE;) return
  116. #ifdef POPUPS
  117. #define DBG_CALL_FAIL(fname,rc)\
  118. {\
  119. DWORD dwRc = rc;\
  120. if(rc)\
  121. {\
  122. DBG_TRACE2(__FILE__ "(%d) : Error 0x%lx: "fname" failed",__LINE__,dwRc);\
  123. DBG_MESSAGE_BOX1("Error 0x%lx:"fname" failed",dwRc);\
  124. }\
  125. else\
  126. {\
  127. DBG_TRACE1(__FILE__ "(%d) : "fname" failed",__LINE__);\
  128. DBG_MESSAGE_BOX(fname" failed");\
  129. }\
  130. }
  131. #else //POPUPS
  132. #define DBG_CALL_FAIL(fname,rc)\
  133. {\
  134. if(rc)\
  135. {\
  136. DBG_TRACE1(__FILE__ "(" __LINE__ ") : Error 0x%lx: "fname" failed",rc);\
  137. }\
  138. else\
  139. {\
  140. DBG_TRACE(__FILE__ "(" __LINE__ ") : "fname" failed");\
  141. }\
  142. }
  143. #endif //POPUPS
  144. #define ASSERT(boolexp)\
  145. {\
  146. if ((boolexp) == FALSE) \
  147. {\
  148. DBG_MESSAGE_BOX("ASSERT FAILED: "#boolexp);\
  149. }\
  150. }
  151. #define DBG_DECLARE_MODULE(modulename)\
  152. DBG_GLOBAL_INFO __dbgGlobalInfo = {"",modulename,TRUE,FALSE}
  153. void __inline FAR pascal
  154. __dbgProcExit(DBG_CONTEXT_INFO __dbgContextInfo)
  155. {
  156. if (__dbgContextInfo.fSilent == TRUE)
  157. return;
  158. OUTPUT_DEBUG_STRING("< ");
  159. DBG_TRACE("Exit");
  160. }
  161. ULONG __inline __cdecl
  162. DbgPrint(char *format, ...)
  163. {
  164. va_list va;
  165. char sz[MAX_TRACE_LINE]={0};
  166. SDBG_PROC_ENTRY("DEBUG-MESSAGE");
  167. va_start(va, format);
  168. _vsnprintf(sz,ARR_SIZE(sz)-1,format,va);
  169. va_end(va);
  170. OUTPUT_DEBUG_STRING(sz);
  171. return 0;
  172. }
  173. void __inline
  174. DbgBreakPoint()
  175. {
  176. SDBG_PROC_ENTRY("DEBUG-ASSERT");
  177. ASSERT(FALSE);
  178. return;
  179. }
  180. #else // DBG_DEBUG
  181. #define DBG_MESSAGE_BOX
  182. #define DBG_MESSAGE_BOX1(a,b)
  183. #define DBG_MESSAGE_BOX2(a,b,c)
  184. #define DBG_MESSAGE_BOX3(a,b,c,d)
  185. #define OUTPUT_DEBUG_STRING(a)
  186. #define DBG_TRACE(a)
  187. #define DBG_TRACE1(a,b)
  188. #define DBG_TRACE2(a,b,c)
  189. #define DBG_TRACE3(a,b,c,d)
  190. #define DBG_PROC_ENTRY(a)
  191. #define SDBG_PROC_ENTRY(a)
  192. #define RETURN return
  193. #define DBG_CALL_FAIL(a,b)
  194. #define DBG_DECLARE_MODULE(a)
  195. #define ASSERT(a)
  196. #endif// DBG_DEBUG
  197. #endif //__UTILS__DBGTRACE_H