gem5  v21.2.1.1
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 <tuple>
47 #include <utility>
48 
49 #include "base/compiler.hh"
50 #include "base/cprintf.hh"
51 
52 namespace gem5
53 {
54 
55 class Logger
56 {
57  public:
58 
62  static Logger &getPanic();
63  static Logger &getFatal();
64  static Logger &getWarn();
65  static Logger &getInfo();
66  static Logger &getHack();
67 
68  enum LogLevel
69  {
72  };
73 
74  static void
76  {
77  getPanic().enabled = (ll >= PANIC);
78  getFatal().enabled = (ll >= FATAL);
79  getWarn().enabled = (ll >= WARN);
80  getInfo().enabled = (ll >= INFO);
81  getHack().enabled = (ll >= HACK);
82  }
83 
84  struct Loc
85  {
86  Loc(const char *file, int line) : file(file), line(line) {}
87  const char *file;
88  int line;
89  };
90 
91  Logger(const char *prefix) : enabled(true), prefix(prefix)
92  {
93  assert(prefix);
94  }
95 
96  virtual ~Logger() {};
97 
98  template<typename ...Args> void
99  print(const Loc &loc, const char *format, const Args &...args)
100  {
101  std::stringstream ss;
102  ccprintf(ss, format, args...);
103  const std::string str = ss.str();
104 
105  std::stringstream ss_formatted;
106  ss_formatted << prefix << str;
107  if (str.length() && str.back() != '\n' && str.back() != '\r')
108  ss_formatted << std::endl;
109  if (!enabled)
110  return;
111  log(loc, ss_formatted.str());
112  }
113 
114  template<typename ...Args> void
115  print(const Loc &loc, const std::string &format, const Args &...args)
116  {
117  print(loc, format.c_str(), args...);
118  }
119 
125  [[noreturn]] void exit_helper() { exit(); ::abort(); }
126 
127  protected:
128  bool enabled;
129 
131  virtual void
132  log(const Loc &loc, std::string s)
133  {
134  std::cerr << loc.file << ":" << loc.line << ": " << s;
135  }
136 
137  virtual void exit() { /* Fall through to the abort in exit_helper. */ }
138 
139  const char *prefix;
140 };
141 
142 
143 #define base_message(logger, ...) \
144  logger.print(::gem5::Logger::Loc(__FILE__, __LINE__), __VA_ARGS__)
145 
146 /*
147  * Only print the message the first time this expression is
148  * encountered. i.e. This doesn't check the string itself and
149  * prevent duplicate strings, this prevents the statement from
150  * happening more than once. So, even if the arguments change and that
151  * would have resulted in a different message thoes messages would be
152  * supressed.
153  */
154 #define base_message_once(...) do { \
155  static bool once = false; \
156  if (!once) { \
157  base_message(__VA_ARGS__); \
158  once = true; \
159  } \
160  } while (0)
161 
162 #define exit_message(logger, ...) \
163  do { \
164  base_message(logger, __VA_ARGS__); \
165  logger.exit_helper(); \
166  } while (0)
167 
178 #define panic(...) exit_message(::gem5::Logger::getPanic(), __VA_ARGS__)
179 
190 #define fatal(...) exit_message(::gem5::Logger::getFatal(), __VA_ARGS__)
191 
204 #define panic_if(cond, ...) \
205  do { \
206  if (GEM5_UNLIKELY(cond)) { \
207  panic("panic condition " # cond " occurred: %s", \
208  ::gem5::csprintf(__VA_ARGS__)); \
209  } \
210  } while (0)
211 
212 
226 #define fatal_if(cond, ...) \
227  do { \
228  if (GEM5_UNLIKELY(cond)) { \
229  fatal("fatal condition " # cond " occurred: %s", \
230  ::gem5::csprintf(__VA_ARGS__)); \
231  } \
232  } while (0)
233 
234 
246 #define warn(...) base_message(::gem5::Logger::getWarn(), __VA_ARGS__)
247 #define inform(...) base_message(::gem5::Logger::getInfo(), __VA_ARGS__)
248 #define hack(...) base_message(::gem5::Logger::getHack(), __VA_ARGS__)
249 
250 #define warn_once(...) \
251  base_message_once(::gem5::Logger::getWarn(), __VA_ARGS__)
252 #define inform_once(...) \
253  base_message_once(::gem5::Logger::getInfo(), __VA_ARGS__)
254 #define hack_once(...) \
255  base_message_once(::gem5::Logger::getHack(), __VA_ARGS__)
256  // end of api_logger
257 
273 #define warn_if(cond, ...) \
274  do { \
275  if (GEM5_UNLIKELY(cond)) \
276  warn(__VA_ARGS__); \
277  } while (0)
278 
279 #define warn_if_once(cond, ...) \
280  do { \
281  if (GEM5_UNLIKELY(cond)) \
282  warn_once(__VA_ARGS__); \
283  } while (0)
284  // end of api_logger
285 
286 #ifdef NDEBUG
287 #define NDEBUG_DEFINED 1
288 #else
289 #define NDEBUG_DEFINED 0
290 #endif
291 
292 template <typename ...Args>
293 inline std::string
294 _assertMsg(const std::string &format, Args... args)
295 {
296  return std::string(": ") + csprintf(format, args...);
297 }
298 
299 inline const char *
301 {
302  return "";
303 }
304 
318 #define gem5_assert(cond, ...) \
319  do { \
320  if (GEM5_UNLIKELY(!NDEBUG_DEFINED && !static_cast<bool>(cond))) { \
321  panic("assert(" #cond ") failed%s", _assertMsg(__VA_ARGS__)); \
322  } \
323  } while (0)
324  // end of api_logger
325 
326 #define chatty_assert(...) \
327  do { \
328  gem5_assert(__VA_ARGS__); \
329  GEM5_DEPRECATED_MACRO(chatty_assert, {}, "Please use gem5_assert()"); \
330  } while(0)
331 
332 } // namespace gem5
333 #endif // __BASE_LOGGING_HH__
gem5::Logger::NUM_LOG_LEVELS
@ NUM_LOG_LEVELS
Definition: logging.hh:71
gem5::Logger
Definition: logging.hh:55
gem5::Logger::exit_helper
void exit_helper()
This helper is necessary since noreturn isn't inherited by virtual functions, and gcc will get mad if...
Definition: logging.hh:125
gem5::Logger::getPanic
static Logger & getPanic()
Get a Logger for the specified type of message.
Definition: logging_mock.cc:86
gem5::Logger::Loc::line
int line
Definition: logging.hh:88
gem5::ArmISA::format
Bitfield< 31, 29 > format
Definition: misc_types.hh:647
gem5::Logger::PANIC
@ PANIC
Definition: logging.hh:70
gem5::Logger::prefix
const char * prefix
Definition: logging.hh:139
gem5::csprintf
std::string csprintf(const char *format, const Args &...args)
Definition: cprintf.hh:161
gem5::Logger::getHack
static Logger & getHack()
Definition: logging_mock.cc:110
gem5::ccprintf
void ccprintf(cp::Print &print)
Definition: cprintf.hh:130
gem5::Logger::INFO
@ INFO
Definition: logging.hh:70
gem5::_assertMsg
std::string _assertMsg(const std::string &format, Args... args)
Definition: logging.hh:294
gem5::Logger::exit
virtual void exit()
Definition: logging.hh:137
gem5::Logger::~Logger
virtual ~Logger()
Definition: logging.hh:96
gem5::Logger::getInfo
static Logger & getInfo()
Definition: logging_mock.cc:104
gem5::ArmISA::s
Bitfield< 4 > s
Definition: misc_types.hh:562
gem5::Logger::Loc
Definition: logging.hh:84
gem5::Logger::print
void print(const Loc &loc, const std::string &format, const Args &...args)
Definition: logging.hh:115
cprintf.hh
ss
std::stringstream ss
Definition: trace.test.cc:45
gem5::Logger::getFatal
static Logger & getFatal()
Definition: logging_mock.cc:92
compiler.hh
gem5::Logger::HACK
@ HACK
Definition: logging.hh:70
gem5::Logger::Loc::Loc
Loc(const char *file, int line)
Definition: logging.hh:86
gem5::Logger::getWarn
static Logger & getWarn()
Definition: logging_mock.cc:98
gem5::Logger::Loc::file
const char * file
Definition: logging.hh:87
gem5::Logger::FATAL
@ FATAL
Definition: logging.hh:70
gem5::Logger::log
virtual void log(const Loc &loc, std::string s)
Generates the log message.
Definition: logging.hh:132
gem5::Logger::setLevel
static void setLevel(LogLevel ll)
Definition: logging.hh:75
gem5::Logger::Logger
Logger(const char *prefix)
Definition: logging.hh:91
gem5::Logger::LogLevel
LogLevel
Definition: logging.hh:68
gem5::Logger::WARN
@ WARN
Definition: logging.hh:70
gem5::Logger::enabled
bool enabled
Definition: logging.hh:128
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: tlb.cc:60
gem5::Logger::print
void print(const Loc &loc, const char *format, const Args &...args)
Definition: logging.hh:99

Generated on Wed May 4 2022 12:13:51 for gem5 by doxygen 1.8.17