Commit | Line | Data |
---|---|---|
1479465f GJ |
1 | /* |
2 | * libdpkg - Debian packaging suite library routines | |
3 | * debug.c - debugging support | |
4 | * | |
5 | * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk> | |
6 | * Copyright © 2011 Guillem Jover <guillem@debian.org> | |
7 | * | |
8 | * This is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
22 | #include <config.h> | |
23 | #include <compat.h> | |
24 | ||
25 | #include <stdarg.h> | |
26 | #include <stdio.h> | |
27 | ||
28 | #include <dpkg/dpkg.h> | |
29 | #include <dpkg/report.h> | |
30 | #include <dpkg/debug.h> | |
31 | ||
32 | static int debug_mask = 0; | |
33 | static FILE *debug_output = NULL; | |
34 | ||
35 | /** | |
36 | * Set the debugging output file. | |
37 | * | |
38 | * Marks the file descriptor as close-on-exec. | |
39 | */ | |
40 | void | |
41 | debug_set_output(FILE *output, const char *filename) | |
42 | { | |
43 | setcloexec(fileno(output), filename); | |
44 | dpkg_set_report_buffer(output); | |
45 | debug_output = output; | |
46 | } | |
47 | ||
48 | /** | |
49 | * Set the debugging mask. | |
50 | * | |
51 | * The mask determines what debugging flags are going to take effect at | |
52 | * run-time. The output will be set to stderr if it has not been set before. | |
53 | */ | |
54 | void | |
55 | debug_set_mask(int mask) | |
56 | { | |
57 | debug_mask = mask; | |
58 | if (!debug_output) | |
59 | debug_output = stderr; | |
60 | } | |
61 | ||
62 | /** | |
63 | * Check if a debugging flag is currently set on the debugging mask. | |
64 | */ | |
65 | bool | |
66 | debug_has_flag(int flag) | |
67 | { | |
68 | return debug_mask & flag; | |
69 | } | |
70 | ||
71 | /** | |
72 | * Output a debugging message. | |
73 | * | |
74 | * The message will be printed to the previously specified output if the | |
75 | * specified flag is present in the current debugging mask. | |
76 | */ | |
77 | void | |
78 | debug(int flag, const char *fmt, ...) | |
79 | { | |
80 | va_list args; | |
81 | ||
82 | if (!debug_has_flag(flag)) | |
83 | return; | |
84 | ||
85 | fprintf(debug_output, "D0%05o: ", flag); | |
86 | va_start(args, fmt); | |
87 | vfprintf(debug_output, fmt, args); | |
88 | va_end(args); | |
89 | putc('\n', debug_output); | |
90 | } |