Source code of Windows XP (NT5)
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.

244 lines
6.4 KiB

  1. /*
  2. * johnkn's debug logging and assert macros
  3. *
  4. */
  5. #if !defined _INC_MMDEBUG_
  6. #define _INC_MMDEBUG_
  7. //
  8. // prototypes for debug functions.
  9. //
  10. #define SQUAWKNUMZ(num) TEXT(#num)
  11. #define SQUAWKNUM(num) SQUAWKNUMZ(num)
  12. #define SQUAWK TEXT(__FILE__) TEXT("(") SQUAWKNUM(__LINE__) TEXT(") ----")
  13. #define DEBUGLINE TEXT(__FILE__) TEXT("(") SQUAWKNUM(__LINE__) TEXT(") ")
  14. #if defined DEBUG || defined _DEBUG || defined DEBUG_RETAIL
  15. int FAR _cdecl AuxDebugEx(int, LPTSTR, ...);
  16. VOID WINAPI AuxDebugDump (int, LPVOID, int);
  17. int WINAPI DebugSetOutputLevel (int);
  18. #if defined DEBUG_RETAIL
  19. #define INLINE_BREAK
  20. #else
  21. #if !defined _WIN32 || defined _X86_
  22. #define INLINE_BREAK _asm {int 3}
  23. #else
  24. #define INLINE_BREAK DebugBreak()
  25. #endif
  26. #endif
  27. #undef assert
  28. #define assert(exp) {\
  29. if (!(exp)) {\
  30. AuxDebugEx(-2, DEBUGLINE TEXT("assert failed: ") TEXT(#exp) TEXT("\r\n")); \
  31. INLINE_BREAK;\
  32. }\
  33. }
  34. #undef assert2
  35. #define assert2(exp,sz) {\
  36. if (!(exp)) {\
  37. AuxDebugEx(-2, DEBUGLINE TEXT("assert failed: ") sz TEXT("\r\n")); \
  38. INLINE_BREAK;\
  39. }\
  40. }
  41. #undef assert3
  42. #define assert3(exp,sz,arg) {\
  43. if (!(exp)) {\
  44. AuxDebugEx(-2, DEBUGLINE TEXT("assert failed: ") sz TEXT("\r\n"), (arg)); \
  45. INLINE_BREAK;\
  46. }\
  47. }
  48. #undef assert4
  49. #define assert4(exp,sz,arg1,arg2) {\
  50. if (!(exp)) {\
  51. AuxDebugEx(-2, DEBUGLINE TEXT("assert failed: ") sz TEXT("\r\n"), (arg1),(arg2)); \
  52. INLINE_BREAK;\
  53. }\
  54. }
  55. #undef assert5
  56. #define assert5(exp,sz,arg1,arg2,arg3) {\
  57. if (!(exp)) {\
  58. AuxDebugEx(-2, DEBUGLINE TEXT("assert failed: ") sz TEXT("\r\n"), (arg1),(arg2),(arg3)); \
  59. INLINE_BREAK;\
  60. }\
  61. }
  62. #define STATICFN
  63. #else // defined(DEBUG)
  64. #define AuxDebugEx 1 ? (void)0 :
  65. #define AuxDebugDump(a,b,c)
  66. #define assert(a) ((void)0)
  67. #define assert2(a,b) ((void)0)
  68. #define assert3(a,b,c) ((void)0)
  69. #define assert4(a,b,c,d) ((void)0)
  70. #define assert5(a,b,c,d,e) ((void)0)
  71. #define INLINE_BREAK
  72. #define DebugSetOutputLevel(i)
  73. #define STATICFN static
  74. #endif // defined(DEBUG)
  75. #define AuxDebug(sz) AuxDebugEx (1, DEBUGLINE sz TEXT("\r\n"))
  76. #define AuxDebug2(sz,a) AuxDebugEx (1, DEBUGLINE sz TEXT("\r\n"), (a))
  77. #endif //_INC_MMDEBUG_
  78. // =============================================================================
  79. //
  80. // include this in only one module in a DLL or APP
  81. //
  82. #if defined DEBUG || defined _DEBUG || defined DEBUG_RETAIL
  83. #if (defined _INC_MMDEBUG_CODE_) && (_INC_MMDEBUG_CODE_ != FALSE)
  84. #undef _INC_MMDEBUG_CODE_
  85. #define _INC_MMDEBUG_CODE_ FALSE
  86. #include <stdarg.h>
  87. #if !defined _WIN32 && !defined wvsprintfA
  88. #define wvsprintfA wvsprintf
  89. #endif
  90. int mmdebug_OutputLevel = 0;
  91. /*+ AuxDebug - create a formatted string and output to debug terminal
  92. *
  93. *-=================================================================*/
  94. int FAR _cdecl AuxDebugEx (
  95. int iLevel,
  96. LPTSTR lpFormat,
  97. ...)
  98. {
  99. TCHAR szBuf[1024];
  100. int cb;
  101. va_list va;
  102. TCHAR * psz;
  103. if (mmdebug_OutputLevel >= iLevel)
  104. {
  105. va_start (va, lpFormat);
  106. cb = wvsprintf (szBuf, lpFormat, va);
  107. va_end (va);
  108. // eat leading ..\..\ which we get from __FILE__ since
  109. // george's wierd generic makefile stuff.
  110. //
  111. psz = szBuf;
  112. while (psz[0] == TEXT('.') && psz[1] == TEXT('.') && psz[2] == TEXT('\\'))
  113. psz += 3;
  114. #ifdef MODULE_DEBUG_PREFIX
  115. OutputDebugString (MODULE_DEBUG_PREFIX);
  116. #endif
  117. OutputDebugString (psz);
  118. }
  119. return cb;
  120. }
  121. /*+ AuxDebugDump -
  122. *
  123. *-=================================================================*/
  124. VOID WINAPI AuxDebugDump (
  125. int iLevel,
  126. LPVOID lpvData,
  127. int nCount)
  128. {
  129. LPBYTE lpData = NULL;
  130. TCHAR szBuf[128];
  131. LPTSTR psz;
  132. int cb;
  133. int ix;
  134. BYTE abRow[8];
  135. if (mmdebug_OutputLevel <= iLevel || nCount <= 0)
  136. return;
  137. do {
  138. cb = wsprintf (szBuf, TEXT("\t%08X: "), lpData);
  139. psz = szBuf + cb;
  140. for (ix = 0; ix < 8; ++ix)
  141. {
  142. LPBYTE lpb = lpData;
  143. abRow[ix] = TEXT('.');
  144. if (IsBadReadPtr (lpData + ix, 1))
  145. lstrcpy (psz, TEXT(".. "));
  146. else
  147. {
  148. wsprintf (psz, TEXT("%02X "), lpData[ix]);
  149. if (lpData[ix] >= 32 && lpData[ix] < 127)
  150. abRow[ix] = lpData[ix];
  151. }
  152. psz += 3;
  153. }
  154. for (ix = 0; ix < 8; ++ix)
  155. *psz++ = abRow[ix];
  156. lstrcpy (psz, TEXT("\r\n"));
  157. #ifdef MODULE_DEBUG_PREFIX
  158. OutputDebugString (MODULE_DEBUG_PREFIX);
  159. #endif
  160. OutputDebugString (szBuf);
  161. } while (lpData += 8, (nCount -= 8) > 0);
  162. return;
  163. }
  164. /*+ DebugSetOutputLevel
  165. *
  166. *-=================================================================*/
  167. BOOL WINAPI DebugSetOutputLevel (
  168. int nLevel)
  169. {
  170. int nOldLevel = mmdebug_OutputLevel;
  171. mmdebug_OutputLevel = nLevel;
  172. return nOldLevel;
  173. }
  174. void FAR cdecl dprintf(LPSTR szFormat, ...)
  175. {
  176. TCHAR ach[MAXSTRINGLEN];
  177. TCHAR szUniFormat[MAXSTRINGLEN];
  178. int s,d;
  179. va_list arg;
  180. MultiByteToWideChar(GetACP(), 0,
  181. szFormat, -1,
  182. szUniFormat, sizeof(szUniFormat)/sizeof(TCHAR));
  183. va_start (arg, szUniFormat);
  184. s = wvsprintf (ach,szUniFormat,arg);
  185. va_end(arg);
  186. for (d=sizeof(ach)-1; s>=0; s--)
  187. {
  188. if ((ach[d--] = ach[s]) == TEXT('\n'))
  189. ach[d--] = TEXT('\r');
  190. }
  191. va_end(arg);
  192. OutputDebugStr(TEXT("MMSYS.CPL: "));
  193. OutputDebugStr(ach+d+1);
  194. }
  195. #endif // _INC_MMDEBUG_CODE_
  196. #endif // DEBUG || _DEBUG