From 960935a5bd31b0a325285beb1deb816429c7fe00 Mon Sep 17 00:00:00 2001 From: Andrew Bardsley Date: Thu, 16 Oct 2014 05:49:53 -0400 Subject: base: Reimplement the DPRINTF mechanism in a Logger class This patch adds a Logger class encapsulating dprintf. This allows variants of DPRINTF logging to be constructed and substituted in place of the default behaviour. The Logger provides a logMessage(when, name, format, ...) member function like Trace::dprintf and a getOstream member function to use a raw ostream for logging. A class OstreamLogger is provided which generates the customary debugging output with Trace::OstreamLogger::logMessage being the old Trace::dprintf. --- src/base/trace.cc | 109 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 65 insertions(+), 44 deletions(-) (limited to 'src/base/trace.cc') diff --git a/src/base/trace.cc b/src/base/trace.cc index 00a4c3e6b..711d49655 100644 --- a/src/base/trace.cc +++ b/src/base/trace.cc @@ -1,4 +1,7 @@ /* + * Copyright (c) 2014 ARM Limited + * All rights reserved + * * Copyright (c) 2001-2006 The Regents of The University of Michigan * All rights reserved. * @@ -27,11 +30,13 @@ * * Authors: Nathan Binkert * Steve Reinhardt + * Andrew Bardsley */ #include #include #include +#include #include #include "base/misc.hh" @@ -39,89 +44,105 @@ #include "base/str.hh" #include "base/trace.hh" -using namespace std; +const std::string &name() +{ + static const std::string default_name("global"); + + return default_name; +} -namespace Trace { +namespace Trace +{ -const string DefaultName("global"); bool enabled = false; -// -// This variable holds the output stream for debug information. Other -// than setting up/redirecting this stream, do *NOT* reference this -// directly; use DebugOut() (see below) to access this stream for -// output. -// -ostream *dprintf_stream = &cerr; -ostream & +// This variable holds the output logger for debug information. Other +// than setting up/redirecting this logger, do *NOT* reference this +// directly + +Logger *debug_logger = NULL; + +Logger * +getDebugLogger() +{ + /* Set a default logger to cerr when no other logger is set */ + if (!debug_logger) + debug_logger = new OstreamLogger(std::cerr); + + return debug_logger; +} + +std::ostream & output() { - return *dprintf_stream; + return getDebugLogger()->getOstream(); } void -setOutput(const string &filename) +setDebugLogger(Logger *logger) { - dprintf_stream = simout.find(filename); - if (!dprintf_stream) - dprintf_stream = simout.create(filename); + if (!logger) + warn("Trying to set debug logger to NULL\n"); + else + debug_logger = logger; } ObjectMatch ignore; - -bool -__dprintf_prologue(Tick when, const std::string &name) +void +Logger::dump(Tick when, const std::string &name, const void *d, int len) { if (!name.empty() && ignore.match(name)) - return false; - - std::ostream &os = *dprintf_stream; - - if (when != MaxTick) - ccprintf(os, "%7d: ", when); + return; - if (!name.empty()) - os << name << ": "; - - return true; -} - -void -dump(Tick when, const std::string &name, const void *d, int len) -{ const char *data = static_cast(d); - std::ostream &os = *dprintf_stream; int c, i, j; for (i = 0; i < len; i += 16) { - if (!__dprintf_prologue(when, name)) - return; + std::ostringstream line; - ccprintf(os, "%08x ", i); + ccprintf(line, "%08x ", i); c = len - i; if (c > 16) c = 16; for (j = 0; j < c; j++) { - ccprintf(os, "%02x ", data[i + j] & 0xff); + ccprintf(line, "%02x ", data[i + j] & 0xff); if ((j & 0xf) == 7 && j > 0) - ccprintf(os, " "); + ccprintf(line, " "); } for (; j < 16; j++) - ccprintf(os, " "); - ccprintf(os, " "); + ccprintf(line, " "); + ccprintf(line, " "); for (j = 0; j < c; j++) { int ch = data[i + j] & 0x7f; - ccprintf(os, "%c", (char)(isprint(ch) ? ch : ' ')); + ccprintf(line, "%c", (char)(isprint(ch) ? ch : ' ')); } - ccprintf(os, "\n"); + ccprintf(line, "\n"); + logMessage(when, name, line.str()); if (c < 16) break; } } +void +OstreamLogger::logMessage(Tick when, const std::string &name, + const std::string &message) +{ + if (!name.empty() && ignore.match(name)) + return; + + if (when != MaxTick) + ccprintf(stream, "%7d: ", when); + + if (!name.empty()) + stream << name << ": "; + + stream << message; + stream.flush(); +} + } // namespace Trace -- cgit v1.2.3