1 /* Functions for recorded errors, warnings, and verbose messages.
2 Copyright (C) 1998-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>. */
27 #include "record-status.h"
29 /* Warnings recorded by record_warnings. */
30 int recorded_warning_count
;
32 /* Errors recorded by record_errors. */
33 int recorded_error_count
;
35 /* If not zero suppress warnings and information messages. */
38 /* If not zero give a lot more messages. */
41 /* Warnings which can be disabled: */
42 /* By default we check the character map for ASCII compatibility. */
43 bool warn_ascii
= true;
44 /* By default we check that the international currency symbol matches a
45 known country code. */
46 bool warn_int_curr_symbol
= true;
48 /* Alter the current locale to match the locale configured by the
49 user, and return the previous saved state. */
59 orig
= setlocale (LC_CTYPE
, NULL
);
61 error (0, 0, "failed to read locale!");
63 if (setlocale (LC_CTYPE
, "") == NULL
)
64 error (0, 0, "failed to set locale!");
71 /* We will return either a valid locale or NULL if we failed
72 to save the locale. */
73 return (struct locale_state
) { .cur_locale
= copy
};
76 /* Use the saved state to restore the locale. */
78 pop_locale (struct locale_state ls
)
80 const char *set
= NULL
;
81 /* We might have failed to save the locale, so only attempt to
82 restore a validly saved non-NULL locale. */
83 if (ls
.cur_locale
!= NULL
)
85 set
= setlocale (LC_CTYPE
, ls
.cur_locale
);
87 error (0, 0, "failed to restore %s locale!", ls
.cur_locale
);
93 /* Wrapper to print verbose informative messages.
94 Verbose messages are only printed if --verbose
95 is in effect and --quiet is not. */
97 __attribute__ ((__format__ (__printf__
, 2, 3), nonnull (1, 2), unused
))
98 record_verbose (FILE *stream
, const char *format
, ...)
108 struct locale_state ls
;
111 va_start (arg
, format
);
114 ret
= vasprintf (&str
, format
, arg
);
121 fprintf (stream
, "[verbose] %s\n", str
);
127 /* Wrapper to print warning messages. We keep track of how
128 many were called because this effects our exit code.
129 Nothing is printed if --quiet is in effect, but warnings
130 are always counted. */
132 __attribute__ ((__format__ (__printf__
, 1, 2), nonnull (1), unused
))
133 record_warning (const char *format
, ...)
138 recorded_warning_count
++;
142 struct locale_state ls
;
145 va_start (arg
, format
);
148 ret
= vasprintf (&str
, format
, arg
);
155 fprintf (stderr
, "[warning] %s\n", str
);
161 /* Wrapper to print error messages. We keep track of how
162 many were called because this effects our exit code.
163 Nothing is printed if --quiet is in effect, but errors
164 are always counted, and fatal errors always exit the
167 __attribute__ ((__format__ (__printf__
, 3, 4), nonnull (3), unused
))
168 record_error (int status
, int errnum
, const char *format
, ...)
173 recorded_error_count
++;
175 /* The existing behaviour is that even if you use --quiet, a fatal
176 error is always printed and terminates the process. */
177 if (!be_quiet
|| status
!= 0)
179 struct locale_state ls
;
182 va_start (arg
, format
);
185 ret
= vasprintf (&str
, format
, arg
);
192 error (status
, errnum
, "[error] %s", str
);
197 /* ... likewise for error_at_line. */
199 __attribute__ ((__format__ (__printf__
, 5, 6), nonnull (3, 5), unused
))
200 record_error_at_line (int status
, int errnum
, const char *filename
,
201 unsigned int linenum
, const char *format
, ...)
206 recorded_error_count
++;
208 /* The existing behaviour is that even if you use --quiet, a fatal
209 error is always printed and terminates the process. */
210 if (!be_quiet
|| status
!= 0)
212 struct locale_state ls
;
215 va_start (arg
, format
);
218 ret
= vasprintf (&str
, format
, arg
);
225 error_at_line (status
, errnum
, filename
, linenum
, "[error] %s", str
);