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.

392 lines
15 KiB

  1. /*
  2. * johnkn's debug logging and assert macros
  3. *
  4. */
  5. #ifdef __cplusplus
  6. extern "C" { // Assume C declarations for C++
  7. #endif // __cplusplus
  8. #if !defined _INC_MMDEBUG_
  9. #define _INC_MMDEBUG_
  10. //
  11. // prototypes for debug functions.
  12. //
  13. #define SQUAWKNUMZ(num) #num
  14. #define SQUAWKNUM(num) SQUAWKNUMZ(num)
  15. #define SQUAWK __FILE__ "(" SQUAWKNUM(__LINE__) ") ----"
  16. #define DEBUGLINE __FILE__ "(" SQUAWKNUM(__LINE__) ") "
  17. #if defined DEBUG || defined _DEBUG || defined DEBUG_RETAIL
  18. int WINAPI AuxDebugEx(int, LPTSTR, ...);
  19. VOID WINAPI AuxDebugDump (int, LPVOID, int);
  20. int WINAPI DebugSetOutputLevel (int);
  21. LPCTSTR WINAPI AuxMMErrText (DWORD mmr);
  22. #if defined DEBUG_RETAIL
  23. #define INLINE_BREAK
  24. #else
  25. #define INLINE_BREAK _asm {int 3}
  26. #endif
  27. #if 0
  28. #undef assert
  29. #define assert(exp) \
  30. (void)((exp) ? 0 : AuxDebugEx(-2, DEBUGLINE "assert failed: " #exp "\r\n", (int)__LINE__))
  31. #undef assert2
  32. #define assert2(exp,sz) \
  33. (void)((exp) ? 0 : AuxDebugEx(-2, DEBUGLINE "assert failed: " sz "\r\n", (int)__LINE__))
  34. #else
  35. #undef assert
  36. #define assert(exp); {\
  37. if (!(exp)) {\
  38. AuxDebugEx(-2, DEBUGLINE "assert failed: " #exp "\r\n", (int)__LINE__); \
  39. INLINE_BREAK;\
  40. }\
  41. }
  42. #undef assert2
  43. #define assert2(exp,sz); {\
  44. if (!(exp)) {\
  45. AuxDebugEx(-2, DEBUGLINE "assert failed: " sz "\r\n", (int)__LINE__); \
  46. INLINE_BREAK;\
  47. }\
  48. }
  49. #undef assert3
  50. #define assert3(exp,sz,arg); {\
  51. if (!(exp)) {\
  52. AuxDebugEx(-2, DEBUGLINE "assert failed: " sz "\r\n", (int)__LINE__, (arg)); \
  53. INLINE_BREAK;\
  54. }\
  55. }
  56. #endif
  57. #define AuxMMR(api,mmr) (mmr) ? AuxDebugEx(1, DEBUGLINE #api " error %d '%s'\r\n", mmr, AuxMMErrText(mmr)) : (int)0
  58. #else // defined(DEBUG)
  59. #define AuxDebugEx 1 ? (void)0 :
  60. #define AuxDebugDump(a,b,c)
  61. #define assert(a) ((void)0)
  62. #define assert2(a,b) ((void)0)
  63. #define assert3(a,b,c) ((void)0)
  64. #define INLINE_BREAK
  65. #define DebugSetOutputLevel(i)
  66. #define AuxMMErrText(mmr)
  67. #define AuxMMR(api,mmr)
  68. #endif // defined(DEBUG)
  69. #define AuxDebug(sz) AuxDebugEx (1, DEBUGLINE sz "\r\n")
  70. #define AuxDebug2(sz,a) AuxDebugEx (1, DEBUGLINE sz "\r\n", (a))
  71. #ifdef __cplusplus
  72. } // Assume C declarations for C++
  73. #endif // __cplusplus
  74. #endif //_INC_MMDEBUG_
  75. // =============================================================================
  76. //
  77. // include this in only one module in a DLL or APP
  78. //
  79. #if defined DEBUG || defined _DEBUG || defined DEBUG_RETAIL
  80. #if (defined _INC_MMDEBUG_CODE_) && (_INC_MMDEBUG_CODE_ != FALSE)
  81. #undef _INC_MMDEBUG_CODE_
  82. #define _INC_MMDEBUG_CODE_ FALSE
  83. #include <stdarg.h>
  84. int debug_OutputOn = 0;
  85. /*+ AuxDebug - create a formatted string and output to debug terminal
  86. *
  87. *-=================================================================*/
  88. int WINAPI AuxDebugEx (
  89. int iLevel,
  90. LPTSTR lpFormat,
  91. ...)
  92. {
  93. char szBuf[1024];
  94. int cb;
  95. va_list va;
  96. if (debug_OutputOn >= iLevel)
  97. {
  98. va_start (va, lpFormat);
  99. cb = wvsprintfA (szBuf, lpFormat, va);
  100. va_end (va);
  101. OutputDebugString (szBuf);
  102. }
  103. return cb;
  104. }
  105. /*+ AuxDebugDump -
  106. *
  107. *-=================================================================*/
  108. VOID WINAPI AuxDebugDump (
  109. int iLevel,
  110. LPVOID lpvData,
  111. int nCount)
  112. {
  113. LPBYTE lpData = lpvData;
  114. char szBuf[128];
  115. char * psz;
  116. int cb;
  117. int ix;
  118. BYTE abRow[8];
  119. if (debug_OutputOn <= iLevel || nCount <= 0)
  120. return;
  121. do {
  122. cb = wsprintf (szBuf, "\t%08X: ", lpData);
  123. psz = szBuf + cb;
  124. for (ix = 0; ix < 8; ++ix)
  125. {
  126. LPBYTE lpb = lpData;
  127. abRow[ix] = '.';
  128. if (IsBadReadPtr (lpData + ix, 1))
  129. lstrcpy (psz, ".. ");
  130. else
  131. {
  132. wsprintf (psz, "%02X ", lpData[ix]);
  133. if (lpData[ix] >= 32 && lpData[ix] < 127)
  134. abRow[ix] = lpData[ix];
  135. }
  136. psz += 3;
  137. }
  138. for (ix = 0; ix < 8; ++ix)
  139. *psz++ = abRow[ix];
  140. lstrcpy (psz, "\r\n");
  141. OutputDebugString (szBuf);
  142. } while (lpData += 8, (nCount -= 8) > 0);
  143. return;
  144. }
  145. /*+ AuxMMErrText
  146. *
  147. *-=================================================================*/
  148. LPCTSTR WINAPI AuxMMErrText (
  149. DWORD mmr)
  150. {
  151. static struct _mmerrors {
  152. DWORD mmr;
  153. LPCTSTR psz;
  154. } aMMErr[] = {
  155. MMSYSERR_NOERROR ,"Success",
  156. MMSYSERR_ERROR ,"unspecified error",
  157. MMSYSERR_BADDEVICEID ,"device ID out of range",
  158. MMSYSERR_NOTENABLED ,"driver failed enable",
  159. MMSYSERR_ALLOCATED ,"device already allocated",
  160. MMSYSERR_INVALHANDLE ,"device handle is invalid",
  161. MMSYSERR_NODRIVER ,"no device driver present",
  162. MMSYSERR_NOMEM ,"memory allocation error",
  163. MMSYSERR_NOTSUPPORTED ,"function isn't supported",
  164. MMSYSERR_BADERRNUM ,"error value out of range",
  165. MMSYSERR_INVALFLAG ,"invalid flag passed",
  166. MMSYSERR_INVALPARAM ,"invalid parameter passed",
  167. #if (WINVER >= 0x0400)
  168. MMSYSERR_HANDLEBUSY ,"handle in use by another thread",
  169. MMSYSERR_INVALIDALIAS ,"specified alias not found",
  170. MMSYSERR_BADDB ,"bad registry database",
  171. MMSYSERR_KEYNOTFOUND ,"registry key not found",
  172. MMSYSERR_READERROR ,"registry read error",
  173. MMSYSERR_WRITEERROR ,"registry write error",
  174. MMSYSERR_DELETEERROR ,"registry delete error",
  175. MMSYSERR_VALNOTFOUND ,"registry value not found",
  176. #endif
  177. WAVERR_BADFORMAT ,"wave:unsupported wave format",
  178. WAVERR_STILLPLAYING ,"wave:still something playing",
  179. WAVERR_UNPREPARED ,"wave:header not prepared",
  180. WAVERR_SYNC ,"wave:device is synchronous",
  181. MIDIERR_UNPREPARED ,"midi:header not prepared",
  182. MIDIERR_STILLPLAYING ,"midi:still something playing",
  183. //MIDIERR_NOMAP ,"midi:no configured instruments",
  184. MIDIERR_NOTREADY ,"midi:hardware is still busy",
  185. MIDIERR_NODEVICE ,"midi:port no longer connected",
  186. MIDIERR_INVALIDSETUP ,"midi:invalid MIF",
  187. MIDIERR_BADOPENMODE ,"midi:operation unsupported w/ open mode",
  188. TIMERR_NOCANDO ,"timer: request not completed",
  189. JOYERR_PARMS ,"joy:bad parameters",
  190. JOYERR_NOCANDO ,"joy:request not completed",
  191. JOYERR_UNPLUGGED ,"joystick is unplugged",
  192. MCIERR_INVALID_DEVICE_ID ,"MCIERR_INVALID_DEVICE_ID",
  193. MCIERR_UNRECOGNIZED_KEYWORD ,"MCIERR_UNRECOGNIZED_KEYWORD",
  194. MCIERR_UNRECOGNIZED_COMMAND ,"MCIERR_UNRECOGNIZED_COMMAND",
  195. MCIERR_HARDWARE ,"MCIERR_HARDWARE",
  196. MCIERR_INVALID_DEVICE_NAME ,"MCIERR_INVALID_DEVICE_NAME",
  197. MCIERR_OUT_OF_MEMORY ,"MCIERR_OUT_OF_MEMORY",
  198. MCIERR_DEVICE_OPEN ,"MCIERR_DEVICE_OPEN",
  199. MCIERR_CANNOT_LOAD_DRIVER ,"MCIERR_CANNOT_LOAD_DRIVER",
  200. MCIERR_MISSING_COMMAND_STRING ,"MCIERR_MISSING_COMMAND_STRING",
  201. MCIERR_PARAM_OVERFLOW ,"MCIERR_PARAM_OVERFLOW",
  202. MCIERR_MISSING_STRING_ARGUMENT ,"MCIERR_MISSING_STRING_ARGUMENT",
  203. MCIERR_BAD_INTEGER ,"MCIERR_BAD_INTEGER",
  204. MCIERR_PARSER_INTERNAL ,"MCIERR_PARSER_INTERNAL",
  205. MCIERR_DRIVER_INTERNAL ,"MCIERR_DRIVER_INTERNAL",
  206. MCIERR_MISSING_PARAMETER ,"MCIERR_MISSING_PARAMETER",
  207. MCIERR_UNSUPPORTED_FUNCTION ,"MCIERR_UNSUPPORTED_FUNCTION",
  208. MCIERR_FILE_NOT_FOUND ,"MCIERR_FILE_NOT_FOUND",
  209. MCIERR_DEVICE_NOT_READY ,"MCIERR_DEVICE_NOT_READY",
  210. MCIERR_INTERNAL ,"MCIERR_INTERNAL",
  211. MCIERR_DRIVER ,"MCIERR_DRIVER",
  212. MCIERR_CANNOT_USE_ALL ,"MCIERR_CANNOT_USE_ALL",
  213. MCIERR_MULTIPLE ,"MCIERR_MULTIPLE",
  214. MCIERR_EXTENSION_NOT_FOUND ,"MCIERR_EXTENSION_NOT_FOUND",
  215. MCIERR_OUTOFRANGE ,"MCIERR_OUTOFRANGE",
  216. MCIERR_FLAGS_NOT_COMPATIBLE ,"MCIERR_FLAGS_NOT_COMPATIBLE",
  217. MCIERR_FILE_NOT_SAVED ,"MCIERR_FILE_NOT_SAVED",
  218. MCIERR_DEVICE_TYPE_REQUIRED ,"MCIERR_DEVICE_TYPE_REQUIRED",
  219. MCIERR_DEVICE_LOCKED ,"MCIERR_DEVICE_LOCKED",
  220. MCIERR_DUPLICATE_ALIAS ,"MCIERR_DUPLICATE_ALIAS",
  221. MCIERR_BAD_CONSTANT ,"MCIERR_BAD_CONSTANT",
  222. MCIERR_MUST_USE_SHAREABLE ,"MCIERR_MUST_USE_SHAREABLE",
  223. MCIERR_MISSING_DEVICE_NAME ,"MCIERR_MISSING_DEVICE_NAME",
  224. MCIERR_BAD_TIME_FORMAT ,"MCIERR_BAD_TIME_FORMAT",
  225. MCIERR_NO_CLOSING_QUOTE ,"MCIERR_NO_CLOSING_QUOTE",
  226. MCIERR_DUPLICATE_FLAGS ,"MCIERR_DUPLICATE_FLAGS",
  227. MCIERR_INVALID_FILE ,"MCIERR_INVALID_FILE",
  228. MCIERR_NULL_PARAMETER_BLOCK ,"MCIERR_NULL_PARAMETER_BLOCK",
  229. MCIERR_UNNAMED_RESOURCE ,"MCIERR_UNNAMED_RESOURCE",
  230. MCIERR_NEW_REQUIRES_ALIAS ,"MCIERR_NEW_REQUIRES_ALIAS",
  231. MCIERR_NOTIFY_ON_AUTO_OPEN ,"MCIERR_NOTIFY_ON_AUTO_OPEN",
  232. MCIERR_NO_ELEMENT_ALLOWED ,"MCIERR_NO_ELEMENT_ALLOWED",
  233. MCIERR_NONAPPLICABLE_FUNCTION ,"MCIERR_NONAPPLICABLE_FUNCTION",
  234. MCIERR_ILLEGAL_FOR_AUTO_OPEN ,"MCIERR_ILLEGAL_FOR_AUTO_OPEN",
  235. MCIERR_FILENAME_REQUIRED ,"MCIERR_FILENAME_REQUIRED",
  236. MCIERR_EXTRA_CHARACTERS ,"MCIERR_EXTRA_CHARACTERS",
  237. MCIERR_DEVICE_NOT_INSTALLED ,"MCIERR_DEVICE_NOT_INSTALLED",
  238. MCIERR_GET_CD ,"MCIERR_GET_CD",
  239. MCIERR_SET_CD ,"MCIERR_SET_CD",
  240. MCIERR_SET_DRIVE ,"MCIERR_SET_DRIVE",
  241. MCIERR_DEVICE_LENGTH ,"MCIERR_DEVICE_LENGTH",
  242. MCIERR_DEVICE_ORD_LENGTH ,"MCIERR_DEVICE_ORD_LENGTH",
  243. MCIERR_NO_INTEGER ,"MCIERR_NO_INTEGER",
  244. MCIERR_WAVE_OUTPUTSINUSE ,"MCIERR_WAVE_OUTPUTSINUSE",
  245. MCIERR_WAVE_SETOUTPUTINUSE ,"MCIERR_WAVE_SETOUTPUTINUSE",
  246. MCIERR_WAVE_INPUTSINUSE ,"MCIERR_WAVE_INPUTSINUSE",
  247. MCIERR_WAVE_SETINPUTINUSE ,"MCIERR_WAVE_SETINPUTINUSE",
  248. MCIERR_WAVE_OUTPUTUNSPECIFIED ,"MCIERR_WAVE_OUTPUTUNSPECIFIED",
  249. MCIERR_WAVE_INPUTUNSPECIFIED ,"MCIERR_WAVE_INPUTUNSPECIFIED",
  250. MCIERR_WAVE_OUTPUTSUNSUITABLE ,"MCIERR_WAVE_OUTPUTSUNSUITABLE",
  251. MCIERR_WAVE_SETOUTPUTUNSUITABLE ,"MCIERR_WAVE_SETOUTPUTUNSUITABLE",
  252. MCIERR_WAVE_INPUTSUNSUITABLE ,"MCIERR_WAVE_INPUTSUNSUITABLE",
  253. MCIERR_WAVE_SETINPUTUNSUITABLE ,"MCIERR_WAVE_SETINPUTUNSUITABLE",
  254. MCIERR_SEQ_DIV_INCOMPATIBLE ,"MCIERR_SEQ_DIV_INCOMPATIBLE",
  255. MCIERR_SEQ_PORT_INUSE ,"MCIERR_SEQ_PORT_INUSE",
  256. MCIERR_SEQ_PORT_NONEXISTENT ,"MCIERR_SEQ_PORT_NONEXISTENT",
  257. MCIERR_SEQ_PORT_MAPNODEVICE ,"MCIERR_SEQ_PORT_MAPNODEVICE",
  258. MCIERR_SEQ_PORT_MISCERROR ,"MCIERR_SEQ_PORT_MISCERROR",
  259. MCIERR_SEQ_TIMER ,"MCIERR_SEQ_TIMER",
  260. MCIERR_SEQ_PORTUNSPECIFIED ,"MCIERR_SEQ_PORTUNSPECIFIED",
  261. MCIERR_SEQ_NOMIDIPRESENT ,"MCIERR_SEQ_NOMIDIPRESENT",
  262. MCIERR_NO_WINDOW ,"MCIERR_NO_WINDOW",
  263. MCIERR_CREATEWINDOW ,"MCIERR_CREATEWINDOW",
  264. MCIERR_FILE_READ ,"MCIERR_FILE_READ",
  265. MCIERR_FILE_WRITE ,"MCIERR_FILE_WRITE",
  266. MCIERR_NO_IDENTITY ,"MCIERR_NO_IDENTITY",
  267. MIXERR_INVALLINE ,"Invalid Mixer Line",
  268. MIXERR_INVALCONTROL ,"Invalid Mixer Control",
  269. MIXERR_INVALVALUE ,"Invalid Mixer Value",
  270. MIXERR_INVALVALUE+1 , "unknown error %d"
  271. };
  272. UINT uRemain = sizeof(aMMErr)/sizeof(aMMErr[0]);
  273. UINT uUpper = uRemain-1;
  274. UINT uLower = 0;
  275. static char szTemp[50];
  276. if (mmr <= aMMErr[uUpper].mmr)
  277. {
  278. // binary search for mmr match, if match
  279. // return string pointer
  280. //
  281. while (--uRemain)
  282. {
  283. UINT ii = (uLower + uUpper) >> 1;
  284. if (aMMErr[ii].mmr < mmr)
  285. {
  286. if (uLower == ii)
  287. break;
  288. uLower = ii;
  289. }
  290. else if (aMMErr[ii].mmr > mmr)
  291. {
  292. if (uUpper == ii)
  293. break;
  294. uUpper = ii;
  295. }
  296. else
  297. {
  298. return aMMErr[ii].psz;
  299. break;
  300. }
  301. }
  302. // we can only get to here if no match was found for
  303. // the error id.
  304. //
  305. if ( ! uRemain)
  306. {
  307. int ix;
  308. INLINE_BREAK;
  309. for (ix = 0; ix < sizeof(aMMErr)/sizeof(aMMErr[0])-1; ++ix)
  310. {
  311. assert (aMMErr[ix].mmr < aMMErr[ix+1].mmr);
  312. }
  313. lstrcpy (szTemp, "#### Fatal Error! error table not sorted!");
  314. return szTemp;
  315. }
  316. }
  317. wsprintf (szTemp, aMMErr[uUpper].psz, mmr);
  318. return szTemp;
  319. }
  320. /*+ DebugSetOutputLevel
  321. *
  322. *-=================================================================*/
  323. BOOL WINAPI DebugSetOutputLevel (
  324. int nLevel)
  325. {
  326. int nOldLevel = debug_OutputOn;
  327. debug_OutputOn = nLevel;
  328. return nOldLevel;
  329. }
  330. #endif // _INC_MMDEBUG_CODE_
  331. #endif // DEBUG || _DEBUG
  332. #ifdef __cplusplus
  333. } // Assume C declarations for C++
  334. #endif // __cplusplus