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.

306 lines
7.3 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. mylog.c
  5. Abstract:
  6. This module contains the debugging support.
  7. Author:
  8. Mu Han (muhan) 26-March-1997
  9. --*/
  10. #ifdef PHONESPLOG
  11. #include <windows.h>
  12. #include <stdio.h>
  13. #include "mylog.h"
  14. #define MAXDEBUGSTRINGLENGTH 512
  15. static DWORD sg_dwTraceID = INVALID_TRACEID;
  16. static char sg_szTraceName[100]; // saves name of dll
  17. static DWORD sg_dwTracingToDebugger = 0;
  18. static DWORD sg_dwTracingToConsole = 0;
  19. static DWORD sg_dwTracingToFile = 0;
  20. static DWORD sg_dwDebuggerMask = 0;
  21. /*++
  22. Routine Description:
  23. Registers for tracing on a debugger if it is enabled in the registry.
  24. This may be called from DllMain().
  25. Arguments:
  26. szName - Component name for use in the tracing
  27. Return Value:
  28. BOOL
  29. --*/
  30. BOOL NTAPI LogRegisterDebugger(LPCTSTR szName)
  31. {
  32. HKEY hTracingKey;
  33. char szTracingKey[100];
  34. const char szDebuggerTracingEnableValue[] = "EnableDebuggerTracing";
  35. const char szTracingMaskValue[] = "ConsoleTracingMask";
  36. sg_dwTracingToDebugger = 0;
  37. #ifdef UNICODE
  38. wsprintfA(szTracingKey, "Software\\Microsoft\\Tracing\\%ls", szName);
  39. #else
  40. wsprintfA(szTracingKey, "Software\\Microsoft\\Tracing\\%s", szName);
  41. #endif
  42. if ( ERROR_SUCCESS == RegOpenKeyExA(HKEY_LOCAL_MACHINE,
  43. szTracingKey,
  44. 0,
  45. KEY_READ,
  46. &hTracingKey) )
  47. {
  48. DWORD dwDataSize = sizeof (DWORD);
  49. DWORD dwDataType;
  50. RegQueryValueExA(hTracingKey,
  51. szDebuggerTracingEnableValue,
  52. 0,
  53. &dwDataType,
  54. (LPBYTE) &sg_dwTracingToDebugger,
  55. &dwDataSize);
  56. RegQueryValueExA(hTracingKey,
  57. szTracingMaskValue,
  58. 0,
  59. &dwDataType,
  60. (LPBYTE) &sg_dwDebuggerMask,
  61. &dwDataSize);
  62. RegCloseKey (hTracingKey);
  63. }
  64. #ifdef UNICODE
  65. wsprintfA(sg_szTraceName, "%ls", szName);
  66. #else
  67. wsprintfA(sg_szTraceName, "%s", szName);
  68. #endif
  69. return TRUE;
  70. }
  71. /*++
  72. Routine Description:
  73. Registers for tracing using the Tracing API.
  74. This is NOT safe to be called from DllMain().
  75. Arguments:
  76. szName - Component name for use in the tracing
  77. Return Value:
  78. BOOL
  79. --*/
  80. BOOL NTAPI LogRegisterTracing(LPCTSTR szName)
  81. {
  82. HKEY hTracingKey;
  83. char szTracingKey[100];
  84. const char szConsoleTracingEnableValue[] = "EnableConsoleTracing";
  85. const char szFileTracingEnableValue[] = "EnableFileTracing";
  86. sg_dwTracingToConsole = 0;
  87. sg_dwTracingToFile = 0;
  88. #ifdef UNICODE
  89. wsprintfA(szTracingKey, "Software\\Microsoft\\Tracing\\%ls", szName);
  90. #else
  91. wsprintfA(szTracingKey, "Software\\Microsoft\\Tracing\\%s", szName);
  92. #endif
  93. if ( ERROR_SUCCESS == RegOpenKeyExA(HKEY_LOCAL_MACHINE,
  94. szTracingKey,
  95. 0,
  96. KEY_READ,
  97. &hTracingKey) )
  98. {
  99. DWORD dwDataSize = sizeof (DWORD);
  100. DWORD dwDataType;
  101. RegQueryValueExA(hTracingKey,
  102. szConsoleTracingEnableValue,
  103. 0,
  104. &dwDataType,
  105. (LPBYTE) &sg_dwTracingToConsole,
  106. &dwDataSize);
  107. RegQueryValueExA(hTracingKey,
  108. szFileTracingEnableValue,
  109. 0,
  110. &dwDataType,
  111. (LPBYTE) &sg_dwTracingToFile,
  112. &dwDataSize);
  113. RegCloseKey (hTracingKey);
  114. }
  115. sg_dwTraceID = TraceRegister(szName);
  116. return (sg_dwTraceID != INVALID_TRACEID);
  117. }
  118. /*++
  119. Routine Description:
  120. DeRegisters for tracing on a debugger.
  121. This may be called from DllMain().
  122. --*/
  123. void NTAPI LogDeRegisterDebugger()
  124. {
  125. sg_dwTracingToDebugger = 0;
  126. }
  127. /*++
  128. Routine Description:
  129. DeRegisters for tracing using the Tracing API.
  130. This is NOT safe to be called from DllMain().
  131. --*/
  132. void NTAPI LogDeRegisterTracing()
  133. {
  134. sg_dwTracingToConsole = 0;
  135. sg_dwTracingToFile = 0;
  136. if (sg_dwTraceID != INVALID_TRACEID)
  137. {
  138. TraceDeregister(sg_dwTraceID);
  139. sg_dwTraceID = INVALID_TRACEID;
  140. }
  141. }
  142. /*++
  143. Routine Description:
  144. Formats the incoming debug message & calls TraceVprintfEx to print it.
  145. Arguments:
  146. dwDbgLevel - The type of the message.
  147. lpszFormat - printf-style format string, followed by appropriate
  148. list of arguments
  149. Return Value:
  150. --*/
  151. void NTAPI LogPrint(IN DWORD dwDbgLevel, IN LPCSTR lpszFormat, IN ...)
  152. {
  153. static char * message[24] =
  154. {
  155. "ERROR",
  156. "WARNING",
  157. "INFO",
  158. "TRACE",
  159. "EVENT",
  160. "INVALID TRACE LEVEL"
  161. };
  162. char szTraceBuf[MAXDEBUGSTRINGLENGTH + 1];
  163. DWORD dwIndex;
  164. SYSTEMTIME SystemTime;
  165. va_list ap;
  166. va_list arglist;
  167. if ( ( sg_dwTracingToDebugger > 0 ) &&
  168. ( 0 != ( dwDbgLevel & sg_dwDebuggerMask ) ) )
  169. {
  170. switch(dwDbgLevel)
  171. {
  172. case PHONESP_ERROR: dwIndex = 0; break;
  173. case PHONESP_WARN: dwIndex = 1; break;
  174. case PHONESP_INFO: dwIndex = 2; break;
  175. case PHONESP_TRACE: dwIndex = 3; break;
  176. case PHONESP_EVENT: dwIndex = 4; break;
  177. default: dwIndex = 5; break;
  178. }
  179. // retrieve local time
  180. GetLocalTime(&SystemTime);
  181. wsprintfA(szTraceBuf,
  182. "%s:[%02u:%02u:%02u.%03u,tid=%x:]%s: ",
  183. sg_szTraceName,
  184. SystemTime.wHour,
  185. SystemTime.wMinute,
  186. SystemTime.wSecond,
  187. SystemTime.wMilliseconds,
  188. GetCurrentThreadId(),
  189. message[dwIndex]);
  190. va_start(ap, lpszFormat);
  191. _vsnprintf(&szTraceBuf[lstrlenA(szTraceBuf)],
  192. MAXDEBUGSTRINGLENGTH - lstrlenA(szTraceBuf),
  193. lpszFormat,
  194. ap
  195. );
  196. lstrcatA (szTraceBuf, "\n");
  197. OutputDebugStringA (szTraceBuf);
  198. va_end(ap);
  199. }
  200. if (sg_dwTraceID != INVALID_TRACEID)
  201. {
  202. if ( ( sg_dwTracingToConsole > 0 ) || ( sg_dwTracingToFile > 0 ) )
  203. {
  204. switch(dwDbgLevel)
  205. {
  206. case PHONESP_ERROR: dwIndex = 0; break;
  207. case PHONESP_WARN: dwIndex = 1; break;
  208. case PHONESP_INFO: dwIndex = 2; break;
  209. case PHONESP_TRACE: dwIndex = 3; break;
  210. case PHONESP_EVENT: dwIndex = 4; break;
  211. default: dwIndex = 5; break;
  212. }
  213. wsprintfA(szTraceBuf, "[%s] %s", message[dwIndex], lpszFormat);
  214. va_start(arglist, lpszFormat);
  215. TraceVprintfExA(sg_dwTraceID, dwDbgLevel, szTraceBuf, arglist);
  216. va_end(arglist);
  217. }
  218. }
  219. }
  220. #endif // PHONESPLOG
  221. // eof