2 * PROJECT: ReactOS EventLog Service
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/services/eventlog/eventsource.c
5 * PURPOSE: Event log sources support
6 * COPYRIGHT: Copyright 2011 Eric Kohl
9 /* INCLUDES *****************************************************************/
16 static LIST_ENTRY EventSourceListHead
;
17 static CRITICAL_SECTION EventSourceListCs
;
19 /* FUNCTIONS ****************************************************************/
22 InitEventSourceList(VOID
)
24 InitializeCriticalSection(&EventSourceListCs
);
25 InitializeListHead(&EventSourceListHead
);
30 DumpEventSourceList(VOID
)
32 PLIST_ENTRY CurrentEntry
;
33 PEVENTSOURCE EventSource
;
35 DPRINT("DumpEventSourceList()\n");
36 EnterCriticalSection(&EventSourceListCs
);
38 CurrentEntry
= EventSourceListHead
.Flink
;
39 while (CurrentEntry
!= &EventSourceListHead
)
41 EventSource
= CONTAINING_RECORD(CurrentEntry
,
43 EventSourceListEntry
);
45 DPRINT("EventSource->szName: %S\n", EventSource
->szName
);
47 CurrentEntry
= CurrentEntry
->Flink
;
50 LeaveCriticalSection(&EventSourceListCs
);
57 AddNewEventSource(PLOGFILE pLogFile
,
60 PEVENTSOURCE lpEventSource
;
62 lpEventSource
= HeapAlloc(GetProcessHeap(), 0,
63 FIELD_OFFSET(EVENTSOURCE
, szName
[wcslen(lpSourceName
) + 1]));
64 if (lpEventSource
!= NULL
)
66 wcscpy(lpEventSource
->szName
, lpSourceName
);
67 lpEventSource
->LogFile
= pLogFile
;
69 DPRINT("Insert event source: %S\n", lpEventSource
->szName
);
71 EnterCriticalSection(&EventSourceListCs
);
72 InsertTailList(&EventSourceListHead
,
73 &lpEventSource
->EventSourceListEntry
);
74 LeaveCriticalSection(&EventSourceListCs
);
77 return (lpEventSource
!= NULL
);
82 LoadEventSources(HKEY hKey
,
86 DWORD dwNumSubKeys
, dwMaxSubKeyLength
;
87 DWORD dwEventSourceNameLength
, MaxValueLen
;
89 PWSTR Buf
= NULL
, SourceList
= NULL
, Source
= NULL
;
90 size_t cchRemaining
= 0;
93 DPRINT("LoadEventSources\n");
95 Result
= RegQueryInfoKeyW(hKey
, NULL
, NULL
, NULL
, &dwNumSubKeys
, &dwMaxSubKeyLength
,
96 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
97 if (Result
!= ERROR_SUCCESS
)
99 DPRINT1("RegQueryInfoKeyW failed: %lu\n", Result
);
105 Buf
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLength
* sizeof(WCHAR
));
108 DPRINT1("Error: cannot allocate heap!\n");
113 * Allocate a buffer for storing the names of the sources as a REG_MULTI_SZ
114 * in the registry. Also add the event log as its own source.
115 * Add a final NULL-terminator.
117 MaxValueLen
= dwNumSubKeys
* dwMaxSubKeyLength
+ wcslen(pLogFile
->LogName
) + 2;
118 SourceList
= HeapAlloc(GetProcessHeap(), 0, MaxValueLen
* sizeof(WCHAR
));
121 DPRINT1("Error: cannot allocate heap!\n");
122 /* It is not dramatic if we cannot create it */
126 cchRemaining
= MaxValueLen
;
131 * Enumerate all the subkeys of the event log key, that constitute
132 * all the possible event sources for this event log. At this point,
133 * skip the possible existing source having the same name as the
134 * event log, it will be added later on.
136 dwEventSourceNameLength
= dwMaxSubKeyLength
;
138 while (RegEnumKeyExW(hKey
,
141 &dwEventSourceNameLength
,
142 NULL
, NULL
, NULL
, NULL
) == ERROR_SUCCESS
)
144 if (_wcsicmp(pLogFile
->LogName
, Buf
) != 0)
146 DPRINT("Event Source: %S\n", Buf
);
147 Success
= AddNewEventSource(pLogFile
, Buf
);
148 if (Success
&& (Source
!= NULL
))
150 /* Append the event source name and an extra NULL-terminator */
151 StringCchCopyExW(Source
, cchRemaining
, Buf
, &Source
, &cchRemaining
, 0);
152 if (cchRemaining
> 0)
160 dwEventSourceNameLength
= dwMaxSubKeyLength
;
164 /* Finally, allow the event log itself to be its own source */
165 DPRINT("Event Source: %S\n", pLogFile
->LogName
);
166 Success
= AddNewEventSource(pLogFile
, pLogFile
->LogName
);
167 if (Success
&& (Source
!= NULL
))
169 /* Append the event source name and an extra NULL-terminator */
170 StringCchCopyExW(Source
, cchRemaining
, pLogFile
->LogName
, &Source
, &cchRemaining
, 0);
171 if (cchRemaining
> 0)
178 /* Save the list of sources in the registry */
179 Result
= RegSetValueExW(hKey
,
184 (MaxValueLen
- cchRemaining
+ 1) * sizeof(WCHAR
));
185 if (Result
!= ERROR_SUCCESS
)
187 DPRINT1("RegSetValueExW failed: %lu\n", Result
);
191 HeapFree(GetProcessHeap(), 0, SourceList
);
193 HeapFree(GetProcessHeap(), 0, Buf
);
195 DumpEventSourceList();
202 GetEventSourceByName(LPCWSTR Name
)
204 PLIST_ENTRY CurrentEntry
;
205 PEVENTSOURCE Item
, Result
= NULL
;
207 DPRINT("GetEventSourceByName(%S)\n", Name
);
208 EnterCriticalSection(&EventSourceListCs
);
210 CurrentEntry
= EventSourceListHead
.Flink
;
211 while (CurrentEntry
!= &EventSourceListHead
)
213 Item
= CONTAINING_RECORD(CurrentEntry
,
215 EventSourceListEntry
);
217 DPRINT("Item->szName: %S\n", Item
->szName
);
218 // if ((*(Item->szName) != 0) && !_wcsicmp(Item->szName, Name))
219 if (_wcsicmp(Item
->szName
, Name
) == 0)
221 DPRINT("Found it\n");
226 CurrentEntry
= CurrentEntry
->Flink
;
229 LeaveCriticalSection(&EventSourceListCs
);
231 DPRINT("Done (Result: %p)\n", Result
);