changeset 8c369af31b6c in /z/repo/gem5
details: http://repo.gem5.org/gem5?cmd=changeset;node=8c369af31b6c
description:
        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 <[email protected]>
        Reviewed-by: Gabor Dozsa <[email protected]>
        Reviewed-by: Curtis Dunham <[email protected]>
        Reviewed-by: Jason Lowe-Power <[email protected]>

diffstat:

 src/base/misc.cc            |   65 +++++++------
 src/base/misc.hh            |  211 ++++++++++++++++++++++---------------------
 src/python/swig/core.i      |   19 +++-
 tests/configs/switcheroo.py |    3 +-
 4 files changed, 162 insertions(+), 136 deletions(-)

diffs (truncated from 436 to 300 lines):

diff -r 7388b21d7eac -r 8c369af31b6c src/base/misc.cc
--- a/src/base/misc.cc  Mon Feb 27 11:25:00 2017 +0000
+++ b/src/base/misc.cc  Mon Feb 27 11:25:01 2017 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 ARM Limited
+ * Copyright (c) 2014, 2017 ARM Limited
  * All rights reserved
  *
  * The license below extends only to copyright in the software and shall
@@ -43,26 +43,35 @@
 
 #include "base/misc.hh"
 
-#include <cstdlib>
-#include <cstring>
-#include <string>
+#include <array>
 
-#include "base/cprintf.hh"
 #include "base/hostinfo.hh"
 #include "base/output.hh"
 #include "base/trace.hh"
 #include "base/types.hh"
 #include "sim/core.hh"
 
-using namespace std;
 
-bool want_warn = true;
-bool want_info = true;
-bool want_hack = true;
+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"),
+    }};
 
-bool warn_verbose = false;
-bool info_verbose = false;
-bool hack_verbose = false;
+    return *loggers[ll];
+}
+
+void
+Logger::setLevel(LogLevel ll)
+{
+    for (int i = 0; i < NUM_LOG_LEVELS; ++i)
+        get(LogLevel(i)).enabled = (i <= ll);
+}
 
 static void
 newline_if_needed(std::ostream &stream, const char *format)
@@ -78,34 +87,28 @@
     }
 }
 
-void
-__exit_epilogue(int code,
-                const char *func, const char *file, int line,
-                const char *format)
+Logger::Logger(std::ostream &_stream, const char *_prefix)
+    : enabled(true), verbose(false), stream(_stream), prefix(_prefix)
 {
-    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
-__base_message_epilogue(std::ostream &stream, bool verbose,
-                        const char *func, const char *file, int line,
+Logger::printEpilogue(const char *func, const char *file, int line,
                         const char *format)
 {
     newline_if_needed(stream, format);
 
     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);
     }
 }
+
+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());
+}
diff -r 7388b21d7eac -r 8c369af31b6c src/base/misc.hh
--- a/src/base/misc.hh  Mon Feb 27 11:25:00 2017 +0000
+++ b/src/base/misc.hh  Mon Feb 27 11:25:01 2017 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 ARM Limited
+ * Copyright (c) 2014, 2017 ARM Limited
  * All rights reserved
  *
  * The license below extends only to copyright in the software and shall
@@ -47,6 +47,7 @@
 
 #include <cassert>
 #include <iostream>
+#include <utility>
 
 #include "base/compiler.hh"
 #include "base/cprintf.hh"
@@ -55,44 +56,91 @@
 #define __FUNCTION__ "how to fix me?"
 #endif
 
-void __exit_epilogue(int code,
-                     const char *func, const char *file, int line,
-                     const char *format) M5_ATTR_NORETURN;
+class Logger
+{
+  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
-template<typename ...Args> void
-__exit_message(const char *prefix, int code,
-               const char *func, const char *file, int line,
-               const char *format, const Args &...args) M5_ATTR_NORETURN;
-template<typename ...Args> void
-__exit_message(const char *prefix, int code,
-               const char *func, const char *file, int line,
-               const std::string &format, const Args &...args) 
M5_ATTR_NORETURN;
+    /**
+     * Set the active log level.
+     *
+     * All levels that are lower or equal to the selected log level
+     * will be activated.
+     *
+     * @param ll Maximum log level to print
+     */
+    static void setLevel(LogLevel ll);
 
-template<typename ...Args> void
-__exit_message(const char *prefix, int code,
-               const char *func, const char *file, int line,
-               const char *format, const Args &...args)
+    /**
+     * 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
+    print(const char *func, const char *file, int line,
+          const char *format, const Args &...args)
+    {
+        if (!enabled)
+            return;
+
+        if (prefix)
+            stream << prefix << ": ";
+        ccprintf(stream, format, args...);
+
+        printEpilogue(func, file, line, format);
+    }
+
+    template<typename ...Args> void
+    print(const char *func, const char *file, int line,
+          const std::string &format, const Args &...args)
+    {
+        print(func, file, line, format.c_str(), args...);
+    }
+
+  protected:
+    virtual void printEpilogue(const char *func, const char *file, int line,
+                               const char *format);
+
+  public:
+    bool enabled;
+    bool verbose;
+
+  protected:
+    std::ostream &stream;
+    const char *prefix;
+};
+
+class ExitLogger : public Logger
 {
-    std::cerr << prefix << ": ";
-    ccprintf(std::cerr, format, args...);
+  public:
+    using Logger::Logger;
 
-    __exit_epilogue(code, func, file, line, format);
-}
+    void printEpilogue(const char *func, const char *file, int line,
+                       const char *format) override;
+};
 
-template<typename ...Args> void
-__exit_message(const char *prefix, int code,
-               const char *func, const char *file, int line,
-               const std::string &format, const Args &...args)
-{
-    __exit_message(prefix, code, func, file, line, format.c_str(),
-                   args...);
-}
-
-#define exit_message(prefix, code, ...)                                 \
-    __exit_message(prefix, code, __FUNCTION__, __FILE__, __LINE__,      \
-                   __VA_ARGS__)
+#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
@@ -101,7 +149,8 @@
 // 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
@@ -110,7 +159,8 @@
 // etc.) and not a simulator bug.  fatal() calls  abort() like
 // 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
@@ -120,10 +170,12 @@
  * @param cond Condition that is checked; if true -> panic
  * @param ...  Printf-based format string with arguments, extends printout.
  */
-#define panic_if(cond, ...) \
-    do { \
-        if ((cond)) \
-            exit_message("panic condition "#cond" occurred", -1, __VA_ARGS__); 
\
+#define panic_if(cond, ...)                                  \
+    do {                                                     \
+        if ((cond)) {                                        \
+            panic("panic condition " # cond " occurred: %s", \
+                  csprintf(__VA_ARGS__));                    \
+        }                                                    \
     } while (0)
 
 
@@ -136,41 +188,17 @@
  * @param cond Condition that is checked; if true -> fatal
  * @param ...  Printf-based format string with arguments, extends printout.
  */
-#define fatal_if(cond, ...) \
-    do { \
-        if ((cond)) \
-            exit_message("fatal condition "#cond" occurred", 1, __VA_ARGS__); \
+#define fatal_if(cond, ...)                                     \
+    do {                                                        \
+        if ((cond)) {                                           \
+            fatal("fatal condition " # cond " occurred: %s",    \
+                  csprintf(__VA_ARGS__));                       \
+        }                                                       \
     } while (0)
_______________________________________________
gem5-dev mailing list
[email protected]
http://m5sim.org/mailman/listinfo/gem5-dev

Reply via email to