po: Update German man pages translation
[dpkg.git] / lib / dpkg / debug.c
blob115d455459b10a53a83f63489a778a18a8d9414c
1 /*
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/>.
22 #include <config.h>
23 #include <compat.h>
25 #include <errno.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <stdio.h>
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;
38 /**
39 * Set the debugging output file.
41 * Marks the file descriptor as close-on-exec.
43 void
44 debug_set_output(FILE *output, const char *filename)
46 setcloexec(fileno(output), filename);
47 dpkg_set_report_buffer(output);
48 debug_output = output;
51 /**
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.
57 void
58 debug_set_mask(int mask)
60 debug_mask = mask;
61 if (!debug_output)
62 debug_output = stderr;
65 /**
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.
71 int
72 debug_parse_mask(const char *str)
74 char *endp;
75 long mask;
77 errno = 0;
78 mask = strtol(str, &endp, 8);
79 if (str == endp || *endp || mask < 0 || errno == ERANGE)
80 return -1;
82 debug_set_mask(mask);
84 return mask;
87 /**
88 * Check if a debugging flag is currently set on the debugging mask.
90 bool
91 debug_has_flag(int flag)
93 return debug_mask & flag;
96 /**
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.
102 void
103 debug(int flag, const char *fmt, ...)
105 va_list args;
107 if (!debug_has_flag(flag))
108 return;
110 fprintf(debug_output, "D0%05o: ", flag);
111 va_start(args, fmt);
112 vfprintf(debug_output, fmt, args);
113 va_end(args);
114 putc('\n', debug_output);
118 * Initialize the debugging support.
120 void
121 dpkg_debug_init(void)
123 const char envvar[] = "DPKG_DEBUG";
124 const char *env;
126 env = getenv(envvar);
127 if (env == NULL)
128 return;
130 if (debug_parse_mask(env) < 0)
131 warning(_("cannot parse debug mask from environment variable %s"),
132 envvar);