1 /* General definitions for recording error and warning status.
2 Copyright (C) 1998-2017 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/>. */
18 #ifndef _RECORD_STATUS_H
19 #define _RECORD_STATUS_H 1
27 /* We tentatively define all of the global data we use:
28 * recorded_warning_count: Number of warnings counted.
29 * recorded_error_count: Number of errors counted.
30 * be_quiet: Should all calls be silent?
31 * verbose: Should verbose messages be printed? */
32 int recorded_warning_count
;
33 int recorded_error_count
;
37 /* Saved locale state. */
40 /* The current in-use locale. */
44 /* Alter the current locale to match the locale configured by the
45 user, and return the previous saved state. */
46 static struct locale_state
55 orig
= setlocale (LC_CTYPE
, NULL
);
57 error (0, 0, "failed to read locale!");
59 if (setlocale (LC_CTYPE
, "") == NULL
)
60 error (0, 0, "failed to set locale!");
67 /* We will return either a valid locale or NULL if we failed
68 to save the locale. */
69 return (struct locale_state
) { .cur_locale
= copy
};
72 /* Use the saved state to restore the locale. */
74 pop_locale (struct locale_state ls
)
76 const char *set
= NULL
;
77 /* We might have failed to save the locale, so only attempt to
78 restore a validly saved non-NULL locale. */
79 if (ls
.cur_locale
!= NULL
)
81 set
= setlocale (LC_CTYPE
, ls
.cur_locale
);
83 error (0, 0, "failed to restore %s locale!", ls
.cur_locale
);
89 /* Wrapper to print verbose informative messages.
90 Verbose messages are only printed if --verbose
91 is in effect and --quiet is not. */
93 __attribute__ ((__format__ (__printf__
, 2, 3), nonnull (1, 2), unused
))
94 record_verbose (FILE *stream
, const char *format
, ...)
104 struct locale_state ls
;
107 va_start (arg
, format
);
110 ret
= vasprintf (&str
, format
, arg
);
117 fprintf (stream
, "%s\n", str
);
123 /* Wrapper to print warning messages. We keep track of how
124 many were called because this effects our exit code.
125 Nothing is printed if --quiet is in effect, but warnings
126 are always counted. */
128 __attribute__ ((__format__ (__printf__
, 1, 2), nonnull (1), unused
))
129 record_warning (const char *format
, ...)
134 recorded_warning_count
++;
138 struct locale_state ls
;
141 va_start (arg
, format
);
144 ret
= vasprintf (&str
, format
, arg
);
151 fprintf (stderr
, "%s\n", str
);
157 /* Wrapper to print error messages. We keep track of how
158 many were called because this effects our exit code.
159 Nothing is printed if --quiet is in effect, but errors
160 are always counted, and fatal errors always exit the
163 __attribute__ ((__format__ (__printf__
, 3, 4), nonnull (3), unused
))
164 record_error (int status
, int errnum
, const char *format
, ...)
169 recorded_error_count
++;
171 /* The existing behaviour is that even if you use --quiet, a fatal
172 error is always printed and terminates the process. */
173 if (!be_quiet
|| status
!= 0)
175 struct locale_state ls
;
178 va_start (arg
, format
);
181 ret
= vasprintf (&str
, format
, arg
);
188 error (status
, errnum
, "%s", str
);
193 /* ... likewise for error_at_line. */
195 __attribute__ ((__format__ (__printf__
, 5, 6), nonnull (3, 5), unused
))
196 record_error_at_line (int status
, int errnum
, const char *filename
,
197 unsigned int linenum
, const char *format
, ...)
202 recorded_error_count
++;
204 /* The existing behaviour is that even if you use --quiet, a fatal
205 error is always printed and terminates the process. */
206 if (!be_quiet
|| status
!= 0)
208 struct locale_state ls
;
211 va_start (arg
, format
);
214 ret
= vasprintf (&str
, format
, arg
);
221 error_at_line (status
, errnum
, filename
, linenum
, "%s", str
);