Debian Codegraph @

lib/msg.h

raw
1 /* dctrl-tools - Debian control file inspection tools
2 Copyright © 1999, 2008, 2011 Antti-Juhani Kaijanaho
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #ifndef MSG_H__
20 #define MSG_H__
21
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "i18n.h"
29
30 static void fail(void) __attribute__((noreturn));
31
32 static inline
33 void fail(void) { exit(2); }
34
35 /* log levels */
36 #define L_FATAL 3
37 #define L_IMPORTANT 2
38 #define L_INFORMATIONAL 1
39 #define L_DEBUG 0
40
41 #if !defined(NDEBUG) && !defined(TEST_NODEBUG) && defined(ENABLE_L_DEBUG)
42 # define INCLUDE_DEBUG_MSGS
43 #endif
44
45 inline static int
46 do_msg(int severity)
47 {
48 extern int loglevel;
49
50 #if defined(TEST_NODEBUG)
51 if (severity == L_DEBUG) {
52 message (L_FATAL, _("I'm broken - please report this bug."), 0, 0);
53 abort ();
54 }
55 #endif
56
57 return severity >= loglevel;
58 }
59
60 inline static char const *
61 get_progname(void)
62 {
63 extern char progname [];
64 return progname;
65 }
66
67 static inline
68 void record_error(void)
69 {
70 extern bool errors;
71 errors = 1;
72 }
73
74 void
75 msg_primitive(const char *fname, int line, const char *fmt, va_list ap);
76
77 static void
78 line_message(int severity, const char *fname, int line, const char *fmt, ...)
79 __attribute__((format(printf,4,5)));
80
81 inline static void
82 line_message(int severity, const char *fname, int line, const char *fmt, ...)
83 {
84 if (do_msg(severity)) {
85 va_list ap;
86 va_start(ap, fmt);
87 msg_primitive(fname, line, fmt, ap);
88 va_end(ap);
89 if (severity >= L_IMPORTANT) record_error();
90 }
91 }
92
93 static void
94 message (int severity, const char * fname, const char * s, ...)
95 __attribute__((format(printf,3,4)));
96
97 inline static void
98 message (int severity, const char * fname, const char * fmt, ...)
99 {
100 if (do_msg(severity)) {
101 va_list ap;
102 va_start(ap, fmt);
103 msg_primitive(fname, 0, fmt, ap);
104 va_end(ap);
105 if (severity >= L_IMPORTANT) record_error();
106 }
107 }
108
109 #ifndef MSG_C__
110 #undef PROGNAME_MAXLEN
111 #endif
112
113 inline static void
114 debug_message (const char * s, const char * fname)
115 {
116 #ifdef INCLUDE_DEBUG_MSGS
117 message (L_DEBUG, fname, "%s", s);
118 #endif
119 }
120
121 static void
122 debug(const char * s, ...) __attribute__((format(printf, 1, 2)));
123
124 inline static void
125 debug(const char * s, ...)
126 {
127 #ifdef INCLUDE_DEBUG_MSGS
128 if (do_msg(L_DEBUG)) {
129 va_list va;
130 va_start(va, s);
131 fprintf(stderr, "%s: ", get_progname());
132 vfprintf(stderr, s, va);
133 fprintf(stderr, "\n");
134 va_end(va);
135 }
136 #endif
137 }
138
139 inline static void
140 errno_msg(int severity, char const * fname)
141 {
142 message(severity, fname, "%s", strerror(errno));
143 }
144
145 #define enomem_msg _("cannot find enough memory")
146
147 inline static void
148 enomem (const char * fname)
149 {
150 message (L_IMPORTANT, fname, enomem_msg);
151 }
152
153 inline static void
154 fatal_enomem (const char * fname)
155 {
156 message(L_FATAL, fname, enomem_msg);
157 fail();
158 }
159
160 #undef enomem_msg
161
162 /* Given a string that represents a log level, return the number that
163 repsesents that loglevel. A negative return value means that the
164 string is not a valid log level. */
165 int
166 str2loglevel (const char * s);
167
168 /* Set current log level to ll. */
169 void
170 set_loglevel (int ll);
171
172 /* Set program name to pn. */
173 void
174 msg_set_progname (const char * pn);
175
176 /* Return true iff loglevels IMPORTANT or FATAL have been used. */
177 static inline
178 bool errors_reported(void)
179 {
180 extern bool errors;
181 return errors;
182 }
183
184 #endif /* MSG_H__ */