gem5  v21.1.0.2
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 namespace gem5
52 {
53 
54 class Logger
55 {
56  public:
57 
61  static Logger &getPanic();
62  static Logger &getFatal();
63  static Logger &getWarn();
64  static Logger &getInfo();
65  static Logger &getHack();
66 
67  enum LogLevel
68  {
71  };
72 
73  static void
75  {
76  getPanic().enabled = (ll >= PANIC);
77  getFatal().enabled = (ll >= FATAL);
78  getWarn().enabled = (ll >= WARN);
79  getInfo().enabled = (ll >= INFO);
80  getHack().enabled = (ll >= HACK);
81  }
82 
83  struct Loc
84  {
85  Loc(const char *file, int line) : file(file), line(line) {}
86  const char *file;
87  int line;
88  };
89 
90  Logger(const char *prefix) : enabled(true), prefix(prefix)
91  {
92  assert(prefix);
93  }
94 
95  virtual ~Logger() {};
96 
97  template<typename ...Args> void
98  print(const Loc &loc, const char *format, const Args &...args)
99  {
100  std::stringstream ss;
101  ccprintf(ss, format, args...);
102  const std::string str = ss.str();
103 
104  std::stringstream ss_formatted;
105  ss_formatted << prefix << str;
106  if (str.length() && str.back() != '\n' && str.back() != '\r')
107  ss_formatted << std::endl;
108  if (!enabled)
109  return;
110  log(loc, ss_formatted.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  [[noreturn]] void exit_helper() { exit(); ::abort(); }
125 
126  protected:
127  bool enabled;
128 
130  virtual void
131  log(const Loc &loc, std::string s)
132  {
133  std::cerr << loc.file << ":" << loc.line << ": " << s;
134  }
135 
136  virtual void exit() { /* Fall through to the abort in exit_helper. */ }
137 
138  const char *prefix;
139 };
140 
141 
142 #define base_message(logger, ...) \
143  logger.print(::gem5::Logger::Loc(__FILE__, __LINE__), __VA_ARGS__)
144 
145 /*
146  * Only print the message the first time this expression is
147  * encountered. i.e. This doesn't check the string itself and
148  * prevent duplicate strings, this prevents the statement from
149  * happening more than once. So, even if the arguments change and that
150  * would have resulted in a different message thoes messages would be
151  * supressed.
152  */
153 #define base_message_once(...) do { \
154  static bool once = false; \
155  if (!once) { \
156  base_message(__VA_ARGS__); \
157  once = true; \
158  } \
159  } while (0)
160 
161 #define exit_message(logger, ...) \
162  do { \
163  base_message(logger, __VA_ARGS__); \
164  logger.exit_helper(); \
165  } while (0)
166 
177 #define panic(...) exit_message(::gem5::Logger::getPanic(), __VA_ARGS__)
178 
189 #define fatal(...) exit_message(::gem5::Logger::getFatal(), __VA_ARGS__)
190 
203 #define panic_if(cond, ...) \
204  do { \
205  if (GEM5_UNLIKELY(cond)) { \
206  panic("panic condition " # cond " occurred: %s", \
207  ::gem5::csprintf(__VA_ARGS__)); \
208  } \
209  } while (0)
210 
211 
225 #define fatal_if(cond, ...) \
226  do { \
227  if (GEM5_UNLIKELY(cond)) { \
228  fatal("fatal condition " # cond " occurred: %s", \
229  ::gem5::csprintf(__VA_ARGS__)); \
230  } \
231  } while (0)
232 
233 
245 #define warn(...) base_message(::gem5::Logger::getWarn(), __VA_ARGS__)
246 #define inform(...) base_message(::gem5::Logger::getInfo(), __VA_ARGS__)
247 #define hack(...) base_message(::gem5::Logger::getHack(), __VA_ARGS__)
248 
249 #define warn_once(...) \
250  base_message_once(::gem5::Logger::getWarn(), __VA_ARGS__)
251 #define inform_once(...) \
252  base_message_once(::gem5::Logger::getInfo(), __VA_ARGS__)
253 #define hack_once(...) \
254  base_message_once(::gem5::Logger::getHack(), __VA_ARGS__)
255  // end of api_logger
256 
272 #define warn_if(cond, ...) \
273  do { \
274  if (GEM5_UNLIKELY(cond)) \
275  warn(__VA_ARGS__); \
276  } while (0)
277 
278 #define warn_if_once(cond, ...) \
279  do { \
280  if (GEM5_UNLIKELY(cond)) \
281  warn_once(__VA_ARGS__); \
282  } while (0)
283  // end of api_logger
284 
298 #ifdef NDEBUG
299 #define chatty_assert(cond, ...)
300 #else
301 #define chatty_assert(cond, ...) \
302  do { \
303  if (GEM5_UNLIKELY(!(cond))) \
304  panic("assert(" # cond ") failed: %s", \
305  ::gem5::csprintf(__VA_ARGS__)); \
306  } while (0)
307 #endif // NDEBUG
308  // end of api_logger
309 
310 } // namespace gem5
311 
312 #endif // __BASE_LOGGING_HH__
gem5::Logger::NUM_LOG_LEVELS
@ NUM_LOG_LEVELS
Definition: logging.hh:70
gem5::Logger
Definition: logging.hh:54
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:124
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:87
gem5::ArmISA::format
Bitfield< 31, 29 > format
Definition: misc_types.hh:646
gem5::Logger::PANIC
@ PANIC
Definition: logging.hh:69
gem5::Logger::prefix
const char * prefix
Definition: logging.hh:138
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:69
gem5::Logger::exit
virtual void exit()
Definition: logging.hh:136
gem5::Logger::~Logger
virtual ~Logger()
Definition: logging.hh:95
gem5::Logger::getInfo
static Logger & getInfo()
Definition: logging_mock.cc:104
gem5::ArmISA::s
Bitfield< 4 > s
Definition: misc_types.hh:561
gem5::Logger::Loc
Definition: logging.hh:83
gem5::Logger::print
void print(const Loc &loc, const std::string &format, const Args &...args)
Definition: logging.hh:114
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:69
gem5::Logger::Loc::Loc
Loc(const char *file, int line)
Definition: logging.hh:85
gem5::Logger::getWarn
static Logger & getWarn()
Definition: logging_mock.cc:98
gem5::Logger::Loc::file
const char * file
Definition: logging.hh:86
gem5::Logger::FATAL
@ FATAL
Definition: logging.hh:69
gem5::Logger::log
virtual void log(const Loc &loc, std::string s)
Generates the log message.
Definition: logging.hh:131
gem5::Logger::setLevel
static void setLevel(LogLevel ll)
Definition: logging.hh:74
gem5::Logger::Logger
Logger(const char *prefix)
Definition: logging.hh:90
gem5::Logger::LogLevel
LogLevel
Definition: logging.hh:67
gem5::Logger::WARN
@ WARN
Definition: logging.hh:69
gem5::Logger::enabled
bool enabled
Definition: logging.hh:127
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::Logger::print
void print(const Loc &loc, const char *format, const Args &...args)
Definition: logging.hh:98

Generated on Tue Sep 21 2021 12:24:57 for gem5 by doxygen 1.8.17