Debian Codegraph @

src/util/simplelog.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /* simplelog.c - Simple logging subsystem for Poldi
2 Copyright (C) 2008 g10 Code GmbH
3
4 This file is part of Poldi.
5
6 Poldi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 Poldi is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20 #include <util-local.h>
21
22 #include <stdio.h>
23 #include <assert.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <time.h>
27 #include <assert.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <syslog.h>
31
32 #include <gpg-error.h>
33
34 #include "simplelog.h"
35
36 struct log_handle
37 {
38 log_backend_t backend;
39 log_level_t min_level;
40 unsigned int flags;
41 char prefix[LOG_PREFIX_LENGTH];
42 FILE *stream;
43 };
44
45 static gpg_error_t
46 internal_release_backend (log_handle_t handle)
47 {
48 assert (handle->backend != LOG_BACKEND_NONE);
49
50 switch (handle->backend)
51 {
52 case LOG_BACKEND_NONE:
53 case LOG_BACKEND_STREAM:
54 case LOG_BACKEND_SYSLOG:
55 /* Nothing to do here. */
56 break;
57
58 case LOG_BACKEND_FILE:
59 /* FIXME: error checking. */
60 assert (handle->stream);
61 fclose (handle->stream);
62 break;
63 }
64
65 handle->backend = LOG_BACKEND_NONE;
66
67 return 0;
68 }
69
70 static gpg_error_t
71 internal_set_backend_file (log_handle_t handle, const char *filename)
72 {
73 gpg_error_t err = 0;
74 FILE *stream = NULL;
75
76 assert (handle->backend == LOG_BACKEND_NONE);
77
78 stream = fopen (filename, "a");
79 if (!stream)
80 {
81 err = gpg_error_from_errno (errno);
82 goto out;
83 }
84
85 handle->backend = LOG_BACKEND_FILE;
86 handle->stream = stream;
87
88 out:
89
90 return err;
91 }
92
93 static gpg_error_t
94 internal_set_backend_stream (log_handle_t handle, FILE *stream)
95 {
96 assert (handle->backend == LOG_BACKEND_NONE);
97 assert (stream);
98
99 handle->backend = LOG_BACKEND_STREAM;
100 handle->stream = stream;
101
102 return 0;
103 }
104
105 static gpg_error_t
106 internal_set_backend_syslog (log_handle_t handle)
107 {
108 assert (handle->backend == LOG_BACKEND_NONE);
109
110 handle->backend = LOG_BACKEND_SYSLOG;
111
112 return 0;
113 }
114
115 gpg_error_t
116 log_create (log_handle_t *handle)
117 {
118 gpg_error_t err = 0;
119
120 *handle = xtrymalloc (sizeof (**handle));
121 if (!*handle)
122 {
123 err = gpg_error_from_errno (errno);
124 goto out;
125 }
126
127 (*handle)->backend = LOG_BACKEND_NONE;
128 (*handle)->min_level = LOG_LEVEL_INFO;
129 (*handle)->flags = 0;
130 (*handle)->prefix[0] = 0;
131
132 out:
133
134 return err;
135 }
136
137 void
138 log_destroy (log_handle_t handle)
139 {
140 if (handle)
141 {
142 if (handle->backend != LOG_BACKEND_NONE)
143 internal_release_backend (handle);
144 xfree (handle);
145 }
146 }
147
148 void
149 log_set_flags (log_handle_t handle, unsigned int flags)
150 {
151 handle->flags |= flags;
152 }
153
154 void
155 log_unset_flags (log_handle_t handle, unsigned int flags)
156 {
157 handle->flags &= ~flags;
158 }
159
160 gpg_error_t
161 log_set_backend_file (log_handle_t handle, const char *filename)
162 {
163 gpg_error_t err;
164
165 assert (handle);
166
167 if (handle->backend != LOG_BACKEND_NONE)
168 internal_release_backend (handle);
169
170 err = internal_set_backend_file (handle, filename);
171
172 return err;
173 }
174
175 gpg_error_t
176 log_set_backend_stream (log_handle_t handle, FILE *stream)
177 {
178 gpg_error_t err;
179
180 assert (handle);
181
182 if (handle->backend != LOG_BACKEND_NONE)
183 internal_release_backend (handle);
184
185 err = internal_set_backend_stream (handle, stream);
186
187 return err;
188 }
189
190 gpg_error_t
191 log_set_backend_syslog (log_handle_t handle)
192 {
193 gpg_error_t err;
194
195 assert (handle);
196
197 if (handle->backend != LOG_BACKEND_NONE)
198 internal_release_backend (handle);
199
200 err = internal_set_backend_syslog (handle);
201
202 return err;
203 }
204
205
206 void
207 log_set_prefix (log_handle_t handle, const char *prefix)
208 {
209 assert (handle);
210
211 strncpy (handle->prefix, prefix, sizeof (handle->prefix) - 1);
212 handle->prefix[sizeof (handle->prefix) - 1] = 0;
213 }
214
215 void
216 log_set_min_level (log_handle_t handle, log_level_t min_level)
217 {
218 assert (handle);
219
220 if (min_level == LOG_LEVEL_DEBUG
221 || min_level == LOG_LEVEL_INFO
222 || min_level == LOG_LEVEL_ERROR
223 || min_level == LOG_LEVEL_FATAL)
224 handle->min_level = min_level;
225 }
226
227 static gpg_error_t
228 internal_log_write (log_handle_t handle, log_level_t level,
229 const char *fmt, va_list ap)
230 {
231 gpg_error_t err;
232
233 assert (handle->backend != LOG_BACKEND_NONE);
234
235 /* FIXME: shall we do error checking here? And what if an error
236 occurs? */
237
238 if (level < handle->min_level)
239 /* User does not want to receive messages for level smaller than
240 min_level. */
241 return 0;
242
243 if (handle->backend == LOG_BACKEND_SYSLOG)
244 {
245 int syslog_priority;
246
247 switch (level)
248 {
249 case LOG_LEVEL_DEBUG:
250 syslog_priority = LOG_DEBUG;
251 break;
252
253 case LOG_LEVEL_INFO:
254 syslog_priority = LOG_INFO;
255 break;
256
257 case LOG_LEVEL_ERROR:
258 syslog_priority = LOG_ERR;
259 break;
260
261 case LOG_LEVEL_FATAL:
262 syslog_priority = LOG_ALERT;
263 break;
264
265 default:
266 /* FIXME: what to do when the user passes an invalid log
267 level? -mo */
268 syslog_priority = LOG_ERR;
269 break;
270 }
271
272 vsyslog (LOG_MAKEPRI (LOG_AUTH, syslog_priority), fmt, ap);
273 err = 0;
274 }
275 else if (handle->backend == LOG_BACKEND_STREAM
276 || handle->backend == LOG_BACKEND_FILE)
277 {
278 FILE *stream = handle->stream;
279
280 assert (stream);
281
282 if ((handle->flags & LOG_FLAG_WITH_PREFIX) && (*handle->prefix != 0))
283 fprintf (stream, "%s ", handle->prefix);
284
285 if (handle->flags & LOG_FLAG_WITH_TIME)
286 {
287 struct tm *tp;
288 time_t atime = time (NULL);
289
290 tp = localtime (&atime);
291 fprintf (stream, "%04d-%02d-%02d %02d:%02d:%02d ",
292 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday,
293 tp->tm_hour, tp->tm_min, tp->tm_sec);
294 }
295
296 if (handle->flags & LOG_FLAG_WITH_PID)
297 fprintf (stream, "[%u] ", (unsigned int) getpid ());
298
299 switch (level)
300 {
301 case LOG_LEVEL_ERROR:
302 case LOG_LEVEL_FATAL:
303 fprintf (stream, "error: ");
304 break;
305
306 case LOG_LEVEL_DEBUG:
307 fprintf (stream, "debug: ");
308 break;
309
310 case LOG_LEVEL_INFO:
311 break;
312 }
313
314 vfprintf (stream, fmt, ap);
315 putc ('\n', stream);
316
317 err = 0;
318 }
319
320 return err;
321 }
322
323 gpg_error_t
324 log_write (log_handle_t handle, log_level_t level,
325 const char *fmt, ...)
326 {
327 gpg_error_t err = 0;
328
329 assert (handle);
330
331 if (handle->backend != LOG_BACKEND_NONE)
332 {
333 va_list ap;
334
335 va_start (ap, fmt);
336 err = internal_log_write (handle, level, fmt, ap);
337 va_end (ap);
338 }
339
340 return err;
341 }
342
343 gpg_error_t
344 log_write_va (log_handle_t handle, log_level_t level,
345 const char *fmt, va_list ap)
346 {
347 gpg_error_t err = 0;
348
349 assert (handle);
350
351 if (handle->backend != LOG_BACKEND_NONE)
352 err = internal_log_write (handle, level, fmt, ap);
353
354 return err;
355 }
356
357 gpg_error_t
358 log_msg_debug (log_handle_t handle, const char *fmt, ...)
359 {
360 gpg_error_t err;
361 va_list ap;
362
363 if (!handle)
364 return 0;
365
366 va_start (ap, fmt);
367 err = log_write_va (handle, LOG_LEVEL_DEBUG, fmt, ap);
368 va_end (ap);
369
370 return err;
371 }
372
373 gpg_error_t
374 log_msg_info (log_handle_t handle, const char *fmt, ...)
375 {
376 gpg_error_t err;
377 va_list ap;
378
379 if (!handle)
380 return 0;
381
382 va_start (ap, fmt);
383 err = log_write_va (handle, LOG_LEVEL_INFO, fmt, ap);
384 va_end (ap);
385
386 return err;
387 }
388
389 gpg_error_t
390 log_msg_error (log_handle_t handle, const char *fmt, ...)
391 {
392 gpg_error_t err;
393 va_list ap;
394
395 if (!handle)
396 return 0;
397
398 va_start (ap, fmt);
399 err = log_write_va (handle, LOG_LEVEL_ERROR, fmt, ap);
400 va_end (ap);
401
402 return err;
403 }
404
405 gpg_error_t
406 log_msg_fatal (log_handle_t handle, const char *fmt, ...)
407 {
408 gpg_error_t err;
409 va_list ap;
410
411 if (!handle)
412 return 0;
413
414 va_start (ap, fmt);
415 err = log_write_va (handle, LOG_LEVEL_FATAL, fmt, ap);
416 va_end (ap);
417
418 return err;
419 }
420
421 /* END */