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.

311 lines
5.7 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. eventlog.c
  5. Abstract:
  6. This file contains all functions that access the application event log.
  7. Author:
  8. Wesley Witt (wesw) 19-Mar-1996
  9. Environment:
  10. User Mode
  11. --*/
  12. #include <windows.h>
  13. #include <tapi.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <tchar.h>
  17. #include "winfax.h"
  18. #include "faxutil.h"
  19. #include "faxreg.h"
  20. #include "faxsvcrg.h"
  21. #include "faxdev.h"
  22. #include "faxevent.h"
  23. #include "messages.h"
  24. #define MAX_STRINGS 64
  25. #define FAX_SVC_EVENT TEXT("Fax Service")
  26. HINSTANCE MyhInstance;
  27. HANDLE hEventSrc;
  28. DWORD FaxCategoryCount;
  29. CRITICAL_SECTION CsEvent;
  30. #ifdef OLD_WAY
  31. FAX_LOG_CATEGORY FaxCategory[16];
  32. #else // OLD_WAY
  33. PFAX_LOG_CATEGORY FaxCategory;
  34. #endif // OLD_WAY
  35. DWORD
  36. FaxEventDllInit(
  37. HINSTANCE hInstance,
  38. DWORD Reason,
  39. LPVOID Context
  40. )
  41. {
  42. if (Reason == DLL_PROCESS_ATTACH) {
  43. MyhInstance = hInstance;
  44. DisableThreadLibraryCalls( hInstance );
  45. }
  46. return TRUE;
  47. }
  48. BOOL
  49. InitializeEventLog(
  50. IN HANDLE HeapHandle,
  51. IN PREG_FAX_SERVICE FaxReg,
  52. PFAX_LOG_CATEGORY DefaultCategories,
  53. int DefaultCategoryCount
  54. )
  55. /*++
  56. Routine Description:
  57. Initializes the event log for the FAX service to
  58. record event entries.
  59. Arguments:
  60. HeapHandle -
  61. FaxReg -
  62. DefaultCategories - points to the array of FAX_LOG_CATEGORY structures
  63. DefaultCategoryCount - the number of elements in DefaultCategories
  64. Return Value:
  65. TRUE for success, FALSE for failure
  66. --*/
  67. {
  68. DWORD i;
  69. HeapInitialize(HeapHandle,NULL,NULL,0);
  70. InitializeCriticalSection( &CsEvent );
  71. //
  72. // create the event source, if it does not already exist
  73. //
  74. if ( CreateFaxEventSource( FaxReg,
  75. DefaultCategories,
  76. DefaultCategoryCount ) == (BOOL) FALSE )
  77. {
  78. return FALSE;
  79. }
  80. Assert( FaxReg->Logging );
  81. //
  82. // allocate memory for the logging category info
  83. //
  84. EnterCriticalSection( &CsEvent );
  85. FaxCategory = (PFAX_LOG_CATEGORY) MemAlloc( sizeof(FAX_LOG_CATEGORY) * FaxReg->LoggingCount );
  86. if (!FaxCategory) {
  87. LeaveCriticalSection( &CsEvent );
  88. return FALSE;
  89. }
  90. //
  91. // capture the event categories from the registry
  92. //
  93. for (i=0; i<FaxReg->LoggingCount; i++) {
  94. FaxCategory[i].Name = StringDup( FaxReg->Logging[i].CategoryName );
  95. FaxCategory[i].Category = FaxReg->Logging[i].Number;
  96. FaxCategory[i].Level = FaxReg->Logging[i].Level;
  97. }
  98. FaxCategoryCount = FaxReg->LoggingCount;
  99. LeaveCriticalSection( &CsEvent );
  100. //
  101. // get a handle to the event log
  102. //
  103. hEventSrc = RegisterEventSource(
  104. NULL,
  105. FAX_SVC_EVENT
  106. );
  107. if (!hEventSrc) {
  108. return FALSE;
  109. }
  110. return TRUE;
  111. }
  112. VOID
  113. RefreshEventLog(
  114. PREG_FAX_LOGGING FaxReg
  115. )
  116. /*++
  117. Routine Description:
  118. Refreshes the event log for the FAX service to
  119. record event entries.
  120. Arguments:
  121. None.
  122. Return Value:
  123. None.
  124. --*/
  125. {
  126. DWORD i;
  127. EnterCriticalSection( &CsEvent );
  128. //
  129. // capture the event categories from the registry
  130. //
  131. for (i=0; i<FaxReg->LoggingCount; i++) {
  132. if (FaxCategory[i].Name) {
  133. MemFree( (LPVOID)FaxCategory[i].Name );
  134. }
  135. FaxCategory[i].Name = StringDup( FaxReg->Logging[i].CategoryName );
  136. FaxCategory[i].Category = FaxReg->Logging[i].Number;
  137. FaxCategory[i].Level = FaxReg->Logging[i].Level;
  138. }
  139. FaxCategoryCount = FaxReg->LoggingCount;
  140. LeaveCriticalSection( &CsEvent );
  141. }
  142. BOOL
  143. FaxLog(
  144. DWORD Category,
  145. DWORD Level,
  146. DWORD StringCount,
  147. DWORD FormatId,
  148. ...
  149. )
  150. /*++
  151. Routine Description:
  152. Writes a log file entry to the event log.
  153. Arguments:
  154. Level - Severity of the log record
  155. StringCount - Number of strings included in the varargs
  156. FormatId - Message file id
  157. Return Value:
  158. TRUE for success, FALSE for failure
  159. --*/
  160. {
  161. LPTSTR Strings[MAX_STRINGS];
  162. DWORD i;
  163. va_list args;
  164. WORD Type;
  165. //
  166. // look for the category
  167. //
  168. EnterCriticalSection( &CsEvent );
  169. for (i=0; i<FaxCategoryCount; i++) {
  170. if (FaxCategory[i].Category == Category) {
  171. if (Level > FaxCategory[i].Level) {
  172. LeaveCriticalSection( &CsEvent );
  173. return FALSE;
  174. }
  175. }
  176. }
  177. LeaveCriticalSection( &CsEvent );
  178. va_start( args, FormatId );
  179. //
  180. // capture the strings
  181. //
  182. for (i=0; i<StringCount; i++) {
  183. Strings[i] = va_arg( args, LPTSTR );
  184. if(Strings[i] == NULL) {
  185. Strings[i] = TEXT("");
  186. }
  187. }
  188. switch (FormatId >> 30) {
  189. case STATUS_SEVERITY_WARNING:
  190. Type = EVENTLOG_WARNING_TYPE;
  191. break;
  192. case STATUS_SEVERITY_ERROR:
  193. Type = EVENTLOG_ERROR_TYPE;
  194. break;
  195. case STATUS_SEVERITY_INFORMATIONAL:
  196. case STATUS_SEVERITY_SUCCESS:
  197. Type = EVENTLOG_INFORMATION_TYPE;
  198. }
  199. //
  200. // record the event
  201. //
  202. ReportEvent(
  203. hEventSrc, // event log handle
  204. Type, // type
  205. (WORD) Category, // category
  206. FormatId, // event id
  207. NULL, // security id
  208. (WORD) StringCount, // string count
  209. 0, // data buffer size
  210. Strings, // strings
  211. NULL // data buffer
  212. );
  213. return TRUE;
  214. }