1 /* Implementation of error and warning reporting utilities.
2 Copyright 2002 Paul Twohey.
3
4 This file is part of VMIPS.
5
6 VMIPS is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2 of the License, or (at your
9 option) any later version.
10
11 VMIPS is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License along
17 with VMIPS; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19
20 #include <cstdio>
21 #include <cstdarg>
22 #include <cassert>
23 #include <cstdlib>
24 #include "error.h"
25
26 /* Print PRE literally (if non-NULL), then FMT with printf style
27 * substitutions from AP and then POST literally (if non-NULL) to file
28 * FILE. Then flush the file.
29 */
format_help(FILE * file,const char * pre,const char * post,const char * fmt,va_list ap)30 static void format_help(FILE *file, const char *pre, const char *post,
31 const char *fmt, va_list ap)
32 {
33 assert(file);
34 assert(fmt);
35
36 if (pre)
37 fputs(pre, file);
38
39 vfprintf(file, fmt, ap);
40
41 if (post)
42 fputs(post, file);
43
44 fflush(file);
45 }
46
error(const char * msg,...)47 void error(const char *msg, ...)
48 {
49 va_list ap;
50 va_start(ap, msg);
51 format_help(stderr, "Error: ", "\n", msg, ap);
52 va_end(ap);
53 }
54
error_exit(const char * msg,...)55 void error_exit(const char *msg, ...)
56 {
57 va_list ap;
58 va_start(ap, msg);
59 format_help(stderr, "Error: ", "\n", msg, ap);
60 va_end(ap);
61
62 exit(1);
63 }
64
fatal_error(const char * msg,...)65 void fatal_error(const char *msg, ...)
66 {
67 va_list ap;
68 va_start(ap, msg);
69 format_help(stderr, "Fatal Error: ", "\n", msg, ap);
70 va_end(ap);
71
72 abort();
73 }
74
warning(const char * msg,...)75 void warning(const char *msg, ...)
76 {
77 va_list ap;
78 va_start(ap, msg);
79 format_help(stderr, "Warning: ", "\n", msg, ap);
80 va_end(ap);
81 }
82