gem5  v20.1.0.0
logging.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, 2017, 2019 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 
41 #ifndef __BASE_LOGGING_HH__
42 #define __BASE_LOGGING_HH__
43 
44 #include <cassert>
45 #include <sstream>
46 #include <utility>
47 
48 #include "base/compiler.hh"
49 #include "base/cprintf.hh"
50 
51 class Logger
52 {
53  public:
54 
58  static Logger &getPanic();
59  static Logger &getFatal();
60  static Logger &getWarn();
61  static Logger &getInfo();
62  static Logger &getHack();
63 
64  enum LogLevel {
67  };
68 
69  static void
71  {
72  getPanic().enabled = (ll >= PANIC);
73  getFatal().enabled = (ll >= FATAL);
74  getWarn().enabled = (ll >= WARN);
75  getInfo().enabled = (ll >= INFO);
76  getHack().enabled = (ll >= HACK);
77  }
78 
79  struct Loc
80  {
81  Loc(const char *file, int line) : file(file), line(line) {}
82  const char *file;
83  int line;
84  };
85 
86  Logger(const char *prefix) : enabled(true), prefix(prefix)
87  {
88  assert(prefix);
89  }
90 
91  virtual ~Logger() {};
92 
93  void
94  print(const Loc &loc, const std::string &str)
95  {
96  std::stringstream ss;
97  ss << prefix << str;
98  if (str.length() && str.back() != '\n' && str.back() != '\r')
99  ss << std::endl;
100  if (!enabled)
101  return;
102  log(loc, ss.str());
103  }
104 
105  template<typename ...Args> void
106  print(const Loc &loc, const char *format, const Args &...args)
107  {
108  std::stringstream ss;
109  ccprintf(ss, format, args...);
110  print(loc, ss.str());
111  }
112 
113  template<typename ...Args> void
114  print(const Loc &loc, const std::string &format, const Args &...args)
115  {
116  print(loc, format.c_str(), args...);
117  }
118 
124  void exit_helper() M5_ATTR_NORETURN { exit(); ::abort(); }
125 
126  protected:
127  bool enabled;
128 
129  virtual void log(const Loc &loc, std::string s) = 0;
130  virtual void exit() { /* Fall through to the abort in exit_helper. */ }
131 
132  const char *prefix;
133 };
134 
135 
136 #define base_message(logger, ...) \
137  logger.print(::Logger::Loc(__FILE__, __LINE__), __VA_ARGS__)
138 
139 /*
140  * Only print the message the first time this expression is
141  * encountered. i.e. This doesn't check the string itself and
142  * prevent duplicate strings, this prevents the statement from
143  * happening more than once. So, even if the arguments change and that
144  * would have resulted in a different message thoes messages would be
145  * supressed.
146  */
147 #define base_message_once(...) do { \
148  static bool once = false; \
149  if (!once) { \
150  base_message(__VA_ARGS__); \
151  once = true; \
152  } \
153  } while (0)
154 
155 #define exit_message(logger, ...) \
156  do { \
157  base_message(logger, __VA_ARGS__); \
158  logger.exit_helper(); \
159  } while (0)
160 
171 #define panic(...) exit_message(::Logger::getPanic(), __VA_ARGS__)
172 
183 #define fatal(...) exit_message(::Logger::getFatal(), __VA_ARGS__)
184 
197 #define panic_if(cond, ...) \
198  do { \
199  if ((cond)) { \
200  panic("panic condition " # cond " occurred: %s", \
201  csprintf(__VA_ARGS__)); \
202  } \
203  } while (0)
204 
205 
219 #define fatal_if(cond, ...) \
220  do { \
221  if ((cond)) { \
222  fatal("fatal condition " # cond " occurred: %s", \
223  csprintf(__VA_ARGS__)); \
224  } \
225  } while (0)
226 
227 
239 #define warn(...) base_message(::Logger::getWarn(), __VA_ARGS__)
240 #define inform(...) base_message(::Logger::getInfo(), __VA_ARGS__)
241 #define hack(...) base_message(::Logger::getHack(), __VA_ARGS__)
242 
243 #define warn_once(...) base_message_once(::Logger::getWarn(), __VA_ARGS__)
244 #define inform_once(...) base_message_once(::Logger::getInfo(), __VA_ARGS__)
245 #define hack_once(...) base_message_once(::Logger::getHack(), __VA_ARGS__)
246  // end of api_logger
247 
263 #define warn_if(cond, ...) \
264  do { \
265  if ((cond)) \
266  warn(__VA_ARGS__); \
267  } while (0)
268 
269 #define warn_if_once(cond, ...) \
270  do { \
271  if ((cond)) \
272  warn_once(__VA_ARGS__); \
273  } while (0)
274  // end of api_logger
275 
289 #ifdef NDEBUG
290 #define chatty_assert(cond, ...)
291 #else
292 #define chatty_assert(cond, ...) \
293  do { \
294  if (!(cond)) \
295  panic("assert(" # cond ") failed: %s", csprintf(__VA_ARGS__)); \
296  } while (0)
297 #endif // NDEBUG
298  // end of api_logger
299 #endif // __BASE_LOGGING_HH__
Logger::exit_helper
void exit_helper() M5_ATTR_NORETURN
This helper is necessary since noreturn isn't inherited by virtual functions, and gcc will get mad if...
Definition: logging.hh:124
Logger::NUM_LOG_LEVELS
@ NUM_LOG_LEVELS
Definition: logging.hh:66
Logger::INFO
@ INFO
Definition: logging.hh:65
Logger::~Logger
virtual ~Logger()
Definition: logging.hh:91
Logger::log
virtual void log(const Loc &loc, std::string s)=0
Logger::prefix
const char * prefix
Definition: logging.hh:132
Logger::Logger
Logger(const char *prefix)
Definition: logging.hh:86
Logger::Loc::Loc
Loc(const char *file, int line)
Definition: logging.hh:81
Logger::setLevel
static void setLevel(LogLevel ll)
Definition: logging.hh:70
Logger::LogLevel
LogLevel
Definition: logging.hh:64
Logger::getPanic
static Logger & getPanic()
Get a Logger for the specified type of message.
Definition: logging.cc:78
Logger::PANIC
@ PANIC
Definition: logging.hh:65
Logger::print
void print(const Loc &loc, const std::string &format, const Args &...args)
Definition: logging.hh:114
ArmISA::ss
Bitfield< 21 > ss
Definition: miscregs_types.hh:56
Logger::print
void print(const Loc &loc, const char *format, const Args &...args)
Definition: logging.hh:106
Logger::getFatal
static Logger & getFatal()
Definition: logging.cc:79
cprintf.hh
compiler.hh
Logger::exit
virtual void exit()
Definition: logging.hh:130
Logger::Loc::file
const char * file
Definition: logging.hh:82
Logger::print
void print(const Loc &loc, const std::string &str)
Definition: logging.hh:94
Logger::WARN
@ WARN
Definition: logging.hh:65
Logger::getInfo
static Logger & getInfo()
Definition: logging.cc:81
Logger::HACK
@ HACK
Definition: logging.hh:65
ccprintf
void ccprintf(cp::Print &print)
Definition: cprintf.hh:127
Logger
Definition: logging.hh:51
Logger::Loc::line
int line
Definition: logging.hh:83
ArmISA::format
Bitfield< 31, 29 > format
Definition: miscregs_types.hh:640
Logger::Loc
Definition: logging.hh:79
ArmISA::s
Bitfield< 4 > s
Definition: miscregs_types.hh:556
Logger::FATAL
@ FATAL
Definition: logging.hh:65
Logger::enabled
bool enabled
Definition: logging.hh:127
Logger::getWarn
static Logger & getWarn()
Definition: logging.cc:80
Logger::getHack
static Logger & getHack()
Definition: logging.cc:82

Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17