Synchronize with trunk.
[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 static LIST_ENTRY EventSourceListHead;
14 static CRITICAL_SECTION EventSourceListCs;
15
16 /* FUNCTIONS ****************************************************************/
17
18 VOID
19 InitEventSourceList(VOID)
20 {
21 InitializeCriticalSection(&EventSourceListCs);
22 InitializeListHead(&EventSourceListHead);
23 }
24
25
26 static VOID
27 DumpEventSourceList(VOID)
28 {
29 PLIST_ENTRY CurrentEntry;
30 PEVENTSOURCE EventSource;
31
32 DPRINT("DumpEventSourceList()\n");
33 EnterCriticalSection(&EventSourceListCs);
34
35 CurrentEntry = EventSourceListHead.Flink;
36 while (CurrentEntry != &EventSourceListHead)
37 {
38 EventSource = CONTAINING_RECORD(CurrentEntry,
39 EVENTSOURCE,
40 EventSourceListEntry);
41
42 DPRINT("EventSource->szName: %S\n", EventSource->szName);
43
44 CurrentEntry = CurrentEntry->Flink;
45 }
46
47 LeaveCriticalSection(&EventSourceListCs);
48
49 DPRINT("Done\n");
50 }
51
52
53 BOOL
54 LoadEventSources(HKEY hKey,
55 PLOGFILE pLogFile)
56 {
57 PEVENTSOURCE lpEventSource;
58 DWORD dwMaxSubKeyLength;
59 DWORD dwEventSourceNameLength;
60 DWORD dwIndex;
61 WCHAR *Buf = NULL;
62
63 DPRINT("LoadEventSources\n");
64
65 RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, &dwMaxSubKeyLength, NULL,
66 NULL, NULL, NULL, NULL, NULL);
67
68 DPRINT("dwMaxSubKeyLength: %lu\n", dwMaxSubKeyLength);
69
70 dwMaxSubKeyLength++;
71
72 Buf = HeapAlloc(MyHeap, 0, dwMaxSubKeyLength * sizeof(WCHAR));
73 if (!Buf)
74 {
75 DPRINT1("Error: can't allocate heap!\n");
76 return FALSE;
77 }
78
79 dwEventSourceNameLength = dwMaxSubKeyLength;
80
81 dwIndex = 0;
82 while (RegEnumKeyExW(hKey,
83 dwIndex,
84 Buf,
85 &dwEventSourceNameLength,
86 NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
87 {
88 DPRINT("Event Source: %S\n", Buf);
89
90 lpEventSource = HeapAlloc(MyHeap, 0, sizeof(EVENTSOURCE) + wcslen(Buf) * sizeof(WCHAR));
91 if (lpEventSource != NULL)
92 {
93 wcscpy(lpEventSource->szName, Buf);
94 lpEventSource->LogFile = pLogFile;
95
96 DPRINT("Insert event source: %S\n", lpEventSource->szName);
97
98
99 EnterCriticalSection(&EventSourceListCs);
100 InsertTailList(&EventSourceListHead,
101 &lpEventSource->EventSourceListEntry);
102 LeaveCriticalSection(&EventSourceListCs);
103 }
104
105 dwEventSourceNameLength = dwMaxSubKeyLength;
106 dwIndex++;
107 }
108
109 HeapFree(MyHeap, 0, Buf);
110
111 DumpEventSourceList();
112
113 return TRUE;
114 }
115
116
117 PEVENTSOURCE
118 GetEventSourceByName(LPCWSTR Name)
119 {
120 PLIST_ENTRY CurrentEntry;
121 PEVENTSOURCE Result = NULL;
122
123 DPRINT("GetEventSourceByName(%S)\n", Name);
124 EnterCriticalSection(&EventSourceListCs);
125
126 CurrentEntry = EventSourceListHead.Flink;
127 while (CurrentEntry != &EventSourceListHead)
128 {
129 PEVENTSOURCE Item = CONTAINING_RECORD(CurrentEntry,
130 EVENTSOURCE,
131 EventSourceListEntry);
132
133 DPRINT("Item->szName: %S\n", Item->szName);
134 // if ((*(Item->szName) != 0) && !_wcsicmp(Item->szName, Name))
135 if (_wcsicmp(Item->szName, Name) == 0)
136 {
137 DPRINT("Found it\n");
138 Result = Item;
139 break;
140 }
141
142 CurrentEntry = CurrentEntry->Flink;
143 }
144
145 LeaveCriticalSection(&EventSourceListCs);
146
147 DPRINT("Done (Result: %p)\n", Result);
148
149 return Result;
150 }