2 * Copyright (c) 2014, 2017 ARM Limited
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.
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * All rights reserved.
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.
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.
40 * Authors: Nathan Binkert
45 #ifndef __BASE_LOGGING_HH__
46 #define __BASE_LOGGING_HH__
53 #include "base/compiler.hh"
54 #include "base/cprintf.hh"
56 #if defined(__SUNPRO_CC)
57 #define __FUNCTION__ "how to fix me?"
73 * Set the active log level.
75 * All levels that are lower or equal to the selected log level
78 * @param ll Maximum log level to print
80 static void setLevel(LogLevel ll);
83 * Get a Logger corresponding to a specific log level
85 * @param ll Log level to access
86 * @return Reference to the requested logger
88 static Logger &get(LogLevel ll);
91 Logger(std::ostream &stream, const char *prefix);
94 template<typename ...Args> void
95 print(const char *func, const char *file, int line,
96 const char *format, const Args &...args)
102 stream << prefix << ": ";
103 ccprintf(stream, format, args...);
105 printEpilogue(func, file, line, format);
108 template<typename ...Args> void
109 print(const char *func, const char *file, int line,
110 const std::string &format, const Args &...args)
112 print(func, file, line, format.c_str(), args...);
116 virtual void printEpilogue(const char *func, const char *file, int line,
124 std::ostream &stream;
128 #define exit_message(logger, code, ...) \
130 logger.print(__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
138 // This implements a cprintf based panic() function. panic() should
139 // be called when something happens that should never ever happen
140 // regardless of what the user does (i.e., an acutal m5 bug). panic()
141 // calls abort which can dump core or enter the debugger.
144 #define panic(...) exit_message(::Logger::get(::Logger::PANIC), -1, \
148 // This implements a cprintf based fatal() function. fatal() should
149 // be called when the simulation cannot continue due to some condition
150 // that is the user's fault (bad configuration, invalid arguments,
151 // etc.) and not a simulator bug. fatal() calls abort() like
154 #define fatal(...) exit_message(::Logger::get(::Logger::FATAL), 1, \
158 * Conditional panic macro that checks the supplied condition and only panics
159 * if the condition is true and allows the programmer to specify diagnostic
160 * printout. Useful to replace if + panic, or if + print + assert, etc.
162 * @param cond Condition that is checked; if true -> panic
163 * @param ... Printf-based format string with arguments, extends printout.
165 #define panic_if(cond, ...) \
168 panic("panic condition " # cond " occurred: %s", \
169 csprintf(__VA_ARGS__)); \
175 * Conditional fatal macro that checks the supplied condition and only causes a
176 * fatal error if the condition is true and allows the programmer to specify
177 * diagnostic printout. Useful to replace if + fatal, or if + print + assert,
180 * @param cond Condition that is checked; if true -> fatal
181 * @param ... Printf-based format string with arguments, extends printout.
183 #define fatal_if(cond, ...) \
186 fatal("fatal condition " # cond " occurred: %s", \
187 csprintf(__VA_ARGS__)); \
192 #define base_message(logger, ...) \
193 logger.print(__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__)
195 // Only print the message the first time this expression is
196 // encountered. i.e. This doesn't check the string itself and
197 // prevent duplicate strings, this prevents the statement from
198 // happening more than once. So, even if the arguments change and that
199 // would have resulted in a different message thoes messages would be
201 #define base_message_once(...) do { \
202 static bool once = false; \
204 base_message(__VA_ARGS__); \
211 base_message(::Logger::get(::Logger::WARN), __VA_ARGS__)
212 #define inform(...) \
213 base_message(::Logger::get(::Logger::INFO), __VA_ARGS__)
215 base_message(::Logger::get(::Logger::HACK), __VA_ARGS__)
217 #define warn_once(...) \
218 base_message_once(::Logger::get(::Logger::WARN), __VA_ARGS__)
219 #define inform_once(...) \
220 base_message_once(::Logger::get(::Logger::INFO), __VA_ARGS__)
221 #define hack_once(...) \
222 base_message_once(::Logger::get(::Logger::HACK), __VA_ARGS__)
225 * Conditional warning macro that checks the supplied condition and
226 * only prints a warning if the condition is true. Useful to replace
229 * @param cond Condition that is checked; if true -> warn
230 * @param ... Printf-based format string with arguments, extends printout.
232 #define warn_if(cond, ...) \
239 * The chatty assert macro will function like a normal assert, but will allow
240 * the specification of additional, helpful material to aid debugging why the
241 * assertion actually failed. Like the normal assertion, the chatty_assert
242 * will not be active in fast builds.
244 * @param cond Condition that is checked; if false -> assert
245 * @param ... Printf-based format string with arguments, extends printout.
248 #define chatty_assert(cond, ...)
250 #define chatty_assert(cond, ...) \
253 panic("assert(" # cond ") failed: %s", csprintf(__VA_ARGS__)); \
256 #endif // __BASE_LOGGING_HH__