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.

254 lines
6.7 KiB

  1. /****************************************************************************/
  2. // errorlog.c
  3. //
  4. // Copyright (C) 1997-1999 Microsoft Corp.
  5. /****************************************************************************/
  6. #include "precomp.h"
  7. #include "errorlog.h"
  8. #include <wlnotify.h>
  9. static WCHAR gs_LogErrorTitle[MAX_PATH+1] = L"";
  10. static HANDLE gs_LogHandle = NULL;
  11. extern HINSTANCE g_hInstance;
  12. void
  13. TsInitLogging()
  14. /*++
  15. Routine Description:
  16. Initialize the global variables that we use for Logging events.
  17. Arguments:
  18. Return Value:
  19. --*/
  20. {
  21. //
  22. // Register ourselves as an event source
  23. //
  24. gs_LogHandle = RegisterEventSourceW(
  25. NULL,
  26. L"TermServDevices"
  27. );
  28. if (gs_LogHandle == NULL) {
  29. KdPrint(("UMRDPDR: Failed to open log file\n"));
  30. }
  31. }
  32. void TsStopLogging()
  33. {
  34. /*++
  35. Routine Description:
  36. Free up resources that we created in TsInitLogging
  37. Arguments:
  38. Return Value:
  39. --*/
  40. //
  41. // Unregister Event Source
  42. //
  43. if (gs_LogHandle) {
  44. if (!DeregisterEventSource(gs_LogHandle)) {
  45. KdPrint(("UMRDPDR:Failed to Deregister Event Source.\n"));
  46. }
  47. gs_LogHandle = NULL;
  48. }
  49. }
  50. void
  51. TsLogError(
  52. IN DWORD dwEventID,
  53. IN DWORD dwErrorType,
  54. IN int nStringsCount,
  55. IN WCHAR * pStrings[],
  56. IN DWORD LineNumber
  57. )
  58. {
  59. /*++
  60. Routine Description:
  61. Log the Error message specified by dwEventID.
  62. Along with the message, the error value returned by GetLastError is logged.
  63. The caller should make sure that the Last Error is appropriately set.
  64. Arguments:
  65. dwEventID - ID of the message (as specified in the .mc file).
  66. nStringsCount - Number of Insert Strings for this event message.
  67. pStrings - An array of Insert Strings.
  68. LineNumber - The caller will pass __LINE__ for this.
  69. Return Value:
  70. --*/
  71. if (gs_LogHandle) {
  72. DWORD RawData[2];
  73. //
  74. // The raw data we will be writing comprises of two DWORDS.
  75. // The first DWORD is the GetLastError value
  76. // The second DWORD is the LineNumber in which the error occurred.
  77. //
  78. RawData[0] = GetLastError();
  79. RawData[1] = LineNumber;
  80. if (!ReportEventW(gs_LogHandle, // LogHandle
  81. (WORD)dwErrorType, // EventType
  82. 0, // EventCategory
  83. dwEventID, // EventID
  84. NULL, // UserSID
  85. (WORD)nStringsCount, // NumStrings
  86. sizeof(RawData), // DataSize
  87. pStrings, // Strings
  88. (LPVOID)RawData)) { // Raw Data
  89. KdPrint(("UMRDPDR: ReportEvent Failed. Error code: %ld\n", GetLastError()));
  90. }
  91. //
  92. // ReportEvent modifies the Last Error value.
  93. // So, we will set it back to the original error value.
  94. //
  95. SetLastError(RawData[0]);
  96. }
  97. }
  98. void TsPopupError(
  99. IN DWORD dwEventID,
  100. IN WCHAR * pStrings[]
  101. )
  102. {
  103. /*++
  104. Routine Description:
  105. Popup an Error message specified by dwEventID.
  106. Along with the message, the error value returned by GetLastError is displayed.
  107. The caller should make sure that the Last Error is appropriately set.
  108. The routine displays the error message in the format "%s \n %s" where the first
  109. insert string is the specific error message as specified by dwEventID, and the
  110. second insert string is the formatted error message for the error value in
  111. GetLastError.
  112. Arguments:
  113. dwEventID - ID of the message (as specified in the .mc file).
  114. pStrings - An array of Insert Strings.
  115. Return Value:
  116. --*/
  117. WCHAR * formattedMsg = NULL;
  118. WCHAR * formattedLastError = NULL;
  119. WCHAR * finalformattedMsg = NULL;
  120. DWORD dwLastError = GetLastError();
  121. //
  122. // Load the error dialog string.
  123. //
  124. if (!wcslen(gs_LogErrorTitle)) {
  125. if (!LoadString(
  126. g_hInstance,
  127. IDS_TSERRORDIALOG_STRING,
  128. gs_LogErrorTitle,
  129. sizeof(gs_LogErrorTitle) / sizeof(gs_LogErrorTitle[0])
  130. )) {
  131. KdPrint(("UMRDPPRN:LoadString %ld failed with Error: %ld.\n",
  132. IDS_TSERRORDIALOG_STRING, GetLastError()));
  133. wcscpy(gs_LogErrorTitle, L"Terminal Server Notify Error");
  134. ASSERT(FALSE);
  135. }
  136. }
  137. //
  138. // Format the message
  139. //
  140. if (!FormatMessageW(
  141. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  142. FORMAT_MESSAGE_FROM_HMODULE |
  143. FORMAT_MESSAGE_ARGUMENT_ARRAY,
  144. (LPCVOID) g_hInstance,
  145. dwEventID,
  146. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  147. (LPWSTR) &formattedMsg,
  148. 0,
  149. (va_list*)pStrings)) {
  150. KdPrint(("UMRDPDR: FormatMessage failed. Error code: %ld.\n", GetLastError()));
  151. goto Cleanup;
  152. }
  153. //
  154. // Format the GetLastError message
  155. //
  156. if (dwLastError != ERROR_SUCCESS) {
  157. if (FormatMessageW(
  158. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  159. FORMAT_MESSAGE_FROM_SYSTEM |
  160. FORMAT_MESSAGE_IGNORE_INSERTS,
  161. NULL,
  162. dwLastError,
  163. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  164. (LPWSTR)&formattedLastError,
  165. 0,
  166. NULL
  167. )) {
  168. //
  169. // Allocate enough memory for constructing the final formattedmessage.
  170. //
  171. finalformattedMsg = (WCHAR *) LocalAlloc (LMEM_FIXED,
  172. LocalSize(formattedLastError) +
  173. LocalSize(formattedMsg) +
  174. 3*sizeof(WCHAR)); // For constructing a string in the format "%s\n%s"
  175. }
  176. else {
  177. KdPrint(("UMRDPDR: FormatMessage failed. Error code: %ld.\n", GetLastError()));
  178. }
  179. }
  180. if (finalformattedMsg) {
  181. swprintf(finalformattedMsg, L"%ws\n%ws", formattedMsg, formattedLastError);
  182. MessageBoxW(NULL, finalformattedMsg, gs_LogErrorTitle, MB_ICONERROR);
  183. }
  184. else {
  185. MessageBoxW(NULL, formattedMsg, gs_LogErrorTitle, MB_ICONERROR);
  186. }
  187. Cleanup:
  188. if (formattedLastError != NULL) {
  189. LocalFree(formattedLastError);
  190. }
  191. if (formattedMsg != NULL) {
  192. LocalFree(formattedMsg);
  193. }
  194. if (finalformattedMsg != NULL) {
  195. LocalFree(finalformattedMsg);
  196. }
  197. SetLastError(dwLastError);
  198. }