Debian Codegraph @

lib/msg.c

raw
1 /* grep-dctrl - grep Debian control files
2 Copyright © 1999, 2004, 2008, 2012 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
20
21 #define MSG_C__
22
23 #include <stdbool.h>
24
25 #include <assert.h>
26 #include <string.h>
27 #include "msg.h"
28
29 #define PROGNAME_MAXLEN 64
30
31 bool errors = false;
32
33 int loglevel = L_IMPORTANT;
34 char progname [PROGNAME_MAXLEN];
35
36 struct str2int_avec_t {
37 const char * str;
38 int integer;
39 };
40
41 int
42 within_interval (int n, int a, int b)
43 {
44 if (a > b)
45 {
46 int tmp;
47 tmp = b;
48 b = a;
49 a = tmp;
50 }
51
52 assert (a <= b);
53
54 return (a <= n && n <= b);
55 }
56
57 void
58 msg_set_progname (const char * pn)
59 {
60 strncpy (progname, pn, PROGNAME_MAXLEN);
61 progname [PROGNAME_MAXLEN - 1] = 0;
62 }
63
64 void
65 set_loglevel (int ll)
66 {
67 assert (within_interval (loglevel, L_FATAL, L_DEBUG));
68
69 loglevel = ll;
70 }
71
72 int
73 str2loglevel (const char * s)
74 {
75 static struct str2int_avec_t avec [] = {
76 { "fatal", L_FATAL },
77 { "important", L_IMPORTANT },
78 { "informational", L_INFORMATIONAL },
79 #if !defined(NDEBUG) && defined(ENABLE_L_DEBUG)
80 { "debug", L_DEBUG },
81 #endif
82 { 0, 0 } };
83
84 int i;
85
86 for (i = 0; avec [i].str != 0; i++)
87 if (strcasecmp (avec [i].str, s) == 0)
88 return avec[i].integer;
89 return -1;
90 }
91
92 void
93 msg_primitive(const char *fname, int line, const char *fmt, va_list ap)
94 {
95 if (fname == 0) {
96 fprintf(stderr, "%s: ", get_progname());
97 } else if (line > 0) {
98 fprintf(stderr, "%s: %s:%i: ", get_progname(), fname, line);
99 } else {
100 fprintf(stderr, "%s: %s: ", get_progname(), fname);
101 }
102 vfprintf(stderr, fmt, ap);
103 fputs(".\n", stderr);
104 }