Debian Codegraph @

src/util/simplelog.c

raw symbols ◀ ▶ SCIP
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>cxx . . $ `<file>/src/util/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"cxx . . $ `<file>/src/util/simplelog.h`/
35
36 struct log_handlecxx . . $ log_handle#
37 {
38 log_backend_tcxx . . $ log_backend_t# backendcxx . . $ log_handle#backend.;
39 log_level_tcxx . . $ log_level_t# min_levelcxx . . $ log_handle#min_level.;
40 unsigned int flagscxx . . $ log_handle#flags.;
41 char prefixcxx . . $ log_handle#prefix.[LOG_PREFIX_LENGTHcxx . . $ `src/util/simplelog.h:50:9`!];
42 FILEcxx . . $ FILE# *streamcxx . . $ log_handle#stream.;
43 };
44
45 static gpg_error_tcxx . . $ gpg_error_t#
46 internal_release_backendcxx . . $ internal_release_backend(7a673cb698c395e0). (log_handle_tcxx . . $ log_handle_t# handle)
47 {
48 assertcxx . . $ `/usr/include/assert.h:118:11`! (handle->backend != LOG_BACKEND_NONE);
49
50 switch (handle->backendcxx . . $ log_handle#backend.)
51 {
52 case LOG_BACKEND_NONEcxx . . $ LOG_BACKEND_NONE.:
53 case LOG_BACKEND_STREAMcxx . . $ LOG_BACKEND_STREAM.:
54 case LOG_BACKEND_SYSLOGcxx . . $ LOG_BACKEND_SYSLOG.:
55 /* Nothing to do here. */
56 break;
57
58 case LOG_BACKEND_FILEcxx . . $ LOG_BACKEND_FILE.:
59 /* FIXME: error checking. */
60 assertcxx . . $ `/usr/include/assert.h:118:11`! (handle->stream);
61 fclosecxx . . $ fclose(89bc1efcdac0ca22). (handle->streamcxx . . $ log_handle#stream.);
62 break;
63 }
64
65 handle->backendcxx . . $ log_handle#backend. = LOG_BACKEND_NONEcxx . . $ LOG_BACKEND_NONE.;
66
67 return 0;
68 }
69
70 static gpg_error_tcxx . . $ gpg_error_t#
71 internal_set_backend_filecxx . . $ internal_set_backend_file(91a1806cddc17828). (log_handle_tcxx . . $ log_handle_t# handle, const char *filename)
72 {
73 gpg_error_tcxx . . $ gpg_error_t# err = 0;
74 FILEcxx . . $ FILE# *stream = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
75
76 assertcxx . . $ `/usr/include/assert.h:118:11`! (handle->backend == LOG_BACKEND_NONE);
77
78 stream = fopencxx . . $ fopen(82ec0787cd37e0a3). (filename, "a");
79 if (!stream)
80 {
81 err = gpg_error_from_errnocxx . . $ gpg_error_from_errno(46cc46841198e427). (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!);
82 goto out;
83 }
84
85 handle->backendcxx . . $ log_handle#backend. = LOG_BACKEND_FILEcxx . . $ LOG_BACKEND_FILE.;
86 handle->streamcxx . . $ log_handle#stream. = stream;
87
88 out:
89
90 return err;
91 }
92
93 static gpg_error_tcxx . . $ gpg_error_t#
94 internal_set_backend_streamcxx . . $ internal_set_backend_stream(79a3f3539912b772). (log_handle_tcxx . . $ log_handle_t# handle, FILEcxx . . $ FILE# *stream)
95 {
96 assertcxx . . $ `/usr/include/assert.h:118:11`! (handle->backend == LOG_BACKEND_NONE);
97 assertcxx . . $ `/usr/include/assert.h:118:11`! (stream);
98
99 handle->backendcxx . . $ log_handle#backend. = LOG_BACKEND_STREAMcxx . . $ LOG_BACKEND_STREAM.;
100 handle->streamcxx . . $ log_handle#stream. = stream;
101
102 return 0;
103 }
104
105 static gpg_error_tcxx . . $ gpg_error_t#
106 internal_set_backend_syslogcxx . . $ internal_set_backend_syslog(7a673cb698c395e0). (log_handle_tcxx . . $ log_handle_t# handle)
107 {
108 assertcxx . . $ `/usr/include/assert.h:118:11`! (handle->backend == LOG_BACKEND_NONE);
109
110 handle->backendcxx . . $ log_handle#backend. = LOG_BACKEND_SYSLOGcxx . . $ LOG_BACKEND_SYSLOG.;
111
112 return 0;
113 }
114
115 gpg_error_tcxx . . $ gpg_error_t#
116 log_createcxx . . $ log_create(ae9175ffffc76e55). (log_handle_tcxx . . $ log_handle_t# *handle)
117 {
118 gpg_error_tcxx . . $ gpg_error_t# err = 0;
119
120 *handle = xtrymalloccxx . . $ `src/poldi.h:35:9`! (sizeof (**handle));
121 if (!*handle)
122 {
123 err = gpg_error_from_errnocxx . . $ gpg_error_from_errno(46cc46841198e427). (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!);
124 goto out;
125 }
126
127 (*handle)->backendcxx . . $ log_handle#backend. = LOG_BACKEND_NONEcxx . . $ LOG_BACKEND_NONE.;
128 (*handle)->min_levelcxx . . $ log_handle#min_level. = LOG_LEVEL_INFOcxx . . $ LOG_LEVEL_INFO.;
129 (*handle)->flagscxx . . $ log_handle#flags. = 0;
130 (*handle)->prefixcxx . . $ log_handle#prefix.[0] = 0;
131
132 out:
133
134 return err;
135 }
136
137 void
138 log_destroycxx . . $ log_destroy(c6d31a59c40299be). (log_handle_tcxx . . $ log_handle_t# handle)
139 {
140 if (handle)
141 {
142 if (handle->backendcxx . . $ log_handle#backend. != LOG_BACKEND_NONEcxx . . $ LOG_BACKEND_NONE.)
143 internal_release_backendcxx . . $ internal_release_backend(7a673cb698c395e0). (handle);
144 xfreecxx . . $ `src/poldi.h:39:9`! (handle);
145 }
146 }
147
148 void
149 log_set_flagscxx . . $ log_set_flags(a836c67e96798efc). (log_handle_tcxx . . $ log_handle_t# handle, unsigned int flags)
150 {
151 handle->flagscxx . . $ log_handle#flags. |= flags;
152 }
153
154 void
155 log_unset_flagscxx . . $ log_unset_flags(a836c67e96798efc). (log_handle_tcxx . . $ log_handle_t# handle, unsigned int flags)
156 {
157 handle->flagscxx . . $ log_handle#flags. &= ~flags;
158 }
159
160 gpg_error_tcxx . . $ gpg_error_t#
161 log_set_backend_filecxx . . $ log_set_backend_file(91a1806cddc17828). (log_handle_tcxx . . $ log_handle_t# handle, const char *filename)
162 {
163 gpg_error_tcxx . . $ gpg_error_t# err;
164
165 assertcxx . . $ `/usr/include/assert.h:118:11`! (handle);
166
167 if (handle->backendcxx . . $ log_handle#backend. != LOG_BACKEND_NONEcxx . . $ LOG_BACKEND_NONE.)
168 internal_release_backendcxx . . $ internal_release_backend(7a673cb698c395e0). (handle);
169
170 err = internal_set_backend_filecxx . . $ internal_set_backend_file(91a1806cddc17828). (handle, filename);
171
172 return err;
173 }
174
175 gpg_error_tcxx . . $ gpg_error_t#
176 log_set_backend_streamcxx . . $ log_set_backend_stream(79a3f3539912b772). (log_handle_tcxx . . $ log_handle_t# handle, FILEcxx . . $ FILE# *stream)
177 {
178 gpg_error_tcxx . . $ gpg_error_t# err;
179
180 assertcxx . . $ `/usr/include/assert.h:118:11`! (handle);
181
182 if (handle->backendcxx . . $ log_handle#backend. != LOG_BACKEND_NONEcxx . . $ LOG_BACKEND_NONE.)
183 internal_release_backendcxx . . $ internal_release_backend(7a673cb698c395e0). (handle);
184
185 err = internal_set_backend_streamcxx . . $ internal_set_backend_stream(79a3f3539912b772). (handle, stream);
186
187 return err;
188 }
189
190 gpg_error_tcxx . . $ gpg_error_t#
191 log_set_backend_syslogcxx . . $ log_set_backend_syslog(7a673cb698c395e0). (log_handle_tcxx . . $ log_handle_t# handle)
192 {
193 gpg_error_tcxx . . $ gpg_error_t# err;
194
195 assertcxx . . $ `/usr/include/assert.h:118:11`! (handle);
196
197 if (handle->backendcxx . . $ log_handle#backend. != LOG_BACKEND_NONEcxx . . $ LOG_BACKEND_NONE.)
198 internal_release_backendcxx . . $ internal_release_backend(7a673cb698c395e0). (handle);
199
200 err = internal_set_backend_syslogcxx . . $ internal_set_backend_syslog(7a673cb698c395e0). (handle);
201
202 return err;
203 }
204
205
206 void
207 log_set_prefixcxx . . $ log_set_prefix(d0a2023680b6f78d). (log_handle_tcxx . . $ log_handle_t# handle, const char *prefix)
208 {
209 assertcxx . . $ `/usr/include/assert.h:118:11`! (handle);
210
211 strncpycxx . . $ strncpy(255158a415b673b0). (handle->prefixcxx . . $ log_handle#prefix., prefix, sizeof (handle->prefixcxx . . $ log_handle#prefix.) - 1);
212 handle->prefixcxx . . $ log_handle#prefix.[sizeof (handle->prefixcxx . . $ log_handle#prefix.) - 1] = 0;
213 }
214
215 void
216 log_set_min_levelcxx . . $ log_set_min_level(af723febd09b6652). (log_handle_tcxx . . $ log_handle_t# handle, log_level_tcxx . . $ log_level_t# min_level)
217 {
218 assertcxx . . $ `/usr/include/assert.h:118:11`! (handle);
219
220 if (min_level == LOG_LEVEL_DEBUGcxx . . $ LOG_LEVEL_DEBUG.
221 || min_level == LOG_LEVEL_INFOcxx . . $ LOG_LEVEL_INFO.
222 || min_level == LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR.
223 || min_level == LOG_LEVEL_FATALcxx . . $ LOG_LEVEL_FATAL.)
224 handle->min_levelcxx . . $ log_handle#min_level. = min_level;
225 }
226
227 static gpg_error_tcxx . . $ gpg_error_t#
228 internal_log_writecxx . . $ internal_log_write(247b63e26cc1c53c). (log_handle_tcxx . . $ log_handle_t# handle, log_level_tcxx . . $ log_level_t# level,
229 const char *fmt, va_listcxx . . $ va_list# ap)
230 {
•231 gpg_error_tcxx . . $ gpg_error_t# err = 0;
232
233 assertcxx . . $ `/usr/include/assert.h:118:11`! (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_levelcxx . . $ log_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->backendcxx . . $ log_handle#backend. == LOG_BACKEND_SYSLOGcxx . . $ LOG_BACKEND_SYSLOG.)
244 {
245 int syslog_priority;
246
247 switch (level)
248 {
249 case LOG_LEVEL_DEBUGcxx . . $ LOG_LEVEL_DEBUG.:
250 syslog_priority = LOG_DEBUGcxx . . $ `<external>/6168640806541382331/syslog.h:58:9`!;
251 break;
252
253 case LOG_LEVEL_INFOcxx . . $ LOG_LEVEL_INFO.:
254 syslog_priority = LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!;
255 break;
256
257 case LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR.:
258 syslog_priority = LOG_ERRcxx . . $ `<external>/6168640806541382331/syslog.h:54:9`!;
259 break;
260
261 case LOG_LEVEL_FATALcxx . . $ LOG_LEVEL_FATAL.:
262 syslog_priority = LOG_ALERTcxx . . $ `<external>/6168640806541382331/syslog.h:52:9`!;
263 break;
264
265 default:
266 /* FIXME: what to do when the user passes an invalid log
267 level? -mo */
268 syslog_priority = LOG_ERRcxx . . $ `<external>/6168640806541382331/syslog.h:54:9`!;
269 break;
270 }
271
272 vsyslogcxx . . $ vsyslog(7aa579bceef62d01). (LOG_MAKEPRIcxx . . $ `<external>/6168640806541382331/syslog.h:63:9`! (LOG_AUTHcxx . . $ `<external>/6168640806541382331/syslog.h:97:9`!, syslog_priority), fmt, ap);
273 }
274 else if (handle->backendcxx . . $ log_handle#backend. == LOG_BACKEND_STREAMcxx . . $ LOG_BACKEND_STREAM.
275 || handle->backendcxx . . $ log_handle#backend. == LOG_BACKEND_FILEcxx . . $ LOG_BACKEND_FILE.)
276 {
277 FILEcxx . . $ FILE# *stream = handle->streamcxx . . $ log_handle#stream.;
278
279 assertcxx . . $ `/usr/include/assert.h:118:11`! (stream);
280
281 if ((handle->flagscxx . . $ log_handle#flags. & LOG_FLAG_WITH_PREFIXcxx . . $ `src/util/simplelog.h:30:9`!) && (*handle->prefixcxx . . $ log_handle#prefix. != 0))
282 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac). (stream, "%s ", handle->prefixcxx . . $ log_handle#prefix.);
283
284 if (handle->flagscxx . . $ log_handle#flags. & LOG_FLAG_WITH_TIMEcxx . . $ `src/util/simplelog.h:31:9`!)
285 {
286 struct tmcxx . . $ tm# *tp;
287 time_tcxx . . $ time_t# atime = timecxx . . $ time(7f38134db6521d23). (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
288
289 tp = localtimecxx . . $ localtime(9477a1348abbbc5). (&atime);
290 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac). (stream, "%04d-%02d-%02d %02d:%02d:%02d ",
291 1900+tp->tm_yearcxx . . $ tm#tm_year., tp->tm_moncxx . . $ tm#tm_mon.+1, tp->tm_mdaycxx . . $ tm#tm_mday.,
292 tp->tm_hourcxx . . $ tm#tm_hour., tp->tm_mincxx . . $ tm#tm_min., tp->tm_seccxx . . $ tm#tm_sec.);
293 }
294
295 if (handle->flagscxx . . $ log_handle#flags. & LOG_FLAG_WITH_PIDcxx . . $ `src/util/simplelog.h:32:9`!)
296 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac). (stream, "[%u] ", (unsigned int) getpidcxx . . $ getpid(b126dc7c1de90089). ());
297
298 switch (level)
299 {
300 case LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR.:
301 case LOG_LEVEL_FATALcxx . . $ LOG_LEVEL_FATAL.:
302 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac). (stream, "error: ");
303 break;
304
305 case LOG_LEVEL_DEBUGcxx . . $ LOG_LEVEL_DEBUG.:
306 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac). (stream, "debug: ");
307 break;
308
309 case LOG_LEVEL_INFOcxx . . $ LOG_LEVEL_INFO.:
310 break;
311 }
312
313 vfprintfcxx . . $ vfprintf(bded311e54d67f9c). (stream, fmt, ap);
314 putccxx . . $ putc(b1cd03d6c470d1bd). ('\n', stream);
315
316 }
317
318 return err;
319 }
320
321 gpg_error_tcxx . . $ gpg_error_t#
322 log_writecxx . . $ log_write(ad0f2dca42ab2e70). (log_handle_tcxx . . $ log_handle_t# handle, log_level_tcxx . . $ log_level_t# level,
323 const char *fmt, ...)
324 {
325 gpg_error_tcxx . . $ gpg_error_t# err = 0;
326
327 assertcxx . . $ `/usr/include/assert.h:118:11`! (handle);
328
329 if (handle->backendcxx . . $ log_handle#backend. != LOG_BACKEND_NONEcxx . . $ LOG_BACKEND_NONE.)
330 {
331 va_listcxx . . $ va_list# ap;
332
333 va_startcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:50:9`! (ap, fmt);
334 err = internal_log_writecxx . . $ internal_log_write(247b63e26cc1c53c). (handle, level, fmt, ap);
335 va_endcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:52:9`! (ap);
336 }
337
338 return err;
339 }
340
341 gpg_error_tcxx . . $ gpg_error_t#
342 log_write_vacxx . . $ log_write_va(247b63e26cc1c53c). (log_handle_tcxx . . $ log_handle_t# handle, log_level_tcxx . . $ log_level_t# level,
343 const char *fmt, va_listcxx . . $ va_list# ap)
344 {
345 gpg_error_tcxx . . $ gpg_error_t# err = 0;
346
347 assertcxx . . $ `/usr/include/assert.h:118:11`! (handle);
348
349 if (handle->backendcxx . . $ log_handle#backend. != LOG_BACKEND_NONEcxx . . $ LOG_BACKEND_NONE.)
350 err = internal_log_writecxx . . $ internal_log_write(247b63e26cc1c53c). (handle, level, fmt, ap);
351
352 return err;
353 }
354
355 gpg_error_tcxx . . $ gpg_error_t#
356 log_msg_debugcxx . . $ log_msg_debug(628bea2675049132). (log_handle_tcxx . . $ log_handle_t# handle, const char *fmt, ...)
357 {
358 gpg_error_tcxx . . $ gpg_error_t# err;
359 va_listcxx . . $ va_list# ap;
360
361 if (!handle)
362 return 0;
363
364 va_startcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:50:9`! (ap, fmt);
365 err = log_write_vacxx . . $ log_write_va(247b63e26cc1c53c). (handle, LOG_LEVEL_DEBUGcxx . . $ LOG_LEVEL_DEBUG., fmt, ap);
366 va_endcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:52:9`! (ap);
367
368 return err;
369 }
370
371 gpg_error_tcxx . . $ gpg_error_t#
372 log_msg_infocxx . . $ log_msg_info(628bea2675049132). (log_handle_tcxx . . $ log_handle_t# handle, const char *fmt, ...)
373 {
374 gpg_error_tcxx . . $ gpg_error_t# err;
375 va_listcxx . . $ va_list# ap;
376
377 if (!handle)
378 return 0;
379
380 va_startcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:50:9`! (ap, fmt);
381 err = log_write_vacxx . . $ log_write_va(247b63e26cc1c53c). (handle, LOG_LEVEL_INFOcxx . . $ LOG_LEVEL_INFO., fmt, ap);
382 va_endcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:52:9`! (ap);
383
384 return err;
385 }
386
387 gpg_error_tcxx . . $ gpg_error_t#
388 log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (log_handle_tcxx . . $ log_handle_t# handle, const char *fmt, ...)
389 {
390 gpg_error_tcxx . . $ gpg_error_t# err;
391 va_listcxx . . $ va_list# ap;
392
393 if (!handle)
394 return 0;
395
396 va_startcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:50:9`! (ap, fmt);
397 err = log_write_vacxx . . $ log_write_va(247b63e26cc1c53c). (handle, LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., fmt, ap);
398 va_endcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:52:9`! (ap);
399
400 return err;
401 }
402
403 gpg_error_tcxx . . $ gpg_error_t#
404 log_msg_fatalcxx . . $ log_msg_fatal(628bea2675049132). (log_handle_tcxx . . $ log_handle_t# handle, const char *fmt, ...)
405 {
406 gpg_error_tcxx . . $ gpg_error_t# err;
407 va_listcxx . . $ va_list# ap;
408
409 if (!handle)
410 return 0;
411
412 va_startcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:50:9`! (ap, fmt);
413 err = log_write_vacxx . . $ log_write_va(247b63e26cc1c53c). (handle, LOG_LEVEL_FATALcxx . . $ LOG_LEVEL_FATAL., fmt, ap);
414 va_endcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:52:9`! (ap);
415
416 return err;
417 }
418
419 /* END */