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.

194 lines
4.4 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1999
  5. //
  6. // File: elog.cpp
  7. //
  8. // Contents: Cert Server Core implementation
  9. //
  10. // History: 02-Jan-97 terences created
  11. //
  12. //---------------------------------------------------------------------------
  13. // TBD: add AddLoggingEvent, which will log to file instead of the event log
  14. // TBD: add audit events
  15. // TBD: add filtering so that criticality sorting of events can take place
  16. #include <pch.cpp>
  17. #pragma hdrstop
  18. #if DBG_CERTSRV
  19. WCHAR const *
  20. wszEventType(
  21. IN DWORD dwEventType)
  22. {
  23. WCHAR const *pwsz;
  24. switch (dwEventType)
  25. {
  26. case EVENTLOG_ERROR_TYPE: pwsz = L"Error"; break;
  27. case EVENTLOG_WARNING_TYPE: pwsz = L"Warning"; break;
  28. case EVENTLOG_INFORMATION_TYPE: pwsz = L"Information"; break;
  29. case EVENTLOG_AUDIT_SUCCESS: pwsz = L"AuditSuccess"; break;
  30. case EVENTLOG_AUDIT_FAILURE: pwsz = L"AuditFailiure"; break;
  31. default: pwsz = L"???"; break;
  32. }
  33. return(pwsz);
  34. }
  35. #endif // DBG_CERTSRV
  36. /*********************************************************************
  37. * FUNCTION: LogEvent( DWORD dwEventType, *
  38. * DWORD dwIdEvent, *
  39. * WORD cStrings, *
  40. * LPTSTR *apwszStrings); *
  41. * *
  42. * PURPOSE: add the event to the event log *
  43. * *
  44. * INPUT: the event ID to report in the log, the number of insert *
  45. * strings, and an array of null-terminated insert strings *
  46. * *
  47. * RETURNS: none *
  48. *********************************************************************/
  49. HRESULT
  50. LogEvent(
  51. DWORD dwEventType,
  52. DWORD dwIdEvent,
  53. WORD cStrings,
  54. WCHAR const **apwszStrings)
  55. {
  56. HRESULT hr;
  57. HANDLE hAppLog = NULL;
  58. WORD wElogType;
  59. #if DBG_CERTSRV
  60. CONSOLEPRINT3((
  61. DBG_SS_CERTSRV,
  62. "LogEvent(Type=%x(%ws), Id=%x)\n",
  63. dwEventType,
  64. wszEventType(dwEventType),
  65. dwIdEvent));
  66. for (DWORD i = 0; i < cStrings; i++)
  67. {
  68. CONSOLEPRINT2((
  69. DBG_SS_CERTSRV,
  70. "LogEvent[%u]: %ws\n",
  71. i,
  72. apwszStrings[i]));
  73. }
  74. #endif // DBG_CERTSRV
  75. wElogType = (WORD) dwEventType;
  76. hAppLog = RegisterEventSource(NULL, g_wszCertSrvServiceName);
  77. if (NULL == hAppLog)
  78. {
  79. hr = myHLastError();
  80. _JumpError(hr, error, "RegisterEventSource");
  81. }
  82. if (!ReportEvent(
  83. hAppLog,
  84. wElogType,
  85. 0,
  86. dwIdEvent,
  87. NULL,
  88. cStrings,
  89. 0,
  90. apwszStrings,
  91. NULL))
  92. {
  93. hr = myHLastError();
  94. _JumpError(hr, error, "ReportEvent");
  95. }
  96. hr = S_OK;
  97. error:
  98. if (NULL != hAppLog)
  99. {
  100. DeregisterEventSource(hAppLog);
  101. }
  102. return(hr);
  103. }
  104. HRESULT
  105. LogEventHResult(
  106. DWORD dwEventType,
  107. DWORD dwIdEvent,
  108. HRESULT hrEvent)
  109. {
  110. HRESULT hr;
  111. WCHAR const *apwsz[1];
  112. WORD cpwsz;
  113. WCHAR awchr[cwcHRESULTSTRING];
  114. apwsz[0] = myGetErrorMessageText(hrEvent, TRUE);
  115. cpwsz = ARRAYSIZE(apwsz);
  116. if (NULL == apwsz[0])
  117. {
  118. apwsz[0] = myHResultToString(awchr, hrEvent);
  119. }
  120. hr = LogEvent(dwEventType, dwIdEvent, cpwsz, apwsz);
  121. _JumpIfError(hr, error, "LogEvent");
  122. error:
  123. if (NULL != apwsz[0] && awchr != apwsz[0])
  124. {
  125. LocalFree(const_cast<WCHAR *>(apwsz[0]));
  126. }
  127. return(hr);
  128. }
  129. HRESULT
  130. LogEventString(
  131. DWORD dwEventType,
  132. DWORD dwIdEvent,
  133. OPTIONAL WCHAR const *pwszString)
  134. {
  135. return(LogEvent(
  136. dwEventType,
  137. dwIdEvent,
  138. NULL == pwszString? 0 : 1,
  139. NULL == pwszString? NULL : &pwszString));
  140. }
  141. HRESULT
  142. LogEventStringHResult(
  143. DWORD dwEventType,
  144. DWORD dwIdEvent,
  145. WCHAR const *pwszString,
  146. HRESULT hrEvent)
  147. {
  148. HRESULT hr;
  149. WCHAR const *apwsz[2];
  150. WORD cpwsz;
  151. WCHAR awchr[cwcHRESULTSTRING];
  152. apwsz[0] = pwszString;
  153. apwsz[1] = myGetErrorMessageText(hrEvent, TRUE);
  154. if (NULL == apwsz[1])
  155. {
  156. apwsz[1] = myHResultToString(awchr, hrEvent);
  157. }
  158. cpwsz = ARRAYSIZE(apwsz);
  159. hr = LogEvent(dwEventType, dwIdEvent, cpwsz, apwsz);
  160. _JumpIfError(hr, error, "LogEvent");
  161. error:
  162. if (NULL != apwsz[1] && awchr != apwsz[1])
  163. {
  164. LocalFree(const_cast<WCHAR *>(apwsz[1]));
  165. }
  166. return(hr);
  167. }