927592e9cfd6f923036ac8b3653422178ed09397
[aaf/sshsm.git] / SoftHSMv2 / src / lib / win32 / syslog.cpp
1 #include <config.h>
2
3 #include <stdio.h>
4 #include <stdarg.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <syslog.h>
8
9 #ifdef _WIN32
10
11 static HANDLE hEventLog = NULL;
12
13 /*
14  * Close the Handle to the application Event Log
15  */
16 void
17 closelog() {
18         DeregisterEventSource(hEventLog);
19 }
20
21 /*
22  * Initialize event logging
23  */
24 void
25 openlog(const char *ident, int logopt, int facility) {
26         /* Get a handle to the Application event log */
27         hEventLog = RegisterEventSourceA(NULL, ident);
28 }
29
30 /*
31  * Log to the NT Event Log
32  */
33 void
34 syslog(int priority, const char *message, ...) {
35         va_list ap;
36         char buf[1024];
37         LPCSTR str[1];
38
39         str[0] = buf;
40
41         va_start(ap, message);
42         vsprintf(buf, message, ap);
43         va_end(ap);
44
45         /* Make sure that the channel is open to write the event */
46         if (hEventLog == NULL) {
47                 openlog("SoftHSM", 0, 0);
48         }
49         if (hEventLog != NULL) {
50                 switch (priority) {
51                 case LOG_INFO:
52                 case LOG_NOTICE:
53                 case LOG_DEBUG:
54                         ReportEventA(hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
55                                      0x40000003, NULL, 1, 0, str, NULL);
56                         break;
57                 case LOG_WARNING:
58                         ReportEventA(hEventLog, EVENTLOG_WARNING_TYPE, 0,
59                                      0x80000002, NULL, 1, 0, str, NULL);
60                         break;
61                 default:
62                         ReportEventA(hEventLog, EVENTLOG_ERROR_TYPE, 0,
63                                      0xc0000001, NULL, 1, 0, str, NULL);
64                         break;
65                 }
66         }
67 }
68
69 #endif