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.

328 lines
8.4 KiB

  1. /*++
  2. Module Name
  3. bgdebug.cpp
  4. Description
  5. Implements functions used for debugging
  6. Note
  7. Revised based on msplog.cpp which is not available to bridge test app
  8. --*/
  9. #include "stdafx.h"
  10. #include <stdio.h>
  11. #define MAXDEBUGSTRINGLENGTH 512
  12. static DWORD sg_dwTraceID = INVALID_TRACEID;
  13. static char sg_szTraceName[100]; // saves name of dll
  14. static DWORD sg_dwTracingToDebugger = 0;
  15. static DWORD sg_dwDebuggerMask = 0;
  16. BOOL BGLogRegister(LPCTSTR szName)
  17. {
  18. HKEY hTracingKey;
  19. char szTracingKey[100];
  20. const char szTracingEnableValue[] = "EnableDebuggerTracing";
  21. const char szTracingMaskValue[] = "ConsoleTracingMask";
  22. sg_dwTracingToDebugger = 0;
  23. #ifdef UNICODE
  24. wsprintfA(szTracingKey, "Software\\Microsoft\\Tracing\\%ls", szName);
  25. #else
  26. wsprintfA(szTracingKey, "Software\\Microsoft\\Tracing\\%s", szName);
  27. #endif
  28. if ( ERROR_SUCCESS == RegOpenKeyExA(HKEY_LOCAL_MACHINE,
  29. szTracingKey,
  30. 0,
  31. KEY_READ,
  32. &hTracingKey) )
  33. {
  34. DWORD dwDataSize = sizeof (DWORD);
  35. DWORD dwDataType;
  36. RegQueryValueExA(hTracingKey,
  37. szTracingEnableValue,
  38. 0,
  39. &dwDataType,
  40. (LPBYTE) &sg_dwTracingToDebugger,
  41. &dwDataSize);
  42. RegQueryValueExA(hTracingKey,
  43. szTracingMaskValue,
  44. 0,
  45. &dwDataType,
  46. (LPBYTE) &sg_dwDebuggerMask,
  47. &dwDataSize);
  48. RegCloseKey (hTracingKey);
  49. }
  50. #ifdef UNICODE
  51. wsprintfA(sg_szTraceName, "%ls", szName);
  52. #else
  53. wsprintfA(sg_szTraceName, "%s", szName);
  54. #endif
  55. sg_dwTraceID = TraceRegister(szName);
  56. return (sg_dwTraceID != INVALID_TRACEID);
  57. }
  58. void BGLogDeRegister()
  59. {
  60. sg_dwTracingToDebugger = 0;
  61. if (sg_dwTraceID != INVALID_TRACEID)
  62. {
  63. TraceDeregister(sg_dwTraceID);
  64. sg_dwTraceID = INVALID_TRACEID;
  65. }
  66. }
  67. void BGLogPrint(DWORD dwDbgLevel, LPCSTR lpszFormat, IN ...)
  68. /*++
  69. Routine Description:
  70. Formats the incoming debug message & calls TraceVprintfEx to print it.
  71. Arguments:
  72. dwDbgLevel - The type of the message.
  73. lpszFormat - printf-style format string, followed by appropriate
  74. list of arguments
  75. Return Value:
  76. --*/
  77. {
  78. static char * message[24] =
  79. {
  80. "ERROR",
  81. "WARNING",
  82. "INFO",
  83. "TRACE",
  84. "EVENT",
  85. "INVALID TRACE LEVEL"
  86. };
  87. char szTraceBuf[MAXDEBUGSTRINGLENGTH + 1];
  88. DWORD dwIndex;
  89. if ( ( sg_dwTracingToDebugger > 0 ) &&
  90. ( 0 != ( dwDbgLevel & sg_dwDebuggerMask ) ) )
  91. {
  92. switch(dwDbgLevel)
  93. {
  94. case BG_ERROR: dwIndex = 0; break;
  95. case BG_WARN: dwIndex = 1; break;
  96. case BG_INFO: dwIndex = 2; break;
  97. case BG_TRACE: dwIndex = 3; break;
  98. case BG_EVENT: dwIndex = 4; break;
  99. default: dwIndex = 5; break;
  100. }
  101. // retrieve local time
  102. SYSTEMTIME SystemTime;
  103. GetLocalTime(&SystemTime);
  104. wsprintfA(szTraceBuf,
  105. "%s:[%02u:%02u:%02u.%03u,tid=%x:]%s: ",
  106. sg_szTraceName,
  107. SystemTime.wHour,
  108. SystemTime.wMinute,
  109. SystemTime.wSecond,
  110. SystemTime.wMilliseconds,
  111. GetCurrentThreadId(),
  112. message[dwIndex]);
  113. va_list ap;
  114. va_start(ap, lpszFormat);
  115. _vsnprintf(&szTraceBuf[lstrlenA(szTraceBuf)],
  116. MAXDEBUGSTRINGLENGTH - lstrlenA(szTraceBuf),
  117. lpszFormat,
  118. ap
  119. );
  120. lstrcatA (szTraceBuf, "\n");
  121. OutputDebugStringA (szTraceBuf);
  122. va_end(ap);
  123. }
  124. if (sg_dwTraceID != INVALID_TRACEID)
  125. {
  126. switch(dwDbgLevel)
  127. {
  128. case BG_ERROR: dwIndex = 0; break;
  129. case BG_WARN: dwIndex = 1; break;
  130. case BG_INFO: dwIndex = 2; break;
  131. case BG_TRACE: dwIndex = 3; break;
  132. case BG_EVENT: dwIndex = 4; break;
  133. default: dwIndex = 5; break;
  134. }
  135. wsprintfA(szTraceBuf, "[%s] %s", message[dwIndex], lpszFormat);
  136. va_list arglist;
  137. va_start(arglist, lpszFormat);
  138. TraceVprintfExA(sg_dwTraceID, dwDbgLevel, szTraceBuf, arglist);
  139. va_end(arglist);
  140. }
  141. }
  142. /*//////////////////////////////////////////////////////////////////////////////
  143. don't assume enum value doesn't change
  144. ////*/
  145. // the order should be the same as event type enum
  146. char* gsaType[] =
  147. {
  148. "Tapi Event",
  149. "Call State",
  150. "Call Media",
  151. "Participant Event"
  152. };
  153. typedef struct
  154. {
  155. int id;
  156. char *str;
  157. }EVENT_ITEM;
  158. //========================================
  159. EVENT_ITEM gaTE[]=
  160. {
  161. TE_TAPIOBJECT, "Tapi Object",
  162. TE_ADDRESS, "Address",
  163. TE_CALLNOTIFICATION, "Call Notification",
  164. TE_CALLSTATE, "Call State",
  165. TE_CALLMEDIA, "Call Media",
  166. TE_CALLHUB, "Call Hub",
  167. TE_CALLINFOCHANGE, "Call Info Change",
  168. TE_PRIVATE, "Private",
  169. TE_REQUEST, "Request",
  170. TE_AGENT, "Agent",
  171. TE_AGENTSESSION, "Agent Session",
  172. TE_QOSEVENT, "QoS Event",
  173. TE_AGENTHANDLER, "Agent Handler",
  174. TE_ACDGROUP, "ACD Group",
  175. TE_QUEUE, "Queue",
  176. TE_DIGITEVENT, "Digit Event",
  177. TE_GENERATEEVENT, "Generate Event"
  178. };
  179. int giaTENum = sizeof (gaTE) / sizeof (EVENT_ITEM);
  180. //========================================
  181. EVENT_ITEM gaCS[] =
  182. {
  183. CS_IDLE, "IDLE",
  184. CS_INPROGRESS, "In Progress",
  185. CS_CONNECTED, "Connected",
  186. CS_DISCONNECTED, "Disconnected",
  187. CS_OFFERING, "Offering",
  188. CS_HOLD, "Hold",
  189. CS_QUEUED, "Queued"
  190. };
  191. int giaCSNum = sizeof (gaCS) / sizeof (EVENT_ITEM);
  192. //========================================
  193. EVENT_ITEM gaCME[] =
  194. {
  195. CME_NEW_STREAM, "New Stream",
  196. CME_STREAM_FAIL, "Stream Fail",
  197. CME_TERMINAL_FAIL, "Terminal Fail",
  198. CME_STREAM_NOT_USED, "Stream Not Used",
  199. CME_STREAM_ACTIVE, "Stream Active",
  200. CME_STREAM_INACTIVE, "Stream Inactive"
  201. };
  202. int giaCMENum = sizeof (gaCME) / sizeof (EVENT_ITEM);
  203. //========================================
  204. EVENT_ITEM gaPE[] =
  205. {
  206. PE_NEW_PARTICIPANT, "New Participant",
  207. PE_INFO_CHANGE, "Info Change",
  208. PE_PARTICIPANT_LEAVE, "Participant Leave",
  209. PE_NEW_SUBSTREAM, "New Substream",
  210. PE_SUBSTREAM_REMOVED, "Substream Removed",
  211. PE_SUBSTREAM_MAPPED, "Substream Mapped",
  212. PE_SUBSTREAM_UNMAPPED, "SubStream Unmapped",
  213. PE_PARTICIPANT_TIMEOUT, "Participant Timeout",
  214. PE_PARTICIPANT_RECOVERED, "Participant Recovered",
  215. PE_PARTICIPANT_ACTIVE, "Participant Active",
  216. PE_PARTICIPANT_INACTIVE, "Participant Inactive",
  217. PE_LOCAL_TALKING, "Local Talking",
  218. PE_LOCAL_SILENT, "Local Silent"
  219. };
  220. int giaPENum = sizeof (gaPE) / sizeof (EVENT_ITEM);
  221. /*//////////////////////////////////////////////////////////////////////////////
  222. ////*/
  223. void BGLogEvent (EVENT_TYPE EventType, int event)
  224. {
  225. EVENT_ITEM *aEvent;
  226. int i, num;
  227. switch (EventType)
  228. {
  229. case BG_TE:
  230. aEvent = gaTE;
  231. num = giaTENum;
  232. break;
  233. case BG_CS:
  234. aEvent = gaCS;
  235. num = giaCSNum;
  236. break;
  237. case BG_CME:
  238. aEvent = gaCME;
  239. num = giaCMENum;
  240. break;
  241. case BG_PE:
  242. aEvent = gaPE;
  243. num = giaPENum;
  244. break;
  245. default:
  246. LOG ((BG_ERROR, "Unsupported event type (%d, %d)", EventType, event));
  247. return;
  248. break;
  249. }
  250. // search the event index string
  251. for (i=0; i<num; i++)
  252. {
  253. if (event != aEvent[i].id)
  254. continue;
  255. // match
  256. LOG ((BG_EVENT, "%s: %s (%d, %d)",
  257. gsaType[EventType], aEvent[i].str, EventType, event));
  258. return;
  259. }
  260. // not found
  261. LOG ((BG_ERROR, "Event string not found (%d, %d)", EventType, event));
  262. }