1 /*
2 * debug.c: debugging messages
3 *
4 * Copyright (C) 2007 Colin Watson.
5 *
6 * This file is part of man-db.
7 *
8 * man-db is free software; you can redistribute it and/or modify it
9 * 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 * man-db is distributed in the hope that it will be useful, but
14 * 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 man-db; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include <stdbool.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <stdarg.h>
31 #include <string.h>
32 #include <errno.h>
33
34 #include "attribute.h"
35
36 #include "manconfig.h"
37
38 #include "debug.h"
39
40 bool debug_level = false;
41
42 void init_debug (void)
43 {
44 const char *man_debug = getenv ("MAN_DEBUG");
45 if (man_debug && STREQ (man_debug, "1"))
46 debug_level = true;
47 }
48
49 static void ATTRIBUTE_FORMAT ((__printf__, 1, 0)) vdebug (const char *message,
50 va_list args)
51 {
52 if (debug_level)
53 vfprintf (stderr, message, args);
54 }
55
56 void debug (const char *message, ...)
57 {
58 if (debug_level) {
59 va_list args;
60
61 va_start (args, message);
62 vdebug (message, args);
63 va_end (args);
64 }
65 }
66
67 void debug_error (const char *message, ...)
68 {
69 if (debug_level) {
70 va_list args;
71
72 va_start (args, message);
73 vdebug (message, args);
74 va_end (args);
75
76 debug (": %s\n", strerror (errno));
77 }
78 }