Mercurial > trustbridge
comparison common/logging.c @ 615:2a4f7364ab81
Add first simple event logging functions
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Thu, 19 Jun 2014 11:53:07 +0200 |
parents | 17e1c8f37d72 |
children | 0172740f5c6e |
comparison
equal
deleted
inserted
replaced
614:de1e3a47ed21 | 615:2a4f7364ab81 |
---|---|
7 */ | 7 */ |
8 #include "logging.h" | 8 #include "logging.h" |
9 #include "strhelp.h" | 9 #include "strhelp.h" |
10 | 10 |
11 #include <stdio.h> | 11 #include <stdio.h> |
12 #include <stdarg.h> | |
13 #include <stdbool.h> | |
14 | |
15 #include <strhelp.h> | |
12 | 16 |
13 #ifdef WIN32 | 17 #ifdef WIN32 |
18 # include <windows.h> | |
19 #else | |
20 # include <syslog.h> | |
21 #endif | |
22 | |
23 | |
24 #ifdef WIN32 | |
25 static void | |
26 win_log(const char *format, va_list ap, bool error) | |
27 { | |
28 HANDLE log_src = NULL; | |
29 wchar_t *wmsg = NULL; | |
30 BOOL failure = TRUE; | |
31 WORD type = 0, | |
32 category = 0; | |
33 char buffer[MAX_LOG+1]; | |
34 | |
35 vsnprintf (buffer, MAX_LOG, format, ap); | |
36 buffer[MAX_LOG] = '\0'; | |
37 | |
38 log_src = RegisterEventSourceA (NULL, LOG_NAME); | |
39 | |
40 if (log_src == NULL) | |
41 { | |
42 PRINTLASTERROR ("Failed to open log source."); | |
43 return; | |
44 } | |
45 | |
46 if (error) | |
47 { | |
48 type = EVENTLOG_ERROR_TYPE; | |
49 } | |
50 else | |
51 { | |
52 type = EVENTLOG_INFORMATION_TYPE; | |
53 } | |
54 | |
55 wmsg = utf8_to_wchar (buffer, strlen(buffer)); | |
56 if (wmsg == NULL) | |
57 { | |
58 ERRORPRINTF ("Failed to convert log message to utf-16"); | |
59 goto done; | |
60 } | |
61 | |
62 failure = ReportEventW (log_src, | |
63 type, | |
64 category, | |
65 0, | |
66 NULL, | |
67 1, | |
68 0, | |
69 (const WCHAR **) &wmsg, | |
70 NULL); | |
71 if (failure) | |
72 { | |
73 PRINTLASTERROR ("Failed to report event."); | |
74 } | |
75 | |
76 done: | |
77 xfree (wmsg); | |
78 | |
79 if (!DeregisterEventSource (log_src)) | |
80 { | |
81 PRINTLASTERROR ("Failed to close log source."); | |
82 } | |
83 return; | |
84 } | |
85 | |
14 char * | 86 char * |
15 getLastErrorMsg() | 87 getLastErrorMsg() |
16 { | 88 { |
17 LPWSTR bufPtr = NULL; | 89 LPWSTR bufPtr = NULL; |
18 DWORD err = GetLastError(); | 90 DWORD err = GetLastError(); |
42 LocalFree (bufPtr); | 114 LocalFree (bufPtr); |
43 | 115 |
44 return retval; | 116 return retval; |
45 } | 117 } |
46 | 118 |
119 #else /* WIN32 */ | |
120 | |
121 | |
122 static void | |
123 linux_log (const char *format, va_list ap, bool error) | |
124 { | |
125 openlog (LOG_NAME, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER); | |
126 vsyslog ( error ? LOG_ERR : LOG_INFO, format, ap); | |
127 } | |
128 | |
129 #endif /* WIN32 */ | |
130 | |
131 void | |
132 syslog_info_printf(const char *format, ...) | |
133 { | |
134 va_list args; | |
135 va_start (args, format); | |
136 #ifdef WIN32 | |
137 win_log (format, args, false); | |
138 #else | |
139 linux_log (format, args, false); | |
47 #endif | 140 #endif |
141 va_end (args); | |
142 } | |
143 | |
144 void | |
145 syslog_error_printf(const char *format, ...) | |
146 { | |
147 va_list args; | |
148 va_start (args, format); | |
149 #ifdef WIN32 | |
150 win_log (format, args, true); | |
151 #else | |
152 linux_log (format, args, true); | |
153 #endif | |
154 va_end (args); | |
155 } |