1 /*
2  * PROJECT:     ReactOS Automatic Testing Utility
3  * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE:     Functions for writing to the Event Log
5  * COPYRIGHT:   Copyright 2009 Dmitry Chapyshev (dmitry@reactos.org)
6  */
7 
8 #include "precomp.h"
9 HANDLE hLog = NULL;
10 
11 VOID
InitLogs(VOID)12 InitLogs(VOID)
13 {
14     WCHAR szBuf[MAX_PATH] = L"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\RosAutotest";
15     WCHAR szPath[MAX_PATH];
16     DWORD dwCategoryNum = 1;
17     DWORD dwDisp, dwData;
18     HKEY hKey;
19 
20     if (RegCreateKeyExW(HKEY_LOCAL_MACHINE,
21                         szBuf, 0, NULL,
22                         REG_OPTION_NON_VOLATILE,
23                         KEY_WRITE, NULL, &hKey, &dwDisp) != ERROR_SUCCESS)
24     {
25         return;
26     }
27 
28     if (!GetModuleFileName(NULL, szPath, sizeof(szPath) / sizeof(szPath[0])))
29         return;
30 
31     if (RegSetValueExW(hKey,
32                        L"EventMessageFile",
33                        0,
34                        REG_EXPAND_SZ,
35                        (LPBYTE)szPath,
36                        (DWORD)(wcslen(szPath) + 1) * sizeof(WCHAR)) != ERROR_SUCCESS)
37     {
38         RegCloseKey(hKey);
39         return;
40     }
41 
42     dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE |
43              EVENTLOG_INFORMATION_TYPE;
44 
45     if (RegSetValueExW(hKey,
46                        L"TypesSupported",
47                        0,
48                        REG_DWORD,
49                        (LPBYTE)&dwData,
50                        sizeof(DWORD)) != ERROR_SUCCESS)
51     {
52         RegCloseKey(hKey);
53         return;
54     }
55 
56     if (RegSetValueExW(hKey,
57                        L"CategoryMessageFile",
58                        0,
59                        REG_EXPAND_SZ,
60                        (LPBYTE)szPath,
61                        (DWORD)(wcslen(szPath) + 1) * sizeof(WCHAR)) != ERROR_SUCCESS)
62     {
63         RegCloseKey(hKey);
64         return;
65     }
66 
67     if (RegSetValueExW(hKey,
68                        L"CategoryCount",
69                        0,
70                        REG_DWORD,
71                        (LPBYTE)&dwCategoryNum,
72                        sizeof(DWORD)) != ERROR_SUCCESS)
73     {
74         RegCloseKey(hKey);
75         return;
76     }
77 
78     RegCloseKey(hKey);
79 
80     hLog = RegisterEventSourceW(NULL, L"RosAutotest");
81 }
82 
83 
84 VOID
FreeLogs(VOID)85 FreeLogs(VOID)
86 {
87     if (hLog) DeregisterEventSource(hLog);
88 }
89 
90 
91 BOOL
WriteLogMessage(WORD wType,DWORD dwEventID,LPWSTR lpMsg)92 WriteLogMessage(WORD wType, DWORD dwEventID, LPWSTR lpMsg)
93 {
94     if (!ReportEventW(hLog,
95                       wType,
96                       0,
97                       dwEventID,
98                       NULL,
99                       1,
100                       0,
101                       (LPCWSTR*)&lpMsg,
102                       NULL))
103     {
104         return FALSE;
105     }
106 
107     return TRUE;
108 }
109