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.

278 lines
6.5 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. Dulog.cpp
  5. Abstract:
  6. Implements the event logging functions.
  7. Note that the C++ class provides the
  8. functionality for event logging.
  9. Notes:
  10. Unicode only.
  11. History:
  12. 03/02/2001 rparsons Created
  13. --*/
  14. #include "precomp.h"
  15. extern SETUP_INFO g_si;
  16. /*++
  17. Routine Description:
  18. Logs an event to the event log and optionally displays the message.
  19. Note that we use this function when writing to the event log
  20. for non-error related stuff
  21. Arguments:
  22. wType - The type of message we're logging
  23. dwEventID - An event ID for our message
  24. fDisplayErr - A flag to indicate if we
  25. should display an error
  26. fCritical - Indicates if we should display a message
  27. from the string table
  28. Return Value:
  29. TRUE on success, FALSE otherwise
  30. --*/
  31. BOOL
  32. LogEventDisplayError(
  33. IN WORD wType,
  34. IN DWORD dwEventID,
  35. IN BOOL fDisplayErr,
  36. IN BOOL fCritical
  37. )
  38. {
  39. WORD wNumStrings = 0;
  40. LPWSTR lpwMessageArray[2];
  41. LPWSTR lpwSourceFile = NULL;
  42. int nLen = 0;
  43. CEventLog cel;
  44. //
  45. // If the Critical flag is set, an error occured while
  46. // trying to get strings from somewhere. Report the event
  47. // without registering a source
  48. //
  49. if (fCritical) {
  50. WCHAR wszMessageBoxTitle[MAX_PATH] = L"";
  51. WCHAR wszPrettyAppName[MAX_PATH] = L"";
  52. WCHAR wszEventLogSourceName[MAX_PATH] = L"";
  53. WCHAR wszTemp[MAX_PATH] = L"";
  54. WCHAR wszCriticalError[MAX_PATH] = L"";
  55. HANDLE hEventLog;
  56. const WCHAR *pwMessage[1];
  57. LoadString(g_si.hInstance,
  58. IDS_MB_TITLE,
  59. wszMessageBoxTitle,
  60. MAX_PATH);
  61. LoadString(g_si.hInstance,
  62. g_si.fOnWin2K ? IDS_APP_NAME_WIN2K :
  63. IDS_APP_NAME_XP,
  64. wszPrettyAppName,
  65. MAX_PATH);
  66. LoadString(g_si.hInstance,
  67. IDS_EL_SOURCE_NAME,
  68. wszEventLogSourceName,
  69. MAX_PATH);
  70. LoadString(g_si.hInstance,
  71. IDS_CRITICAL_ERROR,
  72. wszTemp,
  73. MAX_PATH);
  74. wsprintf(wszCriticalError, wszTemp, wszPrettyAppName);
  75. pwMessage[0] = wszCriticalError;
  76. hEventLog = RegisterEventSource(NULL, wszEventLogSourceName);
  77. if (hEventLog) {
  78. ReportEvent(hEventLog,
  79. EVENTLOG_ERROR_TYPE,
  80. 0,
  81. 1001,
  82. NULL,
  83. 1,
  84. 0,
  85. pwMessage,
  86. NULL);
  87. }
  88. if (!g_si.fQuiet) {
  89. MessageBox(GetDesktopWindow(),
  90. wszCriticalError,
  91. wszMessageBoxTitle,
  92. MB_ICONERROR | MB_OK);
  93. }
  94. DeregisterEventSource(hEventLog);
  95. return TRUE;
  96. }
  97. //
  98. // Determine if we've already created our event source
  99. //
  100. if (!g_si.fEventSourceCreated) {
  101. //
  102. // Build a path to our source file and register
  103. // the event source
  104. //
  105. nLen += wcslen(g_si.lpwEventLogSourceName);
  106. nLen += wcslen(g_si.lpwInstallDirectory);
  107. lpwSourceFile = (LPWSTR) MALLOC((nLen*sizeof(WCHAR))*2);
  108. if (NULL == lpwSourceFile) {
  109. return FALSE;
  110. }
  111. wcscpy(lpwSourceFile, g_si.lpwInstallDirectory);
  112. wcscat(lpwSourceFile, L"\\");
  113. wcscat(lpwSourceFile, g_si.lpwEventLogSourceName);
  114. wcscat(lpwSourceFile, L".exe");
  115. cel.CreateEventSource(lpwSourceFile,
  116. g_si.lpwEventLogSourceName,
  117. dwApplication);
  118. g_si.fEventSourceCreated = TRUE;
  119. FREE(lpwSourceFile);
  120. }
  121. lpwMessageArray[wNumStrings++] = (LPWSTR) g_si.lpwPrettyAppName;
  122. //
  123. // Place the event in the event log
  124. //
  125. cel.LogEvent(g_si.lpwEventLogSourceName,
  126. NULL,
  127. wType,
  128. dwEventID,
  129. 1,
  130. (LPCWSTR*) lpwMessageArray);
  131. if (fDisplayErr) {
  132. if (!g_si.fQuiet) {
  133. DisplayErrMsg(NULL, dwEventID, (LPWSTR) lpwMessageArray);
  134. }
  135. }
  136. return TRUE;
  137. }
  138. /*++
  139. Routine Description:
  140. Logs an event to the event log
  141. Arguments:
  142. wType - Type of message
  143. dwEventID - Event ID
  144. wNumStrings - Number of insertion strings
  145. lpwStrings - Array of strings
  146. Return Value:
  147. None
  148. --*/
  149. void
  150. LogWUEvent(
  151. IN WORD wType,
  152. IN DWORD dwEventID,
  153. IN WORD wNumStrings,
  154. IN LPCWSTR *lpwStrings
  155. )
  156. {
  157. HANDLE hES = NULL;
  158. LPVOID lpMsgBuf = NULL;
  159. LPWSTR lpwSourceFile = NULL;
  160. int nLen = 0;
  161. CEventLog cel;
  162. //
  163. // Determine if we've already created our event source
  164. // in the registry
  165. //
  166. if (!g_si.fEventSourceCreated) {
  167. //
  168. // Build a path to our source file and register
  169. // the event source
  170. //
  171. nLen += wcslen(g_si.lpwEventLogSourceName);
  172. nLen += wcslen(g_si.lpwInstallDirectory);
  173. lpwSourceFile = (LPWSTR) MALLOC((nLen*sizeof(WCHAR))*2);
  174. if (NULL == lpwSourceFile) {
  175. return;
  176. }
  177. wcscpy(lpwSourceFile, g_si.lpwInstallDirectory);
  178. wcscat(lpwSourceFile, L"\\");
  179. wcscat(lpwSourceFile, g_si.lpwEventLogSourceName);
  180. wcscat(lpwSourceFile, L".exe");
  181. cel.CreateEventSource(lpwSourceFile,
  182. g_si.lpwEventLogSourceName,
  183. dwApplication);
  184. g_si.fEventSourceCreated = TRUE;
  185. FREE(lpwSourceFile);
  186. }
  187. if (wNumStrings) {
  188. //
  189. // Report the event with insertion strings
  190. //
  191. cel.LogEvent(g_si.lpwEventLogSourceName,
  192. NULL,
  193. wType,
  194. dwEventID,
  195. 0,
  196. NULL);
  197. } else {
  198. //
  199. // Report the event with no strings
  200. //
  201. cel.LogEvent(g_si.lpwEventLogSourceName,
  202. NULL,
  203. wType,
  204. dwEventID,
  205. wNumStrings,
  206. (LPCWSTR*) lpwStrings);
  207. }
  208. return;
  209. }