2 * libdpkg - Debian packaging suite library routines
3 * debug.c - debugging support
5 * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2011 Guillem Jover <guillem@debian.org>
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.
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.
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/>.
30 #include <dpkg/dpkg.h>
31 #include <dpkg/i18n.h>
32 #include <dpkg/report.h>
33 #include <dpkg/debug.h>
35 static int debug_mask
= 0;
36 static FILE *debug_output
= NULL
;
39 * Set the debugging output file.
41 * Marks the file descriptor as close-on-exec.
44 debug_set_output(FILE *output
, const char *filename
)
46 setcloexec(fileno(output
), filename
);
47 dpkg_set_report_buffer(output
);
48 debug_output
= output
;
52 * Set the debugging mask.
54 * The mask determines what debugging flags are going to take effect at
55 * run-time. The output will be set to stderr if it has not been set before.
58 debug_set_mask(int mask
)
62 debug_output
= stderr
;
66 * Parse the debugging mask.
68 * The mask is parsed from the specified string and sets the global debugging
69 * mask. If there is any error while parsing a negative number is returned.
72 debug_parse_mask(const char *str
)
78 mask
= strtol(str
, &endp
, 8);
79 if (str
== endp
|| *endp
|| mask
< 0 || errno
== ERANGE
)
88 * Check if a debugging flag is currently set on the debugging mask.
91 debug_has_flag(int flag
)
93 return debug_mask
& flag
;
97 * Output a debugging message.
99 * The message will be printed to the previously specified output if the
100 * specified flag is present in the current debugging mask.
103 debug(int flag
, const char *fmt
, ...)
107 if (!debug_has_flag(flag
))
110 fprintf(debug_output
, "D0%05o: ", flag
);
112 vfprintf(debug_output
, fmt
, args
);
114 putc('\n', debug_output
);
118 * Initialize the debugging support.
121 dpkg_debug_init(void)
123 const char envvar
[] = "DPKG_DEBUG";
126 env
= getenv(envvar
);
130 if (debug_parse_mask(env
) < 0)
131 warning(_("cannot parse debug mask from environment variable %s"),