base: Refactor logging to make log level selection cleaner
It's currently possible to change the log level in gem5 by tweaking a set of global variables. These variables are currently exposed to Python using SWIG. This mechanism is far from ideal for two reasons: First, changing the log level requires that the Python world enables or disables individual levels. Ideally, this should be a single call where a log level is selected. Second, exporting global variables is poorly supported by most Python frameworks. SWIG puts variables in their own namespace and PyBind doesn't seem to support it at all. This changeset refactors the logging code to create a more abstract interface. Each log level is associated with an instance of a Logger class. This class contains common functionality, an enable flag, and a verbose flag. Available LogLevels are described by the LogLevel class. Lower log levels are used for more critical messages (PANIC being level 0) and higher levels for less critical messages. The highest log level that is printed is controlled by calling Logger:setLevel(). Change-Id: I31e44299d242d953197a8e62679250c91d6ef776 Signed-off-by: Andreas Sandberg <andreas.sandberg@arm.com> Reviewed-by: Gabor Dozsa <gabor.dozsa@arm.com> Reviewed-by: Curtis Dunham <curtis.dunham@arm.com> Reviewed-by: Jason Lowe-Power <jason@lowepower.com>
This commit is contained in:
parent
bec4409add
commit
60b26f1546
4 changed files with 162 additions and 136 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2014 ARM Limited
|
* Copyright (c) 2014, 2017 ARM Limited
|
||||||
* All rights reserved
|
* All rights reserved
|
||||||
*
|
*
|
||||||
* The license below extends only to copyright in the software and shall
|
* The license below extends only to copyright in the software and shall
|
||||||
|
@ -43,26 +43,35 @@
|
||||||
|
|
||||||
#include "base/misc.hh"
|
#include "base/misc.hh"
|
||||||
|
|
||||||
#include <cstdlib>
|
#include <array>
|
||||||
#include <cstring>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include "base/cprintf.hh"
|
|
||||||
#include "base/hostinfo.hh"
|
#include "base/hostinfo.hh"
|
||||||
#include "base/output.hh"
|
#include "base/output.hh"
|
||||||
#include "base/trace.hh"
|
#include "base/trace.hh"
|
||||||
#include "base/types.hh"
|
#include "base/types.hh"
|
||||||
#include "sim/core.hh"
|
#include "sim/core.hh"
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
bool want_warn = true;
|
Logger &
|
||||||
bool want_info = true;
|
Logger::get(LogLevel ll)
|
||||||
bool want_hack = true;
|
{
|
||||||
|
static std::array<Logger *, NUM_LOG_LEVELS> loggers{{
|
||||||
|
new ExitLogger(std::cerr, "panic"),
|
||||||
|
new ExitLogger(std::cerr, "fatal"),
|
||||||
|
new Logger(std::cerr, "warn"),
|
||||||
|
new Logger(std::cerr, "info"),
|
||||||
|
new Logger(std::cerr, "hack"),
|
||||||
|
}};
|
||||||
|
|
||||||
bool warn_verbose = false;
|
return *loggers[ll];
|
||||||
bool info_verbose = false;
|
}
|
||||||
bool hack_verbose = false;
|
|
||||||
|
void
|
||||||
|
Logger::setLevel(LogLevel ll)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < NUM_LOG_LEVELS; ++i)
|
||||||
|
get(LogLevel(i)).enabled = (i <= ll);
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
newline_if_needed(std::ostream &stream, const char *format)
|
newline_if_needed(std::ostream &stream, const char *format)
|
||||||
|
@ -78,34 +87,28 @@ newline_if_needed(std::ostream &stream, const char *format)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
Logger::Logger(std::ostream &_stream, const char *_prefix)
|
||||||
__exit_epilogue(int code,
|
: enabled(true), verbose(false), stream(_stream), prefix(_prefix)
|
||||||
const char *func, const char *file, int line,
|
|
||||||
const char *format)
|
|
||||||
{
|
{
|
||||||
newline_if_needed(std::cerr, format);
|
|
||||||
|
|
||||||
ccprintf(std::cerr,
|
|
||||||
" @ tick %d\n"
|
|
||||||
"[%s:%s, line %d]\n"
|
|
||||||
"Memory Usage: %ld KBytes\n",
|
|
||||||
curTick(), func, file, line, memUsage());
|
|
||||||
|
|
||||||
if (code < 0)
|
|
||||||
abort();
|
|
||||||
else
|
|
||||||
exit(code);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
__base_message_epilogue(std::ostream &stream, bool verbose,
|
Logger::printEpilogue(const char *func, const char *file, int line,
|
||||||
const char *func, const char *file, int line,
|
|
||||||
const char *format)
|
const char *format)
|
||||||
{
|
{
|
||||||
newline_if_needed(stream, format);
|
newline_if_needed(stream, format);
|
||||||
|
|
||||||
if (verbose) {
|
if (verbose) {
|
||||||
ccprintf(stream, " @ cycle %d\n[%s:%s, line %d]\n",
|
ccprintf(stream, " @ tick %d\n[%s:%s, line %d]\n",
|
||||||
curTick(), func, file, line);
|
curTick(), func, file, line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
ExitLogger::printEpilogue(const char *func, const char *file, int line,
|
||||||
|
const char *format)
|
||||||
|
{
|
||||||
|
Logger::printEpilogue(func, file, line, format);
|
||||||
|
|
||||||
|
ccprintf(stream, "Memory Usage: %ld KBytes\n", memUsage());
|
||||||
|
}
|
||||||
|
|
183
src/base/misc.hh
183
src/base/misc.hh
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2014 ARM Limited
|
* Copyright (c) 2014, 2017 ARM Limited
|
||||||
* All rights reserved
|
* All rights reserved
|
||||||
*
|
*
|
||||||
* The license below extends only to copyright in the software and shall
|
* The license below extends only to copyright in the software and shall
|
||||||
|
@ -47,6 +47,7 @@
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
#include "base/compiler.hh"
|
#include "base/compiler.hh"
|
||||||
#include "base/cprintf.hh"
|
#include "base/cprintf.hh"
|
||||||
|
@ -55,44 +56,91 @@
|
||||||
#define __FUNCTION__ "how to fix me?"
|
#define __FUNCTION__ "how to fix me?"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void __exit_epilogue(int code,
|
class Logger
|
||||||
const char *func, const char *file, int line,
|
{
|
||||||
const char *format) M5_ATTR_NORETURN;
|
public:
|
||||||
|
enum LogLevel {
|
||||||
|
PANIC = 0,
|
||||||
|
FATAL,
|
||||||
|
WARN,
|
||||||
|
INFO,
|
||||||
|
HACK,
|
||||||
|
NUM_LOG_LEVELS,
|
||||||
|
};
|
||||||
|
|
||||||
// General exit message, these functions will never return and will
|
/**
|
||||||
// either abort() if code is < 0 or exit with the code if >= 0
|
* Set the active log level.
|
||||||
template<typename ...Args> void
|
*
|
||||||
__exit_message(const char *prefix, int code,
|
* All levels that are lower or equal to the selected log level
|
||||||
const char *func, const char *file, int line,
|
* will be activated.
|
||||||
const char *format, const Args &...args) M5_ATTR_NORETURN;
|
*
|
||||||
template<typename ...Args> void
|
* @param ll Maximum log level to print
|
||||||
__exit_message(const char *prefix, int code,
|
*/
|
||||||
const char *func, const char *file, int line,
|
static void setLevel(LogLevel ll);
|
||||||
const std::string &format, const Args &...args) M5_ATTR_NORETURN;
|
|
||||||
|
/**
|
||||||
|
* Get a Logger corresponding to a specific log level
|
||||||
|
*
|
||||||
|
* @param ll Log level to access
|
||||||
|
* @return Reference to the requested logger
|
||||||
|
*/
|
||||||
|
static Logger &get(LogLevel ll);
|
||||||
|
|
||||||
|
public:
|
||||||
|
Logger(std::ostream &stream, const char *prefix);
|
||||||
|
virtual ~Logger() {};
|
||||||
|
|
||||||
template<typename ...Args> void
|
template<typename ...Args> void
|
||||||
__exit_message(const char *prefix, int code,
|
print(const char *func, const char *file, int line,
|
||||||
const char *func, const char *file, int line,
|
|
||||||
const char *format, const Args &...args)
|
const char *format, const Args &...args)
|
||||||
{
|
{
|
||||||
std::cerr << prefix << ": ";
|
if (!enabled)
|
||||||
ccprintf(std::cerr, format, args...);
|
return;
|
||||||
|
|
||||||
__exit_epilogue(code, func, file, line, format);
|
if (prefix)
|
||||||
|
stream << prefix << ": ";
|
||||||
|
ccprintf(stream, format, args...);
|
||||||
|
|
||||||
|
printEpilogue(func, file, line, format);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename ...Args> void
|
template<typename ...Args> void
|
||||||
__exit_message(const char *prefix, int code,
|
print(const char *func, const char *file, int line,
|
||||||
const char *func, const char *file, int line,
|
|
||||||
const std::string &format, const Args &...args)
|
const std::string &format, const Args &...args)
|
||||||
{
|
{
|
||||||
__exit_message(prefix, code, func, file, line, format.c_str(),
|
print(func, file, line, format.c_str(), args...);
|
||||||
args...);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define exit_message(prefix, code, ...) \
|
protected:
|
||||||
__exit_message(prefix, code, __FUNCTION__, __FILE__, __LINE__, \
|
virtual void printEpilogue(const char *func, const char *file, int line,
|
||||||
__VA_ARGS__)
|
const char *format);
|
||||||
|
|
||||||
|
public:
|
||||||
|
bool enabled;
|
||||||
|
bool verbose;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::ostream &stream;
|
||||||
|
const char *prefix;
|
||||||
|
};
|
||||||
|
|
||||||
|
class ExitLogger : public Logger
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using Logger::Logger;
|
||||||
|
|
||||||
|
void printEpilogue(const char *func, const char *file, int line,
|
||||||
|
const char *format) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define exit_message(logger, code, ...) \
|
||||||
|
do { \
|
||||||
|
logger.print(__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
|
||||||
|
if (code < 0) \
|
||||||
|
::abort(); \
|
||||||
|
else \
|
||||||
|
::exit(code); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
//
|
//
|
||||||
// This implements a cprintf based panic() function. panic() should
|
// This implements a cprintf based panic() function. panic() should
|
||||||
|
@ -101,7 +149,8 @@ __exit_message(const char *prefix, int code,
|
||||||
// calls abort which can dump core or enter the debugger.
|
// calls abort which can dump core or enter the debugger.
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
#define panic(...) exit_message("panic", -1, __VA_ARGS__)
|
#define panic(...) exit_message(::Logger::get(::Logger::PANIC), -1, \
|
||||||
|
__VA_ARGS__)
|
||||||
|
|
||||||
//
|
//
|
||||||
// This implements a cprintf based fatal() function. fatal() should
|
// This implements a cprintf based fatal() function. fatal() should
|
||||||
|
@ -110,7 +159,8 @@ __exit_message(const char *prefix, int code,
|
||||||
// etc.) and not a simulator bug. fatal() calls abort() like
|
// etc.) and not a simulator bug. fatal() calls abort() like
|
||||||
// panic() does.
|
// panic() does.
|
||||||
//
|
//
|
||||||
#define fatal(...) exit_message("fatal", -1, __VA_ARGS__)
|
#define fatal(...) exit_message(::Logger::get(::Logger::FATAL), 1, \
|
||||||
|
__VA_ARGS__)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Conditional panic macro that checks the supplied condition and only panics
|
* Conditional panic macro that checks the supplied condition and only panics
|
||||||
|
@ -122,8 +172,10 @@ __exit_message(const char *prefix, int code,
|
||||||
*/
|
*/
|
||||||
#define panic_if(cond, ...) \
|
#define panic_if(cond, ...) \
|
||||||
do { \
|
do { \
|
||||||
if ((cond)) \
|
if ((cond)) { \
|
||||||
exit_message("panic condition "#cond" occurred", -1, __VA_ARGS__); \
|
panic("panic condition " # cond " occurred: %s", \
|
||||||
|
csprintf(__VA_ARGS__)); \
|
||||||
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
|
||||||
|
@ -138,39 +190,15 @@ __exit_message(const char *prefix, int code,
|
||||||
*/
|
*/
|
||||||
#define fatal_if(cond, ...) \
|
#define fatal_if(cond, ...) \
|
||||||
do { \
|
do { \
|
||||||
if ((cond)) \
|
if ((cond)) { \
|
||||||
exit_message("fatal condition "#cond" occurred", 1, __VA_ARGS__); \
|
fatal("fatal condition " # cond " occurred: %s", \
|
||||||
|
csprintf(__VA_ARGS__)); \
|
||||||
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
|
||||||
void
|
#define base_message(logger, ...) \
|
||||||
__base_message_epilogue(std::ostream &stream, bool verbose,
|
logger.print(__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__)
|
||||||
const char *func, const char *file, int line,
|
|
||||||
const char *format);
|
|
||||||
|
|
||||||
template<typename ...Args> void
|
|
||||||
__base_message(std::ostream &stream, const char *prefix, bool verbose,
|
|
||||||
const char *func, const char *file, int line,
|
|
||||||
const char *format, const Args &...args)
|
|
||||||
{
|
|
||||||
stream << prefix << ": ";
|
|
||||||
ccprintf(stream, format, args...);
|
|
||||||
|
|
||||||
__base_message_epilogue(stream, verbose, func, file, line, format);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename ...Args> void
|
|
||||||
__base_message(std::ostream &stream, const char *prefix, bool verbose,
|
|
||||||
const char *func, const char *file, int line,
|
|
||||||
const std::string &format, const Args &...args)
|
|
||||||
{
|
|
||||||
__base_message(stream, prefix, verbose, func, file, line, format.c_str(),
|
|
||||||
args...);
|
|
||||||
}
|
|
||||||
|
|
||||||
#define base_message(stream, prefix, verbose, ...) \
|
|
||||||
__base_message(stream, prefix, verbose, __FUNCTION__, __FILE__, __LINE__, \
|
|
||||||
__VA_ARGS__)
|
|
||||||
|
|
||||||
// Only print the message the first time this expression is
|
// Only print the message the first time this expression is
|
||||||
// encountered. i.e. This doesn't check the string itself and
|
// encountered. i.e. This doesn't check the string itself and
|
||||||
|
@ -186,37 +214,20 @@ __base_message(std::ostream &stream, const char *prefix, bool verbose,
|
||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
#define cond_message(cond, ...) do { \
|
|
||||||
if (cond) \
|
|
||||||
base_message(__VA_ARGS__); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define cond_message_once(cond, ...) do { \
|
|
||||||
static bool once = false; \
|
|
||||||
if (!once && cond) { \
|
|
||||||
base_message(__VA_ARGS__); \
|
|
||||||
once = true; \
|
|
||||||
} \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
|
|
||||||
extern bool want_warn, warn_verbose;
|
|
||||||
extern bool want_info, info_verbose;
|
|
||||||
extern bool want_hack, hack_verbose;
|
|
||||||
|
|
||||||
#define warn(...) \
|
#define warn(...) \
|
||||||
cond_message(want_warn, std::cerr, "warn", warn_verbose, __VA_ARGS__)
|
base_message(::Logger::get(::Logger::WARN), __VA_ARGS__)
|
||||||
#define inform(...) \
|
#define inform(...) \
|
||||||
cond_message(want_info, std::cout, "info", info_verbose, __VA_ARGS__)
|
base_message(::Logger::get(::Logger::INFO), __VA_ARGS__)
|
||||||
#define hack(...) \
|
#define hack(...) \
|
||||||
cond_message(want_hack, std::cerr, "hack", hack_verbose, __VA_ARGS__)
|
base_message(::Logger::get(::Logger::HACK), __VA_ARGS__)
|
||||||
|
|
||||||
#define warn_once(...) \
|
#define warn_once(...) \
|
||||||
cond_message_once(want_warn, std::cerr, "warn", warn_verbose, __VA_ARGS__)
|
base_message_once(::Logger::get(::Logger::WARN), __VA_ARGS__)
|
||||||
#define inform_once(...) \
|
#define inform_once(...) \
|
||||||
cond_message_once(want_info, std::cout, "info", info_verbose, __VA_ARGS__)
|
base_message_once(::Logger::get(::Logger::INFO), __VA_ARGS__)
|
||||||
#define hack_once(...) \
|
#define hack_once(...) \
|
||||||
cond_message_once(want_hack, std::cerr, "hack", hack_verbose, __VA_ARGS__)
|
base_message_once(::Logger::get(::Logger::HACK), __VA_ARGS__)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Conditional warning macro that checks the supplied condition and
|
* Conditional warning macro that checks the supplied condition and
|
||||||
|
@ -246,10 +257,8 @@ extern bool want_hack, hack_verbose;
|
||||||
#else //!NDEBUG
|
#else //!NDEBUG
|
||||||
#define chatty_assert(cond, ...) \
|
#define chatty_assert(cond, ...) \
|
||||||
do { \
|
do { \
|
||||||
if (!(cond)) { \
|
if (!(cond)) \
|
||||||
base_message(std::cerr, "assert("#cond") failing", 1, __VA_ARGS__); \
|
panic("assert(" # cond ") failed: %s", csprintf(__VA_ARGS__)); \
|
||||||
assert(cond); \
|
|
||||||
} \
|
|
||||||
} while (0)
|
} while (0)
|
||||||
#endif // NDEBUG
|
#endif // NDEBUG
|
||||||
#endif // __BASE_MISC_HH__
|
#endif // __BASE_MISC_HH__
|
||||||
|
|
|
@ -92,7 +92,20 @@ void serializeAll(const std::string &cpt_dir);
|
||||||
CheckpointIn *getCheckpoint(const std::string &cpt_dir);
|
CheckpointIn *getCheckpoint(const std::string &cpt_dir);
|
||||||
void unserializeGlobals(CheckpointIn &cp);
|
void unserializeGlobals(CheckpointIn &cp);
|
||||||
|
|
||||||
bool want_warn, warn_verbose;
|
class Logger
|
||||||
bool want_info, info_verbose;
|
{
|
||||||
bool want_hack, hack_verbose;
|
public:
|
||||||
|
enum LogLevel {
|
||||||
|
PANIC = 0,
|
||||||
|
FATAL,
|
||||||
|
WARN,
|
||||||
|
INFO,
|
||||||
|
HACK,
|
||||||
|
NUM_LOG_LEVELS,
|
||||||
|
};
|
||||||
|
|
||||||
|
static void setLevel(LogLevel ll);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Logger();
|
||||||
|
};
|
||||||
|
|
|
@ -105,7 +105,8 @@ def run_test(root, switcher=None, freq=1000, verbose=False):
|
||||||
# Suppress "Entering event queue" messages since we get tons of them.
|
# Suppress "Entering event queue" messages since we get tons of them.
|
||||||
# Worse yet, they include the timestamp, which makes them highly
|
# Worse yet, they include the timestamp, which makes them highly
|
||||||
# variable and unsuitable for comparing as test outputs.
|
# variable and unsuitable for comparing as test outputs.
|
||||||
_m5.core.cvar.want_info = verbose
|
if not verbose:
|
||||||
|
_m5.core.Logger.setLevel(_m5.core.Logger.WARN)
|
||||||
|
|
||||||
# instantiate configuration
|
# instantiate configuration
|
||||||
m5.instantiate()
|
m5.instantiate()
|
||||||
|
|
Loading…
Reference in a new issue