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.

229 lines
5.8 KiB

  1. #pragma once
  2. #include <stdio.h>
  3. #include <stdarg.h>
  4. #include <stdio.h>
  5. #include <Rtutils.h>
  6. #include <Winsock.h>
  7. extern DWORD g_dwTraceId;
  8. #if defined(DBG) || defined(_DEBUG)
  9. #define MYTRACE_ENABLE DWORD g_dwTraceId=0; int CMyDebugTrace::m_nIndent=-1;
  10. #define MYTRACE_START(UNIQUE_NAME) g_dwTraceId = TraceRegister(UNIQUE_NAME); // TRACE_USE_FILE
  11. #define MYTRACE_STOP TraceDeregister(g_dwTraceId);
  12. #define MYTRACE_ENTER(MSG) CMyDebugTrace _NewMyTrace(MSG, __FILE__, __LINE__, true);
  13. #define MYTRACE_ENTER_EXTRA_SPACE(MSG) CMyDebugTrace _NewMyTrace(MSG, __FILE__, __LINE__, true,1);
  14. #define MYTRACE_ENTER_NOSHOWEXIT(MSG) CMyDebugTrace _NewMyTrace(MSG, __FILE__, __LINE__, false);
  15. #define MYTRACE_ERROR(MSG,ERROR_NUMBER) _NewMyTrace.TraceError(MSG, ERROR_NUMBER, __LINE__);
  16. #define MYTRACE_IP(ULONG_ADDRESS) _NewMyTrace.IP2A(ULONG_ADDRESS)
  17. #define MYTRACE _NewMyTrace.MyTrace
  18. #define MYTRACE_BUFFER2STR(BUFF, NLEN) _NewMyTrace.Buffer2Str(BUFF, NLEN)
  19. #else
  20. #define MYTRACE_ENABLE
  21. #define MYTRACE_START(UNIQUE_NAME)
  22. #define MYTRACE_STOP
  23. #define MYTRACE_ENTER(MSG)
  24. #define MYTRACE_ENTER_EXTRA_SPACE(MSG)
  25. #define MYTRACE_ENTER_NOSHOWEXIT(MSG)
  26. #define MYTRACE_ERROR(MSG,ERROR_NUMBER)
  27. #define MYTRACE_IP(ULONG_ADDRESS) 0
  28. #define MYTRACE
  29. #define MYTRACE_BUFFER2STR(BUFF, NLEN) 0
  30. #endif
  31. //#define DOTRACE(ID, str)
  32. #define DOTRACE(ID, str) TracePrintf(g_dwTraceId, str);
  33. //#define DOTRACE(ID, str) OutputDebugString(str);OutputDebugString(L"\n");
  34. //#define DOTRACE(ID, str) MessageBox(NULL,str,m_szMsgEnter,MB_OK|MB_SERVICE_NOTIFICATION);
  35. class CMyDebugTrace
  36. {
  37. public:
  38. CMyDebugTrace(
  39. LPCSTR szMsgEnter,
  40. LPCSTR szFile,
  41. const int nLine,
  42. bool bShowExit,
  43. int nExtraWhiteSpace=0
  44. )
  45. {
  46. USES_CONVERSION;
  47. m_nExtraSpace = nExtraWhiteSpace;
  48. m_bShowExit = bShowExit;
  49. lstrcpy(m_szMsgEnter, A2T(szMsgEnter));
  50. // From the entry/creation text we extract the function name
  51. // example "CFoo::Format" we will have CFoo to prefix in front of all traces done after
  52. lstrcpy(m_szFunction, A2T(szMsgEnter));
  53. wchar_t* pEnd = wcschr(m_szFunction, L':');
  54. if ( pEnd )
  55. *pEnd = L'\0';
  56. else
  57. lstrcpy(m_szFunction, L"");
  58. lstrcpy(m_szFile, A2T(szFile));
  59. m_nIndent++;
  60. wsprintf(
  61. m_szTrace,
  62. L"%s%s %s",
  63. SzRepeat(m_nIndent),
  64. m_bShowExit ? L"++" : L"+-",
  65. m_szMsgEnter
  66. );
  67. int nLen = lstrlen(m_szTrace);
  68. wsprintf(
  69. m_szTrace,
  70. L"%s%s[L%d]%s",
  71. m_szTrace,
  72. SzRepeat(80-nLen),
  73. nLine,
  74. m_szFile
  75. );
  76. if ( m_nExtraSpace != 0 )
  77. DOTRACE(g_dwTraceId, TEXT(""));
  78. DOTRACE(g_dwTraceId, m_szTrace);
  79. }
  80. ~CMyDebugTrace()
  81. {
  82. if ( m_bShowExit )
  83. {
  84. wsprintf(m_szTrace, L"%s-- %s", SzRepeat(m_nIndent), m_szMsgEnter);
  85. DOTRACE(g_dwTraceId, m_szTrace);
  86. if ( m_nExtraSpace != 0 )
  87. DOTRACE(g_dwTraceId, TEXT(""));
  88. }
  89. m_nIndent--;
  90. }
  91. //
  92. // Output to the debug window a user trace
  93. //
  94. inline void _cdecl MyTrace(LPCSTR lpszFormat, ...)
  95. {
  96. #if defined(DBG) || defined(_DEBUG)
  97. USES_CONVERSION;
  98. va_list args;
  99. va_start(args, lpszFormat);
  100. char szBuffer[2048];
  101. _vsnprintf(szBuffer, sizeof(szBuffer), lpszFormat, args);
  102. TCHAR szLine[2048];
  103. wsprintf(szLine, L"%s%s %s", SzRepeat(CMyDebugTrace::m_nIndent+3), m_szFunction, A2T(szBuffer));
  104. DOTRACE(g_dwTraceId, szLine);
  105. va_end(args);
  106. #endif
  107. }
  108. //
  109. // Output a debug trace for a error including Line number and source file name
  110. //
  111. inline void
  112. TraceError(
  113. LPCSTR szMsg,
  114. int nError,
  115. const int nLine
  116. )
  117. {
  118. USES_CONVERSION;
  119. TCHAR szBuffer[512];
  120. //
  121. // No Error # was given so lets get the last one raised
  122. //
  123. if ( nError == 0 )
  124. nError = GetLastError();
  125. wsprintf(szBuffer, L"%s %s ERROR(0x%X):%s [L%d]%s", SzRepeat(CMyDebugTrace::m_nIndent), m_szMsgEnter, nError, A2T(szMsg), nLine, m_szFile);
  126. DOTRACE(g_dwTraceId, szBuffer);
  127. }
  128. //
  129. // Return the ascii equivalent of the IP example "192.168.0.1"
  130. //
  131. inline char*
  132. IP2A(ULONG ulAddress)
  133. {
  134. in_addr tmpAddr;
  135. tmpAddr.s_addr = ulAddress;
  136. return inet_ntoa(tmpAddr);
  137. }
  138. inline char*
  139. Buffer2Str(char* pBuff, int nLen)
  140. {
  141. static char szPrintableStr[1024];
  142. memcpy(szPrintableStr, pBuff, min(nLen,1022));
  143. szPrintableStr[min(nLen,1023)] = '\0';
  144. int nLenMin = strlen(szPrintableStr);
  145. for ( int nChar=0; nChar < nLenMin; nChar++ )
  146. {
  147. if ( isprint(szPrintableStr[nChar])==0 )
  148. szPrintableStr[nChar] = '';
  149. }
  150. return szPrintableStr;
  151. }
  152. //
  153. // Return a left paded space indentation
  154. //
  155. inline LPCTSTR
  156. SzRepeat(int nCount)
  157. {
  158. static TCHAR szPading[200];
  159. lstrcpy(szPading, L"");
  160. for ( int nI=0; nI < nCount; nI++ )
  161. lstrcat(szPading, L" ");
  162. return szPading;
  163. }
  164. int m_nExtraSpace;
  165. bool m_bShowExit;
  166. TCHAR m_szTrace[512];
  167. TCHAR m_szMsgEnter[1024];
  168. TCHAR m_szFunction[256];
  169. TCHAR m_szFile[256];
  170. static int m_nIndent;
  171. };