gem5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
misc.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, 2017 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Copyright (c) 2002-2005 The Regents of The University of Michigan
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions are
19  * met: redistributions of source code must retain the above copyright
20  * notice, this list of conditions and the following disclaimer;
21  * redistributions in binary form must reproduce the above copyright
22  * notice, this list of conditions and the following disclaimer in the
23  * documentation and/or other materials provided with the distribution;
24  * neither the name of the copyright holders nor the names of its
25  * contributors may be used to endorse or promote products derived from
26  * this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  *
40  * Authors: Nathan Binkert
41  * Dave Greene
42  * Andreas Sandberg
43  */
44 
45 #ifndef __BASE_MISC_HH__
46 #define __BASE_MISC_HH__
47 
48 #include <cassert>
49 #include <cstdlib>
50 #include <iostream>
51 #include <utility>
52 
53 #include "base/compiler.hh"
54 #include "base/cprintf.hh"
55 
56 #if defined(__SUNPRO_CC)
57 #define __FUNCTION__ "how to fix me?"
58 #endif
59 
60 class Logger
61 {
62  public:
63  enum LogLevel {
64  PANIC = 0,
70  };
71 
80  static void setLevel(LogLevel ll);
81 
88  static Logger &get(LogLevel ll);
89 
90  public:
91  Logger(std::ostream &stream, const char *prefix);
92  virtual ~Logger() {};
93 
94  template<typename ...Args> void
95  print(const char *func, const char *file, int line,
96  const char *format, const Args &...args)
97  {
98  if (!enabled)
99  return;
100 
101  if (prefix)
102  stream << prefix << ": ";
103  ccprintf(stream, format, args...);
104 
105  printEpilogue(func, file, line, format);
106  }
107 
108  template<typename ...Args> void
109  print(const char *func, const char *file, int line,
110  const std::string &format, const Args &...args)
111  {
112  print(func, file, line, format.c_str(), args...);
113  }
114 
115  protected:
116  virtual void printEpilogue(const char *func, const char *file, int line,
117  const char *format);
118 
119  public:
120  bool enabled;
121  bool verbose;
122 
123  protected:
124  std::ostream &stream;
125  const char *prefix;
126 };
127 
128 class ExitLogger : public Logger
129 {
130  public:
131  using Logger::Logger;
132 
133  void printEpilogue(const char *func, const char *file, int line,
134  const char *format) override;
135 };
136 
137 #define exit_message(logger, code, ...) \
138  do { \
139  logger.print(__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
140  if (code < 0) \
141  ::abort(); \
142  else \
143  ::exit(code); \
144  } while (0)
145 
146 //
147 // This implements a cprintf based panic() function. panic() should
148 // be called when something happens that should never ever happen
149 // regardless of what the user does (i.e., an acutal m5 bug). panic()
150 // calls abort which can dump core or enter the debugger.
151 //
152 //
153 #define panic(...) exit_message(::Logger::get(::Logger::PANIC), -1, \
154  __VA_ARGS__)
155 
156 //
157 // This implements a cprintf based fatal() function. fatal() should
158 // be called when the simulation cannot continue due to some condition
159 // that is the user's fault (bad configuration, invalid arguments,
160 // etc.) and not a simulator bug. fatal() calls abort() like
161 // panic() does.
162 //
163 #define fatal(...) exit_message(::Logger::get(::Logger::FATAL), 1, \
164  __VA_ARGS__)
165 
174 #define panic_if(cond, ...) \
175  do { \
176  if ((cond)) { \
177  panic("panic condition " # cond " occurred: %s", \
178  csprintf(__VA_ARGS__)); \
179  } \
180  } while (0)
181 
182 
192 #define fatal_if(cond, ...) \
193  do { \
194  if ((cond)) { \
195  fatal("fatal condition " # cond " occurred: %s", \
196  csprintf(__VA_ARGS__)); \
197  } \
198  } while (0)
199 
200 
201 #define base_message(logger, ...) \
202  logger.print(__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__)
203 
204 // Only print the message the first time this expression is
205 // encountered. i.e. This doesn't check the string itself and
206 // prevent duplicate strings, this prevents the statement from
207 // happening more than once. So, even if the arguments change and that
208 // would have resulted in a different message thoes messages would be
209 // supressed.
210 #define base_message_once(...) do { \
211  static bool once = false; \
212  if (!once) { \
213  base_message(__VA_ARGS__); \
214  once = true; \
215  } \
216  } while (0)
217 
218 
219 #define warn(...) \
220  base_message(::Logger::get(::Logger::WARN), __VA_ARGS__)
221 #define inform(...) \
222  base_message(::Logger::get(::Logger::INFO), __VA_ARGS__)
223 #define hack(...) \
224  base_message(::Logger::get(::Logger::HACK), __VA_ARGS__)
225 
226 #define warn_once(...) \
227  base_message_once(::Logger::get(::Logger::WARN), __VA_ARGS__)
228 #define inform_once(...) \
229  base_message_once(::Logger::get(::Logger::INFO), __VA_ARGS__)
230 #define hack_once(...) \
231  base_message_once(::Logger::get(::Logger::HACK), __VA_ARGS__)
232 
241 #define warn_if(cond, ...) \
242  do { \
243  if ((cond)) \
244  warn(__VA_ARGS__); \
245  } while (0)
246 
256 #ifdef NDEBUG
257 #define chatty_assert(cond, ...)
258 #else
259 #define chatty_assert(cond, ...) \
260  do { \
261  if (!(cond)) \
262  panic("assert(" # cond ") failed: %s", csprintf(__VA_ARGS__)); \
263  } while (0)
264 #endif // NDEBUG
265 #endif // __BASE_MISC_HH__
Definition: misc.hh:60
void ccprintf(cp::Print &print)
Definition: cprintf.hh:130
const char * prefix
Definition: misc.hh:125
void print(const char *func, const char *file, int line, const std::string &format, const Args &...args)
Definition: misc.hh:109
virtual ~Logger()
Definition: misc.hh:92
bool enabled
Definition: misc.hh:120
LogLevel
Definition: misc.hh:63
void printEpilogue(const char *func, const char *file, int line, const char *format) override
Definition: misc.cc:108
void print(const char *func, const char *file, int line, const char *format, const Args &...args)
Definition: misc.hh:95
virtual void printEpilogue(const char *func, const char *file, int line, const char *format)
Definition: misc.cc:96
Logger(std::ostream &stream, const char *prefix)
Definition: misc.cc:90
std::ostream & stream
Definition: misc.hh:124
bool verbose
Definition: misc.hh:121
Bitfield< 31, 29 > format
Definition: miscregs.hh:1819
static void setLevel(LogLevel ll)
Set the active log level.
Definition: misc.cc:70

Generated on Fri Jun 9 2017 13:03:38 for gem5 by doxygen 1.8.6