diff options
author | Gabe Black <gabeblack@google.com> | 2017-12-03 01:22:38 -0800 |
---|---|---|
committer | Gabe Black <gabeblack@google.com> | 2017-12-12 19:45:58 +0000 |
commit | 211124aff2c0750d1b80f0cd71fec3f06c50bcc6 (patch) | |
tree | cbd95fa69c55820f0b5ed6330b4411d1e4469d43 /src/base/logging.cc | |
parent | a51576e19c7f63930bc3b59c9a2e0a49cbc0bd81 (diff) | |
download | gem5-211124aff2c0750d1b80f0cd71fec3f06c50bcc6.tar.xz |
misc: Rework the logging functions.
Removed the "verbose" switch which wasn't used.
Replaced the "get(LogLevel)" function with a get for each level. The
parameter was always constant, so we can just call the right function
at the right time.
Made the "exit" behavior of panic/fatal a part of the logging
implementation so that it can be overridden, and corrected a comment
which said that both fatal and panic called ::abort().
Got rid of the printEpilogue function by reworking the print() methods.
The subclasses of Logger can now override a "log" function which takes
a composed message, letting the Logger class centralize how the message
is put together and leaving the actual output mechanism to the
subclass.
Unfortunately there wasn't a way to tell gcc that the panic/fatal
macros wouldn't return, so there needed to be an exit_helper wrapper
function which calls the actual logger exit function. That can be
marked as noreturn, unlike the virtual exit function. If the exit
function does return, the wrapper will call ::abort(), placating gcc
and ensuring that even if exit isn't implemented properly, exit_helper
will still not return. That also provides a handy default
implementation.
Change-Id: I66d0cebd59f1127db980f3b565dbdf60687d8862
Reviewed-on: https://gem5-review.googlesource.com/6263
Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com>
Maintainer: Andreas Sandberg <andreas.sandberg@arm.com>
Diffstat (limited to 'src/base/logging.cc')
-rw-r--r-- | src/base/logging.cc | 93 |
1 files changed, 34 insertions, 59 deletions
diff --git a/src/base/logging.cc b/src/base/logging.cc index cec9c8360..adc3deb90 100644 --- a/src/base/logging.cc +++ b/src/base/logging.cc @@ -43,80 +43,55 @@ #include "base/logging.hh" -#include <array> +#include <sstream> #include "base/hostinfo.hh" -#include "base/output.hh" -#include "base/trace.hh" -#include "base/types.hh" -#include "sim/core.hh" -void -Logger::setLevel(LogLevel ll) -{ - for (int i = 0; i < NUM_LOG_LEVELS; ++i) - get(LogLevel(i)).enabled = (i <= ll); -} +namespace { -static void -newline_if_needed(std::ostream &stream, const char *format) +class NormalLogger : public Logger { - const size_t format_len(strlen(format)); - - switch (format_len ? format[format_len - 1] : '\0') { - case '\n': - case '\r': - break; - default: - stream << std::endl; - } -} + public: + using Logger::Logger; -Logger::Logger(std::ostream &_stream, const char *_prefix) - : enabled(true), verbose(false), stream(_stream), prefix(_prefix) -{ -} + protected: + void log(const Loc &loc, std::string s) override { std::cerr << s; } +}; -void -Logger::printEpilogue(const char *func, const char *file, int line, - const char *format) +class ExitLogger : public NormalLogger { - newline_if_needed(stream, format); + public: + using NormalLogger::NormalLogger; - if (verbose) { - ccprintf(stream, " @ tick %d\n[%s:%s, line %d]\n", - curTick(), func, file, line); + protected: + void + log(const Loc &loc, std::string s) override + { + std::stringstream ss; + ccprintf(ss, "Memory Usage: %ld KBytes\n", memUsage()); + NormalLogger::log(loc, s + ss.str()); } -} +}; -class ExitLogger : public Logger +class FatalLogger : public ExitLogger { public: - using Logger::Logger; + using ExitLogger::ExitLogger; - void printEpilogue(const char *func, const char *file, int line, - const char *format) override; + protected: + void exit() override { ::exit(1); } }; -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()); -} +ExitLogger panicLogger("panic: "); +FatalLogger fatalLogger("fatal: "); +NormalLogger warnLogger("warn: "); +NormalLogger infoLogger("info: "); +NormalLogger hackLogger("hack: "); -Logger & -Logger::get(LogLevel ll) -{ - 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"), - }}; +} // anonymous namespace - return *loggers[ll]; -} +Logger &Logger::getPanic() { return panicLogger; } +Logger &Logger::getFatal() { return fatalLogger; } +Logger &Logger::getWarn() { return warnLogger; } +Logger &Logger::getInfo() { return infoLogger; } +Logger &Logger::getHack() { return hackLogger; } |