]> The Tcpdump Group git mirrors - libpcap/blob - rpcapd/log.c
Temporarily remove the Windows "system log" code.
[libpcap] / rpcapd / log.c
1 #include <stdio.h>
2 #include <stdarg.h>
3 #include <stdlib.h>
4
5 #ifdef _WIN32
6 #include <windows.h>
7 #else
8 #include <syslog.h>
9 #endif
10
11 #include "log.h"
12
13 static int log_to_systemlog;
14 static int log_debug_messages;
15
16 static void rpcapd_vlog_stderr(log_priority,
17 PCAP_FORMAT_STRING(const char *), va_list) PCAP_PRINTFLIKE(2, 0);
18
19 static void rpcapd_vlog_stderr(log_priority priority, const char *message, va_list ap)
20 {
21 const char *tag;
22
23 /*
24 * Squelch warnings from compilers that *don't* assume that
25 * priority always has a valid enum value and therefore don't
26 * assume that we'll always go through one of the case arms.
27 *
28 * If we have a default case, compilers that *do* assume that
29 * will then complain about the default case code being
30 * unreachable.
31 *
32 * Damned if you do, damned if you don't.
33 */
34 tag = "";
35
36 switch (priority) {
37
38 case LOGPRIO_DEBUG:
39 tag = "DEBUG: ";
40 break;
41
42 case LOGPRIO_INFO:
43 tag = "";
44 break;
45
46 case LOGPRIO_WARNING:
47 tag = "warning: ";
48 break;
49
50 case LOGPRIO_ERROR:
51 tag = "error: ";
52 break;
53 }
54
55 fprintf(stderr, "rpcapd: %s", tag);
56 vfprintf(stderr, message, ap);
57 putc('\n', stderr);
58 }
59
60 static void rpcapd_vlog_systemlog(log_priority,
61 PCAP_FORMAT_STRING(const char *), va_list) PCAP_PRINTFLIKE(2, 0);
62
63 #ifdef _WIN32
64 #define MESSAGE_SUBKEY \
65 "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\rpcapd"
66
67 static void rpcapd_vlog_systemlog(log_priority priority, const char *message,
68 va_list ap)
69 {
70 #if 0
71 static int initialized = 0;
72 HKEY hey_handle;
73 static HANDLE log_handle;
74 WORD eventlog_type;
75 DWORD event_id;
76 char msgbuf[1024];
77 char *strings[1];
78
79 if (!initialized) {
80 /*
81 * Register our message stuff in the Registry.
82 *
83 * First, create the registry key for us. If the key
84 * already exists, this succeeds and returns a handle
85 * for it.
86 */
87 if (RegCreateKey(HKEY_LOCAL_MACHINE, MESSAGE_SUBKEY,
88 &key_handle) != ERROR_SUCCESS) {
89 /*
90 * Failed - give up and just log this message,
91 * and all subsequent messages, to the
92 * standard error.
93 */
94 log_to_systemlog = 0;
95 initialized = 1;
96 rpcapd_vlog_stderr(priority, message, ap);
97 return;
98 }
99 log_handle = RegisterEventSource(NULL, "rpcapd");
100 initialized = 1;
101 }
102
103 switch (priority) {
104
105 case LOGPRIO_DEBUG:
106 //
107 // XXX - what *should* we do about debug messages?
108 //
109 eventlog_type = EVENTLOG_INFORMATION_TYPE;
110 event_id = RPCAPD_INFO_ID;
111 break;
112
113 case LOGPRIO_INFO:
114 eventlog_type = EVENTLOG_INFORMATION_TYPE;
115 event_id = RPCAPD_INFO_ID;
116 break;
117
118 case LOGPRIO_WARNING:
119 eventlog_type = EVENTLOG_WARNING_TYPE;
120 event_id = RPCAPD_WARNING_ID;
121 break;
122
123 case LOGPRIO_ERROR:
124 eventlog_type = EVENTLOG_ERROR_TYPE;
125 event_id = RPCAPD_ERROR_ID;
126 break;
127
128 default:
129 /* Don't do this. */
130 return;
131 }
132
133 vsprintf(msgbuf, message, ap);
134
135 strings[0] = msgbuf;
136 /*
137 * If this fails, how are we going to report it?
138 */
139 (void) ReportEvent(log_handle, eventlog_type, 0, event_id, NULL, 1, 0,
140 strings, NULL);
141 #else
142 rpcapd_vlog_stderr(priority, message, ap);
143 #endif
144 }
145 #else
146 static void rpcapd_vlog_systemlog(log_priority priority, const char *message,
147 va_list ap)
148 {
149 static int initialized = 0;
150 int syslog_priority;
151
152 if (!initialized) {
153 //
154 // Open the log.
155 //
156 openlog("rpcapd", LOG_PID, LOG_DAEMON);
157 initialized = 1;
158 }
159
160 switch (priority) {
161
162 case LOGPRIO_DEBUG:
163 syslog_priority = LOG_DEBUG;
164 break;
165
166 case LOGPRIO_INFO:
167 syslog_priority = LOG_INFO;
168 break;
169
170 case LOGPRIO_WARNING:
171 syslog_priority = LOG_WARNING;
172 break;
173
174 case LOGPRIO_ERROR:
175 syslog_priority = LOG_ERR;
176 break;
177
178 default:
179 /* Don't do this. */
180 return;
181 }
182
183 vsyslog(syslog_priority, message, ap);
184 }
185 #endif
186
187 void rpcapd_log_set(int log_to_systemlog_arg, int log_debug_messages_arg)
188 {
189 log_debug_messages = log_debug_messages_arg;
190 log_to_systemlog = log_to_systemlog_arg;
191 }
192
193 void rpcapd_log(log_priority priority, const char *message, ...)
194 {
195 va_list ap;
196
197 if (priority != LOGPRIO_DEBUG || log_debug_messages) {
198 va_start(ap, message);
199 if (log_to_systemlog)
200 {
201 rpcapd_vlog_systemlog(priority, message, ap);
202 }
203 else
204 {
205 rpcapd_vlog_stderr(priority, message, ap);
206 }
207 va_end(ap);
208 }
209 }