wrong character in the GSM 03.38 table (ç for Ç)
[gammu.git] / smsd / log-event.c
blob319728fb17d7c242eb0d490577122d122f14dc23
1 /**
2 * Windows event log logging backend.
3 */
5 #include <winsock2.h>
6 #include <windows.h>
7 #include <winreg.h>
8 #include <stdio.h>
9 #include "smsd-event.h"
10 #include "core.h"
12 gboolean eventlog_deregister(void)
14 LONG ret;
16 ret = RegDeleteKey(
17 HKEY_LOCAL_MACHINE,
18 "System\\CurrentControlSet\\Services\\EventLog\\Application\\GammuSMSD"
21 return (ret == ERROR_SUCCESS);
24 gboolean eventlog_register(void)
26 LONG ret;
27 HKEY hKey;
28 DWORD data;
30 SECURITY_DESCRIPTOR SD;
31 SECURITY_ATTRIBUTES SA;
33 char program_name[MAX_PATH];
35 if (GetModuleFileName(NULL, program_name, sizeof(program_name)) == 0)
36 return FALSE;
38 if (!InitializeSecurityDescriptor(&SD, SECURITY_DESCRIPTOR_REVISION)) {
39 return FALSE;
42 if(!SetSecurityDescriptorDacl(&SD, TRUE, 0, FALSE)) {
43 return FALSE;
46 SA.nLength = sizeof(SA);
47 SA.lpSecurityDescriptor = &SD;
48 SA.bInheritHandle = FALSE;
50 ret = RegCreateKeyEx(
51 HKEY_LOCAL_MACHINE,
52 "System\\CurrentControlSet\\Services\\EventLog\\Application\\GammuSMSD",
54 NULL,
55 REG_OPTION_NON_VOLATILE,
56 KEY_WRITE,
57 &SA,
58 &hKey,
59 NULL);
61 if (ret != ERROR_SUCCESS) {
62 fprintf(stderr, "Failed to create registry key!\n");
63 return FALSE;
66 data = 3;
67 ret = RegSetValueEx(
68 hKey,
69 "CategoryCount",
71 REG_DWORD,
72 (BYTE *)&data,
73 sizeof(DWORD));
75 if (ret != ERROR_SUCCESS) {
76 fprintf(stderr, "Failed to write CategoryCount to registry!\n");
77 return FALSE;
80 ret = RegSetValueEx(
81 hKey,
82 "CategoryMessageFile",
84 REG_SZ,
85 (BYTE *)program_name,
86 strlen(program_name) + 1);
88 if (ret != ERROR_SUCCESS) {
89 fprintf(stderr, "Failed to write CategoryMessageFile to registry!\n");
90 return FALSE;
93 ret = RegSetValueEx(
94 hKey,
95 "EventMessageFile",
97 REG_SZ,
98 (BYTE *)program_name,
99 strlen(program_name) + 1);
101 if (ret != ERROR_SUCCESS) {
102 fprintf(stderr, "Failed to write EventMessageFile to registry!\n");
103 return FALSE;
106 ret = RegSetValueEx(
107 hKey,
108 "ParameterMessageFile",
110 REG_SZ,
111 (BYTE *)program_name,
112 strlen(program_name) + 1);
114 if (ret != ERROR_SUCCESS) {
115 fprintf(stderr, "Failed to write ParameterMessageFile to registry!\n");
116 return FALSE;
119 data = EVENTLOG_ERROR_TYPE | EVENTLOG_INFORMATION_TYPE | EVENTLOG_WARNING_TYPE;
120 ret = RegSetValueEx(
121 hKey,
122 "TypesSupported",
124 REG_DWORD,
125 (BYTE *)&data,
126 sizeof(DWORD));
128 if (ret != ERROR_SUCCESS) {
129 fprintf(stderr, "Failed to write TypesSupported to registry!\n");
130 return FALSE;
133 RegCloseKey(hKey);
135 return TRUE;
138 void *eventlog_init(void)
140 HANDLE handle;
141 handle = RegisterEventSource(NULL, "gammu-smsd");
142 if (handle == NULL) {
143 fprintf(stderr, "Error opening event log!\n");
145 return (void *)handle;
148 void eventlog_log(void *handle, int level, const char *message)
150 LPCTSTR lpstrings[1];
151 WORD evtype = EVENTLOG_ERROR_TYPE;
152 DWORD eventid = 0;
153 DWORD eventcat = 0;
155 switch (level) {
156 case DEBUG_ERROR:
157 evtype = EVENTLOG_ERROR_TYPE;
158 eventid = EVENT_MSG_ERROR;
159 eventcat = EVENT_CAT_SMSD;
160 break;
161 case DEBUG_INFO:
162 evtype = EVENTLOG_SUCCESS;
163 eventid = EVENT_MSG_INFO;
164 eventcat = EVENT_CAT_SMSD;
165 break;
166 case DEBUG_NOTICE:
167 eventid = EVENT_MSG_NOTICE;
168 evtype = EVENTLOG_INFORMATION_TYPE;
169 eventcat = EVENT_CAT_SMSD;
170 break;
171 case DEBUG_SQL:
172 eventid = EVENT_MSG_SQL;
173 evtype = EVENTLOG_INFORMATION_TYPE;
174 eventcat = EVENT_CAT_SQL;
175 break;
176 case DEBUG_GAMMU:
177 eventid = EVENT_MSG_GAMMU;
178 evtype = EVENTLOG_INFORMATION_TYPE;
179 eventcat = EVENT_CAT_GAMMU;
180 break;
181 default:
182 eventid = EVENT_MSG_OTHER;
183 evtype = EVENTLOG_INFORMATION_TYPE;
184 eventcat = EVENT_CAT_SMSD;
185 break;
187 lpstrings[0] = message;
189 * @todo: 1024 is probably wrong, we should use mc to get proper
190 * event identifiers.
192 ReportEvent(handle, evtype, eventcat, eventid, NULL, 1, 0,
193 lpstrings, NULL);
196 void eventlog_close(void *handle)
198 DeregisterEventSource(handle);
201 /* How should editor hadle tabs in this file? Add editor commands here.
202 * vim: noexpandtab sw=8 ts=8 sts=8: