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.

330 lines
6.4 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. All rights reserved.
  4. Module Name:
  5. perfutil.cxx
  6. Abstract:
  7. Utility routines copied from sockets\internet\svcs\lib\perfutil.c
  8. (which were in-turn copied from perfmon interface common code).
  9. Code reuse from copy & paste instead of fixing the interface.
  10. Author:
  11. Albert Ting (AlbertT) 17-Dec-1996
  12. Revision History:
  13. --*/
  14. #include "precomp.hxx"
  15. #pragma hdrstop
  16. #include "perf.hxx"
  17. #include "perfp.hxx"
  18. #include "messages.h"
  19. /********************************************************************
  20. Globals
  21. ********************************************************************/
  22. //
  23. // Translation table from kEventLogLevel to EVENTLOG_*_TYPE
  24. //
  25. UINT
  26. gauEventLogTable[] = {
  27. EVENTLOG_INFORMATION_TYPE, // kSuccess
  28. EVENTLOG_INFORMATION_TYPE, // kInformation,
  29. EVENTLOG_WARNING_TYPE, // kWarning,
  30. EVENTLOG_ERROR_TYPE // kError
  31. };
  32. //
  33. // Test for delimiter, end of line and non-digit characters
  34. // used by IsNumberInUnicodeList routine.
  35. //
  36. enum CHAR_TYPE {
  37. kDigit = 1,
  38. kDelimiter = 2,
  39. kInvalid = 3
  40. };
  41. inline
  42. CHAR_TYPE
  43. EvalThisChar(
  44. WCHAR c,
  45. WCHAR d
  46. )
  47. {
  48. if( c == d || c == 0 )
  49. {
  50. return kDelimiter;
  51. }
  52. if( c < L'0' || c > L'9' )
  53. {
  54. return kInvalid;
  55. }
  56. return kDigit;
  57. }
  58. BOOL
  59. Pfp_bNumberInUnicodeList (
  60. IN DWORD dwNumber,
  61. IN LPCWSTR lpwszUnicodeList
  62. )
  63. /*++
  64. Arguments:
  65. IN dwNumber
  66. DWORD number to find in list
  67. IN lpwszUnicodeList
  68. Null terminated, Space delimited list of decimal numbers
  69. Return Value:
  70. TRUE:
  71. dwNumber was found in the list of unicode number strings
  72. FALSE:
  73. dwNumber was not found in the list.
  74. --*/
  75. {
  76. DWORD dwThisNumber;
  77. LPCWSTR pwcThisChar;
  78. BOOL bValidNumber;
  79. BOOL bNewItem;
  80. WCHAR wckDelimiter; // could be an argument to be more flexible
  81. if( !lpwszUnicodeList )
  82. {
  83. //
  84. // NULL pointer, # not found.
  85. //
  86. return FALSE;
  87. }
  88. pwcThisChar = lpwszUnicodeList;
  89. dwThisNumber = 0;
  90. wckDelimiter = (WCHAR)' ';
  91. bValidNumber = FALSE;
  92. bNewItem = TRUE;
  93. while (TRUE) {
  94. switch (EvalThisChar (*pwcThisChar, wckDelimiter)) {
  95. case kDigit:
  96. // if this is the first kDigit after a kDelimiter, then
  97. // set flags to start computing the new number
  98. if (bNewItem) {
  99. bNewItem = FALSE;
  100. bValidNumber = TRUE;
  101. }
  102. if (bValidNumber) {
  103. dwThisNumber *= 10;
  104. dwThisNumber += (*pwcThisChar - L'0');
  105. }
  106. break;
  107. case kDelimiter:
  108. // a delimter is either the kDelimiter character or the
  109. // end of the string ('\0') if when the kDelimiter has been
  110. // reached a valid number was found, then compare it to the
  111. // number from the argument list. if this is the end of the
  112. // string and no match was found, then return.
  113. //
  114. if (bValidNumber) {
  115. if (dwThisNumber == dwNumber) return TRUE;
  116. bValidNumber = FALSE;
  117. }
  118. if (*pwcThisChar == 0) {
  119. return FALSE;
  120. } else {
  121. bNewItem = TRUE;
  122. dwThisNumber = 0;
  123. }
  124. break;
  125. case kInvalid:
  126. // if an kInvalid character was encountered, ignore all
  127. // characters up to the next kDelimiter and then start fresh.
  128. // the kInvalid number is not compared.
  129. bValidNumber = FALSE;
  130. break;
  131. default:
  132. break;
  133. }
  134. pwcThisChar++;
  135. }
  136. } // IsNumberInUnicodeList
  137. VOID
  138. Pfp_vOpenEventLog (
  139. VOID
  140. )
  141. /*++
  142. Routine Description:
  143. Reads the level of event logging from the registry and opens the channel
  144. to the event logger for subsequent event log entries.
  145. Arguments:
  146. Return Value:
  147. Revision History:
  148. --*/
  149. {
  150. if (gpd.hEventLog == NULL)
  151. {
  152. gpd.hEventLog = RegisterEventSource( NULL, gszAppName );
  153. if (gpd.hEventLog != NULL)
  154. {
  155. Pf_vReportEvent( kInformation | kDebug,
  156. MSG_PERF_LOG_OPEN,
  157. 0,
  158. NULL,
  159. NULL );
  160. }
  161. }
  162. }
  163. VOID
  164. Pfp_vCloseEventLog(
  165. VOID
  166. )
  167. /*++
  168. Routine Description:
  169. Closes the global event log connection.
  170. Arguments:
  171. Return Value:
  172. --*/
  173. {
  174. if( gpd.hEventLog )
  175. {
  176. Pf_vReportEvent( kInformation | kDebug,
  177. MSG_PERF_LOG_CLOSE,
  178. 0,
  179. NULL,
  180. NULL );
  181. DeregisterEventSource( gpd.hEventLog );
  182. gpd.hEventLog = NULL;
  183. }
  184. }
  185. VOID
  186. Pf_vReportEvent(
  187. IN UINT uLevel,
  188. IN DWORD dwMessage,
  189. IN DWORD cbData, OPTIONAL
  190. IN PVOID pvData, OPTIONAL
  191. IN LPCWSTR pszFirst, OPTIONAL
  192. ...
  193. )
  194. /*++
  195. Routine Description:
  196. Log an event.
  197. Arguments:
  198. uLevel - Combination of level (e.g., kSuccess, kError) and
  199. type (e.g., kUser, kVerbose).
  200. dwMessage - Message number.
  201. cbData - Size of optional data.
  202. pvData - Optional data.
  203. pszFirst - String inserts begin here, optional.
  204. Return Value:
  205. --*/
  206. {
  207. LPCWSTR apszMergeStrings[kMaxMergeStrings];
  208. UINT cMergeStrings = 0;
  209. va_list vargs;
  210. //
  211. // Skip the message if the log level is not high enough.
  212. //
  213. if(( uLevel >> kLevelShift ) > gpd.uLogLevel )
  214. {
  215. return;
  216. }
  217. if( !gpd.hEventLog )
  218. {
  219. Pfp_vOpenEventLog();
  220. }
  221. if( !gpd.hEventLog )
  222. {
  223. return;
  224. }
  225. if( pszFirst )
  226. {
  227. apszMergeStrings[cMergeStrings++] = pszFirst;
  228. va_start( vargs, pszFirst );
  229. while(( cMergeStrings < kMaxMergeStrings ) &&
  230. ( apszMergeStrings[cMergeStrings] = va_arg( vargs, LPCWSTR )))
  231. {
  232. cMergeStrings++;
  233. }
  234. va_end( vargs );
  235. }
  236. ReportEvent( gpd.hEventLog,
  237. (WORD)gauEventLogTable[uLevel & kTypeMask],
  238. 0,
  239. dwMessage,
  240. NULL,
  241. (WORD)cMergeStrings,
  242. cbData,
  243. apszMergeStrings,
  244. pvData );
  245. }