flags: Change naming of functions to be clearer
[gem5.git] / src / base / misc.hh
1 /*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 * Dave Greene
30 */
31
32 #ifndef __BASE_MISC_HH__
33 #define __BASE_MISC_HH__
34
35 #include "base/compiler.hh"
36 #include "base/cprintf.hh"
37 #include "base/varargs.hh"
38
39 #if defined(__SUNPRO_CC)
40 #define __FUNCTION__ "how to fix me?"
41 #endif
42
43 // General exit message, these functions will never return and will
44 // either abort() if code is < 0 or exit with the code if >= 0
45 void __exit_message(const char *prefix, int code,
46 const char *func, const char *file, int line,
47 const char *format, CPRINTF_DECLARATION) M5_ATTR_NORETURN;
48
49 void __exit_message(const char *prefix, int code,
50 const char *func, const char *file, int line,
51 const std::string &format, CPRINTF_DECLARATION) M5_ATTR_NORETURN;
52
53 inline void
54 __exit_message(const char *prefix, int code,
55 const char *func, const char *file, int line,
56 const std::string& format, CPRINTF_DEFINITION)
57 {
58 __exit_message(prefix, code, func, file, line, format.c_str(),
59 VARARGS_ALLARGS);
60 }
61
62 M5_PRAGMA_NORETURN(__exit_message)
63 #define exit_message(prefix, code, ...) \
64 __exit_message(prefix, code, __FUNCTION__, __FILE__, __LINE__, \
65 __VA_ARGS__)
66
67 //
68 // This implements a cprintf based panic() function. panic() should
69 // be called when something happens that should never ever happen
70 // regardless of what the user does (i.e., an acutal m5 bug). panic()
71 // calls abort which can dump core or enter the debugger.
72 //
73 //
74 #define panic(...) exit_message("panic", -1, __VA_ARGS__)
75
76 //
77 // This implements a cprintf based fatal() function. fatal() should
78 // be called when the simulation cannot continue due to some condition
79 // that is the user's fault (bad configuration, invalid arguments,
80 // etc.) and not a simulator bug. fatal() calls exit(1), i.e., a
81 // "normal" exit with an error code, as opposed to abort() like
82 // panic() does.
83 //
84 #define fatal(...) exit_message("fatal", 1, __VA_ARGS__)
85
86 void
87 __base_message(std::ostream &stream, const char *prefix, bool verbose,
88 const char *func, const char *file, int line,
89 const char *format, CPRINTF_DECLARATION);
90
91 inline void
92 __base_message(std::ostream &stream, const char *prefix, bool verbose,
93 const char *func, const char *file, int line,
94 const std::string &format, CPRINTF_DECLARATION)
95 {
96 __base_message(stream, prefix, verbose, func, file, line, format.c_str(),
97 VARARGS_ALLARGS);
98 }
99
100 #define base_message(stream, prefix, verbose, ...) \
101 __base_message(stream, prefix, verbose, __FUNCTION__, __FILE__, __LINE__, \
102 __VA_ARGS__)
103
104 // Only print the message the first time this expression is
105 // encountered. i.e. This doesn't check the string itself and
106 // prevent duplicate strings, this prevents the statement from
107 // happening more than once. So, even if the arguments change and that
108 // would have resulted in a different message thoes messages would be
109 // supressed.
110 #define base_message_once(...) do { \
111 static bool once = false; \
112 if (!once) { \
113 base_message(__VA_ARGS__); \
114 once = true; \
115 } \
116 } while (0)
117
118 #define cond_message(cond, ...) do { \
119 if (cond) \
120 base_message(__VA_ARGS__); \
121 } while (0)
122
123 #define cond_message_once(cond, ...) do { \
124 static bool once = false; \
125 if (!once && cond) { \
126 base_message(__VA_ARGS__); \
127 once = true; \
128 } \
129 } while (0)
130
131
132 extern bool want_warn, warn_verbose;
133 extern bool want_info, info_verbose;
134 extern bool want_hack, hack_verbose;
135
136 #define warn(...) \
137 cond_message(want_warn, std::cerr, "warn", warn_verbose, __VA_ARGS__)
138 #define inform(...) \
139 cond_message(want_info, std::cout, "info", info_verbose, __VA_ARGS__)
140 #define hack(...) \
141 cond_message(want_hack, std::cerr, "hack", hack_verbose, __VA_ARGS__)
142
143 #define warn_once(...) \
144 cond_message_once(want_warn, std::cerr, "warn", warn_verbose, __VA_ARGS__)
145 #define inform_once(...) \
146 cond_message_once(want_info, std::cout, "info", info_verbose, __VA_ARGS__)
147 #define hack_once(...) \
148 cond_message_once(want_hack, std::cerr, "hack", hack_verbose, __VA_ARGS__)
149
150 #endif // __BASE_MISC_HH__