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.

263 lines
5.7 KiB

  1. /*
  2. - debug.c
  3. -
  4. * Microsoft Internet Phone
  5. * Debug functions
  6. *
  7. * Revision History:
  8. *
  9. * When Who What
  10. * -------- ------------------ ---------------------------------------
  11. * 11.16.95 Yoram Yaacovi Copied from the mstools tapicomm misc.c
  12. * 11.16.95 Yoram Yaacovi Modified for iphone
  13. * 1.10.96 Yoram Yaacovi Added conditional trace and file debug output
  14. *
  15. * Functions:
  16. * DebugPrintFileLine
  17. * DebugPrintf
  18. * DebugTrap
  19. *
  20. */
  21. //#include "mpswab.h"
  22. #include "_apipch.h"
  23. #pragma warning(disable:4212) // nonstd extension: ellipsis used
  24. #if defined(DEBUG) || defined(TEST)
  25. extern BOOL fTrace; // Set TRUE if you want debug traces
  26. extern BOOL fDebugTrap; // Set TRUE to get int3's
  27. extern TCHAR szDebugOutputFile[MAX_PATH]; // the name of the debug output file
  28. /*
  29. * FUNCTION: DebugPrintFileLine(..)
  30. *
  31. * PURPOSE: Pretty print a trace or error message to the debugging output.
  32. *
  33. * PARAMETERS:
  34. * dwParam - One dword parameter (can be the error code)
  35. * pszPrefix - String to prepend to the printed message.
  36. * szFileName - Filename the error occured in.
  37. * nLineNumber - Line number the error occured at.
  38. *
  39. * RETURN VALUE:
  40. * none
  41. *
  42. * COMMENTS:
  43. * If szFileName == NULL, then the File and Line are not printed.
  44. *
  45. */
  46. void DebugPrintFileLine(
  47. DWORD dwParam, LPTSTR szPrefix,
  48. LPTSTR szFileName, DWORD nLineNumber)
  49. {
  50. TCHAR szBuffer[256];
  51. if (szPrefix == NULL)
  52. szPrefix = szEmpty;
  53. // Pretty print the error message.
  54. // <not done yet>
  55. // If szFileName, then use it; else don't.
  56. if (szFileName != NULL)
  57. {
  58. wnsprintf(szBuffer, ARRAYSIZE(szBuffer),
  59. TEXT("%s: \"%x\" in File \"%s\", Line %d\r\n"),
  60. szPrefix, dwParam, szFileName, nLineNumber);
  61. }
  62. else
  63. {
  64. wnsprintf(szBuffer, ARRAYSIZE(szBuffer), TEXT("%s: \"%x\"\r\n"), szPrefix, dwParam);
  65. }
  66. // Print it!
  67. OutputDebugString(szBuffer);
  68. return;
  69. }
  70. /*
  71. * FUNCTION: DebugPrintfError(LPTSTR, ...)
  72. *
  73. * PURPOSE: a wrapper around DebugPrintf for error cases
  74. *
  75. * PARAMETERS:
  76. * lpszFormat - the same as printf
  77. *
  78. * RETURN VALUE:
  79. * none.
  80. *
  81. * COMMENTS:
  82. *
  83. */
  84. void __cdecl DebugPrintfError(LPTSTR lpszFormat, ...)
  85. {
  86. va_list v1;
  87. va_start(v1, lpszFormat);
  88. DebugPrintf(AVERROR, lpszFormat, v1);
  89. }
  90. /*
  91. * FUNCTION: DebugPrintfTrace(LPTSTR, ...)
  92. *
  93. * PURPOSE: a wrapper around DebugPrintf for the trace case
  94. *
  95. * PARAMETERS:
  96. * lpszFormat - the same as printf
  97. *
  98. * RETURN VALUE:
  99. * none.
  100. *
  101. * COMMENTS:
  102. *
  103. */
  104. void __cdecl DebugPrintfTrace(LPTSTR lpszFormat, ...)
  105. {
  106. va_list v1;
  107. va_start(v1, lpszFormat);
  108. DebugPrintf(AVTRACE, lpszFormat, v1);
  109. }
  110. /*
  111. * FUNCTION: DebugPrintf(ULONG ulFlags, LPTSTR, va_list)
  112. *
  113. * PURPOSE: wsprintfA to the debugging output.
  114. *
  115. * PARAMETERS:
  116. * ulFlags - trace, error, zone information
  117. * lpszFormat - the same as printf
  118. *
  119. * RETURN VALUE:
  120. * none.
  121. *
  122. * COMMENTS:
  123. *
  124. */
  125. #ifdef WIN16
  126. void __cdecl DebugPrintf(ULONG ulFlags, LPTSTR lpszFormat, ...)
  127. #else
  128. void __cdecl DebugPrintf(ULONG ulFlags, LPTSTR lpszFormat, va_list v1)
  129. #endif
  130. {
  131. TCHAR szOutput[512];
  132. LPTSTR lpszOutput=szOutput;
  133. DWORD dwSize;
  134. #ifdef WIN16
  135. va_list v1;
  136. va_start(v1, lpszFormat);
  137. #endif
  138. // if error, start the string with "ERROR:"
  139. if (ulFlags == AVERROR)
  140. {
  141. StrCpyN(lpszOutput, TEXT("ERROR: "), ARRAYSIZE(szOutput));
  142. lpszOutput = lpszOutput+lstrlen(lpszOutput);
  143. }
  144. // if trace, and tracing not enabled, leave
  145. if ((ulFlags == AVTRACE) && !fTrace)
  146. goto out;
  147. dwSize = (ARRAYSIZE(szOutput) - (DWORD)(lpszOutput - szOutput));
  148. dwSize = wvnsprintf(lpszOutput, dwSize, lpszFormat, v1);
  149. // Append carriage return, if necessary
  150. if ((szOutput[lstrlen(szOutput)-1] == '\n') &&
  151. (szOutput[lstrlen(szOutput)-2] != '\r'))
  152. {
  153. szOutput[lstrlen(szOutput)-1] = 0;
  154. StrCatBuff(szOutput, TEXT("\r\n"), ARRAYSIZE(szOutput));
  155. }
  156. if (lstrlen(szDebugOutputFile))
  157. {
  158. HANDLE hLogFile=NULL;
  159. DWORD dwBytesWritten=0;
  160. BOOL bSuccess=FALSE;
  161. // open a log file for appending. create if does not exist
  162. if ((hLogFile = CreateFile(szDebugOutputFile,
  163. GENERIC_WRITE,
  164. 0, // not FILE_SHARED_READ or WRITE
  165. NULL,
  166. OPEN_ALWAYS,
  167. FILE_ATTRIBUTE_NORMAL,
  168. (HANDLE)NULL)) != INVALID_HANDLE_VALUE)
  169. {
  170. // Write log string to file. Nothing I can do if this fails
  171. SetFilePointer(hLogFile, 0, NULL, FILE_END); // seek to end
  172. bSuccess = WriteFile(hLogFile,
  173. szOutput,
  174. lstrlen(szOutput),
  175. &dwBytesWritten,
  176. NULL);
  177. IF_WIN32(CloseHandle(hLogFile);) IF_WIN16(CloseFile(hLogFile);)
  178. }
  179. }
  180. else
  181. {
  182. OutputDebugString(szOutput);
  183. }
  184. out:
  185. return;
  186. }
  187. #ifdef OLD_STUFF
  188. /***************************************************************************
  189. Name : DebugTrap
  190. Purpose : depending on a registry setting, does the int3 equivalent
  191. Parameters: none
  192. Returns :
  193. Notes :
  194. ***************************************************************************/
  195. void DebugTrapFn(void)
  196. {
  197. if (fDebugTrap)
  198. DebugBreak();
  199. // _asm { int 3};
  200. }
  201. #endif
  202. #else // no DEBUG
  203. // need these to resolve def file exports
  204. void DebugPrintFileLine(
  205. DWORD dwParam, LPTSTR szPrefix,
  206. LPTSTR szFileName, DWORD nLineNumber)
  207. {}
  208. void __cdecl DebugPrintf(ULONG ulFlags, LPTSTR lpszFormat, ...)
  209. {}
  210. #ifdef OLD_STUFF
  211. void DebugTrapFn(void)
  212. {}
  213. #endif
  214. void __cdecl DebugPrintfError(LPTSTR lpszFormat, ...)
  215. {}
  216. void __cdecl DebugPrintfTrace(LPTSTR lpszFormat, ...)
  217. {}
  218. #endif // DEBUG