logging.cc (12334:e0ab29a34764) logging.cc (12375:ceeb064dec4a)
1/*
2 * Copyright (c) 2014, 2017 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

--- 29 unchanged lines hidden (view full) ---

38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Nathan Binkert
41 * Andreas Sandberg
42 */
43
44#include "base/logging.hh"
45
1/*
2 * Copyright (c) 2014, 2017 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

--- 29 unchanged lines hidden (view full) ---

38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Nathan Binkert
41 * Andreas Sandberg
42 */
43
44#include "base/logging.hh"
45
46#include <array>
46#include <sstream>
47
48#include "base/hostinfo.hh"
47
48#include "base/hostinfo.hh"
49#include "base/output.hh"
50#include "base/trace.hh"
51#include "base/types.hh"
52#include "sim/core.hh"
53
49
54void
55Logger::setLevel(LogLevel ll)
56{
57 for (int i = 0; i < NUM_LOG_LEVELS; ++i)
58 get(LogLevel(i)).enabled = (i <= ll);
59}
50namespace {
60
51
61static void
62newline_if_needed(std::ostream &stream, const char *format)
52class NormalLogger : public Logger
63{
53{
64 const size_t format_len(strlen(format));
54 public:
55 using Logger::Logger;
65
56
66 switch (format_len ? format[format_len - 1] : '\0') {
67 case '\n':
68 case '\r':
69 break;
70 default:
71 stream << std::endl;
72 }
73}
57 protected:
58 void log(const Loc &loc, std::string s) override { std::cerr << s; }
59};
74
60
75Logger::Logger(std::ostream &_stream, const char *_prefix)
76 : enabled(true), verbose(false), stream(_stream), prefix(_prefix)
61class ExitLogger : public NormalLogger
77{
62{
78}
63 public:
64 using NormalLogger::NormalLogger;
79
65
80void
81Logger::printEpilogue(const char *func, const char *file, int line,
82 const char *format)
83{
84 newline_if_needed(stream, format);
85
86 if (verbose) {
87 ccprintf(stream, " @ tick %d\n[%s:%s, line %d]\n",
88 curTick(), func, file, line);
66 protected:
67 void
68 log(const Loc &loc, std::string s) override
69 {
70 std::stringstream ss;
71 ccprintf(ss, "Memory Usage: %ld KBytes\n", memUsage());
72 NormalLogger::log(loc, s + ss.str());
89 }
73 }
90}
74};
91
75
92class ExitLogger : public Logger
76class FatalLogger : public ExitLogger
93{
94 public:
77{
78 public:
95 using Logger::Logger;
79 using ExitLogger::ExitLogger;
96
80
97 void printEpilogue(const char *func, const char *file, int line,
98 const char *format) override;
81 protected:
82 void exit() override { ::exit(1); }
99};
100
83};
84
101void
102ExitLogger::printEpilogue(const char *func, const char *file, int line,
103 const char *format)
104{
105 Logger::printEpilogue(func, file, line, format);
85ExitLogger panicLogger("panic: ");
86FatalLogger fatalLogger("fatal: ");
87NormalLogger warnLogger("warn: ");
88NormalLogger infoLogger("info: ");
89NormalLogger hackLogger("hack: ");
106
90
107 ccprintf(stream, "Memory Usage: %ld KBytes\n", memUsage());
108}
91} // anonymous namespace
109
92
110Logger &
111Logger::get(LogLevel ll)
112{
113 static std::array<Logger *, NUM_LOG_LEVELS> loggers{{
114 new ExitLogger(std::cerr, "panic"),
115 new ExitLogger(std::cerr, "fatal"),
116 new Logger(std::cerr, "warn"),
117 new Logger(std::cerr, "info"),
118 new Logger(std::cerr, "hack"),
119 }};
120
121 return *loggers[ll];
122}
93Logger &Logger::getPanic() { return panicLogger; }
94Logger &Logger::getFatal() { return fatalLogger; }
95Logger &Logger::getWarn() { return warnLogger; }
96Logger &Logger::getInfo() { return infoLogger; }
97Logger &Logger::getHack() { return hackLogger; }