* Sync up to trunk head (r65394).
[reactos.git] / base / services / eventlog / eventsource.c
1 /*
2 * PROJECT: ReactOS kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/services/eventlog/eventsource.c
5 * PURPOSE: Event logging service
6 * COPYRIGHT: Copyright 2011 Eric Kohl
7 */
8
9 /* INCLUDES *****************************************************************/
10
11 #include "eventlog.h"
12
13 #define NDEBUG
14 #include <debug.h>
15
16 static LIST_ENTRY EventSourceListHead;
17 static CRITICAL_SECTION EventSourceListCs;
18
19 /* FUNCTIONS ****************************************************************/
20
21 VOID
22 InitEventSourceList(VOID)
23 {
24 InitializeCriticalSection(&EventSourceListCs);
25 InitializeListHead(&EventSourceListHead);
26 }
27
28
29 static VOID
30 DumpEventSourceList(VOID)
31 {
32 PLIST_ENTRY CurrentEntry;
33 PEVENTSOURCE EventSource;
34
35 DPRINT("DumpEventSourceList()\n");
36 EnterCriticalSection(&EventSourceListCs);
37
38 CurrentEntry = EventSourceListHead.Flink;
39 while (CurrentEntry != &EventSourceListHead)
40 {
41 EventSource = CONTAINING_RECORD(CurrentEntry,
42 EVENTSOURCE,
43 EventSourceListEntry);
44
45 DPRINT("EventSource->szName: %S\n", EventSource->szName);
46
47 CurrentEntry = CurrentEntry->Flink;
48 }
49
50 LeaveCriticalSection(&EventSourceListCs);
51
52 DPRINT("Done\n");
53 }
54
55
56 BOOL
57 LoadEventSources(HKEY hKey,
58 PLOGFILE pLogFile)
59 {
60 PEVENTSOURCE lpEventSource;
61 DWORD dwMaxSubKeyLength;
62 DWORD dwEventSourceNameLength;
63 DWORD dwIndex;
64 WCHAR *Buf = NULL;
65
66 DPRINT("LoadEventSources\n");
67
68 RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, &dwMaxSubKeyLength, NULL,
69 NULL, NULL, NULL, NULL, NULL);
70
71 DPRINT("dwMaxSubKeyLength: %lu\n", dwMaxSubKeyLength);
72
73 dwMaxSubKeyLength++;
74
75 Buf = HeapAlloc(MyHeap, 0, dwMaxSubKeyLength * sizeof(WCHAR));
76 if (!Buf)
77 {
78 DPRINT1("Error: can't allocate heap!\n");
79 return FALSE;
80 }
81
82 dwEventSourceNameLength = dwMaxSubKeyLength;
83
84 dwIndex = 0;
85 while (RegEnumKeyExW(hKey,
86 dwIndex,
87 Buf,
88 &dwEventSourceNameLength,
89 NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
90 {
91 DPRINT("Event Source: %S\n", Buf);
92
93 lpEventSource = HeapAlloc(MyHeap, 0, sizeof(EVENTSOURCE) + wcslen(Buf) * sizeof(WCHAR));
94 if (lpEventSource != NULL)
95 {
96 wcscpy(lpEventSource->szName, Buf);
97 lpEventSource->LogFile = pLogFile;
98
99 DPRINT("Insert event source: %S\n", lpEventSource->szName);
100
101
102 EnterCriticalSection(&EventSourceListCs);
103 InsertTailList(&EventSourceListHead,
104 &lpEventSource->EventSourceListEntry);
105 LeaveCriticalSection(&EventSourceListCs);
106 }
107
108 dwEventSourceNameLength = dwMaxSubKeyLength;
109 dwIndex++;
110 }
111
112 HeapFree(MyHeap, 0, Buf);
113
114 DumpEventSourceList();
115
116 return TRUE;
117 }
118
119
120 PEVENTSOURCE
121 GetEventSourceByName(LPCWSTR Name)
122 {
123 PLIST_ENTRY CurrentEntry;
124 PEVENTSOURCE Result = NULL;
125
126 DPRINT("GetEventSourceByName(%S)\n", Name);
127 EnterCriticalSection(&EventSourceListCs);
128
129 CurrentEntry = EventSourceListHead.Flink;
130 while (CurrentEntry != &EventSourceListHead)
131 {
132 PEVENTSOURCE Item = CONTAINING_RECORD(CurrentEntry,
133 EVENTSOURCE,
134 EventSourceListEntry);
135
136 DPRINT("Item->szName: %S\n", Item->szName);
137 // if ((*(Item->szName) != 0) && !_wcsicmp(Item->szName, Name))
138 if (_wcsicmp(Item->szName, Name) == 0)
139 {
140 DPRINT("Found it\n");
141 Result = Item;
142 break;
143 }
144
145 CurrentEntry = CurrentEntry->Flink;
146 }
147
148 LeaveCriticalSection(&EventSourceListCs);
149
150 DPRINT("Done (Result: %p)\n", Result);
151
152 return Result;
153 }