]> The Tcpdump Group git mirrors - libpcap/blob - rpcapd/log.c
CI: Call print_so_deps() on rpcapd in remote enabled build
[libpcap] / rpcapd / log.c
1 /*
2 * Copyright (c) 1993, 1994, 1995, 1996, 1998
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 */
21
22 #include <config.h>
23
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27
28 #ifdef _WIN32
29 /* Prevent inclusion of winsock.h, which causes redefinition errors when
30 * winsock2.h is included later */
31 #define WIN32_LEAN_AND_MEAN
32 #include <windows.h>
33 #else
34 #include <syslog.h>
35 #endif
36
37 #include "portability.h"
38
39 #include "log.h"
40
41 static int log_to_systemlog;
42 static int log_debug_messages;
43
44 static void rpcapd_vlog_stderr(log_priority,
45 PCAP_FORMAT_STRING(const char *), va_list) PCAP_PRINTFLIKE(2, 0);
46
47 static void rpcapd_vlog_stderr(log_priority priority, const char *message, va_list ap)
48 {
49 const char *tag;
50
51 /*
52 * Squelch warnings from compilers that *don't* assume that
53 * priority always has a valid enum value and therefore don't
54 * assume that we'll always go through one of the case arms.
55 *
56 * If we have a default case, compilers that *do* assume that
57 * will then complain about the default case code being
58 * unreachable.
59 *
60 * Damned if you do, damned if you don't.
61 */
62 tag = "";
63
64 switch (priority) {
65
66 case LOGPRIO_DEBUG:
67 tag = "DEBUG: ";
68 break;
69
70 case LOGPRIO_INFO:
71 tag = "";
72 break;
73
74 case LOGPRIO_WARNING:
75 tag = "warning: ";
76 break;
77
78 case LOGPRIO_ERROR:
79 tag = "error: ";
80 break;
81 }
82
83 fprintf(stderr, "rpcapd: %s", tag);
84 vfprintf(stderr, message, ap);
85 putc('\n', stderr);
86 }
87
88 static void rpcapd_vlog_systemlog(log_priority,
89 PCAP_FORMAT_STRING(const char *), va_list) PCAP_PRINTFLIKE(2, 0);
90
91 #ifdef _WIN32
92 #define MESSAGE_SUBKEY \
93 "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\rpcapd"
94
95 static void rpcapd_vlog_systemlog(log_priority priority, const char *message,
96 va_list ap)
97 {
98 #if 0
99 static int initialized = 0;
100 HKEY hey_handle;
101 static HANDLE log_handle;
102 WORD eventlog_type;
103 DWORD event_id;
104 char msgbuf[1024];
105 char *strings[1];
106
107 if (!initialized) {
108 /*
109 * Register our message stuff in the Registry.
110 *
111 * First, create the registry key for us. If the key
112 * already exists, this succeeds and returns a handle
113 * for it.
114 */
115 if (RegCreateKey(HKEY_LOCAL_MACHINE, MESSAGE_SUBKEY,
116 &key_handle) != ERROR_SUCCESS) {
117 /*
118 * Failed - give up and just log this message,
119 * and all subsequent messages, to the
120 * standard error.
121 */
122 log_to_systemlog = 0;
123 initialized = 1;
124 rpcapd_vlog_stderr(priority, message, ap);
125 return;
126 }
127 log_handle = RegisterEventSource(NULL, "rpcapd");
128 initialized = 1;
129 }
130
131 switch (priority) {
132
133 case LOGPRIO_DEBUG:
134 //
135 // XXX - what *should* we do about debug messages?
136 //
137 eventlog_type = EVENTLOG_INFORMATION_TYPE;
138 event_id = RPCAPD_INFO_ID;
139 break;
140
141 case LOGPRIO_INFO:
142 eventlog_type = EVENTLOG_INFORMATION_TYPE;
143 event_id = RPCAPD_INFO_ID;
144 break;
145
146 case LOGPRIO_WARNING:
147 eventlog_type = EVENTLOG_WARNING_TYPE;
148 event_id = RPCAPD_WARNING_ID;
149 break;
150
151 case LOGPRIO_ERROR:
152 eventlog_type = EVENTLOG_ERROR_TYPE;
153 event_id = RPCAPD_ERROR_ID;
154 break;
155
156 default:
157 /* Don't do this. */
158 return;
159 }
160
161 vsprintf(msgbuf, message, ap);
162
163 strings[0] = msgbuf;
164 /*
165 * If this fails, how are we going to report it?
166 */
167 (void) ReportEvent(log_handle, eventlog_type, 0, event_id, NULL, 1, 0,
168 strings, NULL);
169 #else
170 rpcapd_vlog_stderr(priority, message, ap);
171 #endif
172 }
173 #else
174 static void rpcapd_vlog_systemlog(log_priority priority, const char *message,
175 va_list ap)
176 {
177 static int initialized = 0;
178 int syslog_priority;
179
180 if (!initialized) {
181 //
182 // Open the log.
183 //
184 openlog("rpcapd", LOG_PID, LOG_DAEMON);
185 initialized = 1;
186 }
187
188 switch (priority) {
189
190 case LOGPRIO_DEBUG:
191 syslog_priority = LOG_DEBUG;
192 break;
193
194 case LOGPRIO_INFO:
195 syslog_priority = LOG_INFO;
196 break;
197
198 case LOGPRIO_WARNING:
199 syslog_priority = LOG_WARNING;
200 break;
201
202 case LOGPRIO_ERROR:
203 syslog_priority = LOG_ERR;
204 break;
205
206 default:
207 /* Don't do this. */
208 return;
209 }
210
211 #ifdef HAVE_VSYSLOG
212 vsyslog(syslog_priority, message, ap);
213 #else
214 /*
215 * Thanks, IBM, for not providing vsyslog() in AIX!
216 *
217 * They also warn that the syslog functions shouldn't
218 * be used in multithreaded programs, but the only thing
219 * obvious that seems to make the syslog_r functions
220 * better is that they have an additional argument
221 * that points to the information that's static to
222 * the syslog code in non-thread-safe versions. Most
223 * of that data is set by openlog(); since we already
224 * do an openlog before doing logging, and don't
225 * change that data afterwards, I suspect that, in
226 * practice, the regular syslog routines are OK for
227 * us (especially given that we'd end up having one
228 * static struct syslog_data anyway, which means we'd
229 * just be like the non-thread-safe version).
230 */
231 char logbuf[1024+1];
232
233 vsnprintf(logbuf, sizeof logbuf, message, ap);
234 syslog(syslog_priority, "%s", logbuf);
235 #endif
236 }
237 #endif
238
239 void rpcapd_log_set(int log_to_systemlog_arg, int log_debug_messages_arg)
240 {
241 log_debug_messages = log_debug_messages_arg;
242 log_to_systemlog = log_to_systemlog_arg;
243 }
244
245 void rpcapd_log(log_priority priority, const char *message, ...)
246 {
247 va_list ap;
248
249 if (priority != LOGPRIO_DEBUG || log_debug_messages) {
250 va_start(ap, message);
251 if (log_to_systemlog)
252 {
253 rpcapd_vlog_systemlog(priority, message, ap);
254 }
255 else
256 {
257 rpcapd_vlog_stderr(priority, message, ap);
258 }
259 va_end(ap);
260 }
261 }