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.

285 lines
5.6 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. All rights reserved
  4. Module Name:
  5. output.cxx
  6. Abstract:
  7. output
  8. Author:
  9. Larry Zhu (LZhu) December 1, 2001 Created
  10. Environment:
  11. User Mode -Win32
  12. Revision History:
  13. --*/
  14. #include "precomp.hxx"
  15. #pragma hdrstop
  16. #ifdef UNICODE
  17. #undef UNICODE
  18. #endif
  19. #ifdef _UNICODE
  20. #undef _UNICODE
  21. #endif
  22. #include "output.hxx"
  23. typedef struct _TLibarayGlobals
  24. {
  25. ULONG uMajorVersion;
  26. ULONG uMinorVersion;
  27. ULONG uDebugMask;
  28. PCSTR pszDbgPrompt;
  29. PCRITICAL_SECTION pCriticalSection;
  30. } TLibarayGlobals;
  31. TLibarayGlobals g_AnsiOutputGlobals = {
  32. 1, // major version
  33. 2, // minor version
  34. 0xF, // debug mask
  35. "SSPI_TEST", // debug prompt
  36. NULL, // no serialization
  37. };
  38. CHAR ToChar(IN CHAR c)
  39. {
  40. if (isprint(c))
  41. {
  42. return c;
  43. }
  44. return '.';
  45. }
  46. VOID SpaceIt(IN ULONG len, IN CHAR* buf)
  47. {
  48. memset(buf, ' ', len);
  49. }
  50. CHAR ToHex(IN ULONG c)
  51. {
  52. static PCSTR pszDigits = "0123456789abcdef";
  53. static ULONG len = strlen(pszDigits);
  54. if (c <= len)
  55. { // c >= 0
  56. return pszDigits[c];
  57. }
  58. return '*';
  59. }
  60. VOID
  61. DebugPrintHex(
  62. IN ULONG ulLevel,
  63. IN OPTIONAL PCSTR pszBanner,
  64. IN ULONG cbBuffer,
  65. IN const VOID* pvbuffer
  66. )
  67. {
  68. if (g_AnsiOutputGlobals.uDebugMask & ulLevel)
  69. {
  70. PCRITICAL_SECTION pCriticalSection = g_AnsiOutputGlobals.pCriticalSection;
  71. const UCHAR* pBuffer = reinterpret_cast<const UCHAR*>(pvbuffer);
  72. ULONG high = 0;
  73. ULONG low = 0;
  74. CHAR szLine[256] = {0};
  75. ULONG i = 0;
  76. CHAR szBanner[MAX_PATH * 4] = {0};
  77. DWORD dwPid = GetCurrentProcessId();
  78. DWORD dwTid = GetCurrentThreadId();
  79. _snprintf(szBanner, sizeof(szBanner) - 1,
  80. g_AnsiOutputGlobals.pszDbgPrompt ? "%#x.%#x %s> %s %s" : "%#x.%#x%s> %s %s",
  81. dwPid,
  82. dwTid,
  83. g_AnsiOutputGlobals.pszDbgPrompt ? g_AnsiOutputGlobals.pszDbgPrompt : "",
  84. DebugLevel2Str(ulLevel),
  85. pszBanner ? pszBanner : "");
  86. if (pCriticalSection)
  87. {
  88. EnterCriticalSection(pCriticalSection);
  89. }
  90. OutputDebugStringPrintf(szBanner, "\n");
  91. SpaceIt(72, szLine);
  92. for (i = 0; i < cbBuffer; i++)
  93. {
  94. high = pBuffer[i] / 16;
  95. low = pBuffer[i] % 16;
  96. szLine[3 * (i % 16)] = ToHex(high);
  97. szLine[3 * (i % 16) + 1] = ToHex(low);
  98. szLine [52 + (i % 16)] = ToChar(pBuffer[i]);
  99. if (i % 16 == 7 && i != (cbBuffer - 1))
  100. {
  101. szLine[3 * (i % 16) + 2] = '-';
  102. }
  103. if (i % 16 == 15)
  104. {
  105. OutputDebugStringPrintf(NULL, " %s\n", szLine);
  106. SpaceIt(72, szLine);
  107. }
  108. }
  109. OutputDebugStringPrintf(NULL, " %s\n", szLine);
  110. if (pCriticalSection)
  111. {
  112. LeaveCriticalSection(pCriticalSection);
  113. }
  114. }
  115. }
  116. PCSTR
  117. DebugLevel2Str(
  118. IN ULONG ulLevel
  119. )
  120. {
  121. PCSTR pszText = NULL;
  122. switch (ulLevel)
  123. {
  124. case SSPI_WARN:
  125. pszText = "[warn]";
  126. break;
  127. case SSPI_ERROR:
  128. pszText = "[error]";
  129. break;
  130. case SSPI_LOG:
  131. pszText = "[log]";
  132. break;
  133. case SSPI_LOG_MORE:
  134. pszText = "[more]";
  135. break;
  136. case SSPI_MSG:
  137. pszText = "[msg]";
  138. break;
  139. default:
  140. pszText = "[invalid]";
  141. break;
  142. }
  143. return pszText;
  144. }
  145. VOID
  146. VOutputDebugStringPrintf(
  147. IN OPTIONAL PCSTR pszBanner,
  148. IN PCSTR pszFmt,
  149. IN va_list pArgs
  150. )
  151. {
  152. CHAR szBuffer[4096] = {0};
  153. INT cbUsed = 0;
  154. cbUsed = _snprintf(szBuffer, sizeof(szBuffer) - 1, "%s", pszBanner ? pszBanner : "");
  155. if (cbUsed >= 0)
  156. {
  157. _vsnprintf(szBuffer + cbUsed, sizeof(szBuffer) - cbUsed, pszFmt, pArgs);
  158. }
  159. OutputDebugStringA(szBuffer);
  160. }
  161. VOID
  162. OutputDebugStringPrintf(
  163. IN OPTIONAL PCSTR pszBanner,
  164. IN PCSTR pszFmt,
  165. IN ...
  166. )
  167. {
  168. va_list marker;
  169. va_start(marker, pszFmt);
  170. VOutputDebugStringPrintf(pszBanner ? pszBanner : "", pszFmt, marker);
  171. va_end(marker);
  172. }
  173. VOID DebugPrintf(
  174. IN ULONG ulLevel,
  175. IN PCSTR pszFmt,
  176. IN ...
  177. )
  178. {
  179. if (g_AnsiOutputGlobals.uDebugMask & ulLevel)
  180. {
  181. CHAR szBanner[MAX_PATH] = {0};
  182. DWORD dwPid = GetCurrentProcessId();
  183. DWORD dwTid = GetCurrentThreadId();
  184. _snprintf(szBanner, sizeof(szBanner) - 1,
  185. g_AnsiOutputGlobals.pszDbgPrompt ? "%#x.%#x %s> %s " : "%#x.%#x%s> %s ",
  186. dwPid,
  187. dwTid,
  188. g_AnsiOutputGlobals.pszDbgPrompt ? g_AnsiOutputGlobals.pszDbgPrompt : "", DebugLevel2Str(ulLevel));
  189. va_list marker;
  190. va_start(marker, pszFmt);
  191. VOutputDebugStringPrintf(szBanner, pszFmt, marker);
  192. va_end(marker);
  193. }
  194. }
  195. VOID
  196. DebugLogOpen(
  197. IN PCSTR pszPrompt,
  198. IN ULONG ulMask
  199. )
  200. {
  201. g_AnsiOutputGlobals.uDebugMask = ulMask;
  202. g_AnsiOutputGlobals.pszDbgPrompt = pszPrompt;
  203. }
  204. VOID
  205. DebugLogOpenSerialized(
  206. IN PCSTR pszPrompt,
  207. IN ULONG ulMask,
  208. IN PCRITICAL_SECTION pCriticalSection
  209. )
  210. {
  211. g_AnsiOutputGlobals.uDebugMask = ulMask;
  212. g_AnsiOutputGlobals.pszDbgPrompt = pszPrompt;
  213. g_AnsiOutputGlobals.pCriticalSection = pCriticalSection;
  214. }
  215. VOID
  216. DebugLogClose(
  217. VOID
  218. )
  219. {
  220. g_AnsiOutputGlobals.uDebugMask = 0;
  221. g_AnsiOutputGlobals.pszDbgPrompt = NULL;
  222. }