Daniel Carvalho has uploaded this change for review. ( https://gem5-review.googlesource.com/c/public/gem5/+/42141 )

Change subject: base: Add LOC to Loggers
......................................................................

base: Add LOC to Loggers

Printing the line and the file that triggered a log
is useful for debugging.

Change-Id: I74e0637b2943049134bd3e9a4bc6cab3766591a9
Signed-off-by: Daniel R. Carvalho <[email protected]>
---
M src/base/gtest/logging.cc
M src/base/logging.hh
M src/base/logging.test.cc
3 files changed, 91 insertions(+), 68 deletions(-)



diff --git a/src/base/gtest/logging.cc b/src/base/gtest/logging.cc
index d9cb9eb..4ca0f9f 100644
--- a/src/base/gtest/logging.cc
+++ b/src/base/gtest/logging.cc
@@ -58,11 +58,6 @@
     using Logger::Logger;

   protected:
-    void
-    log(const Loc &loc, std::string s) override
-    {
-        std::cerr << loc.file << ":" << loc.line << ": " << s;
-    }
     // Throw an exception to escape down to the gtest framework.
     void exit() override { throw GTestException(); }
 };
diff --git a/src/base/logging.hh b/src/base/logging.hh
index 4ef700a..9d0c478 100644
--- a/src/base/logging.hh
+++ b/src/base/logging.hh
@@ -123,7 +123,12 @@
     bool enabled;

     /** Generates the log message. By default it is sent to cerr. */
-    virtual void log(const Loc &loc, std::string s) { std::cerr << s; }
+    virtual void
+    log(const Loc &loc, std::string s)
+    {
+        std::cerr << loc.file << ":" << loc.line << ": " << s;
+    }
+
     virtual void exit() { /* Fall through to the abort in exit_helper. */ }

     const char *prefix;
diff --git a/src/base/logging.test.cc b/src/base/logging.test.cc
index c979148..840e29e 100644
--- a/src/base/logging.test.cc
+++ b/src/base/logging.test.cc
@@ -182,7 +182,8 @@
     Logger &logger = Logger::getWarn();
     testing::internal::CaptureStderr();
     logger.print(Logger::Loc("File", 10), "message");
-    ASSERT_EQ(testing::internal::GetCapturedStderr(), "warn: message\n");
+    ASSERT_EQ(testing::internal::GetCapturedStderr(),
+        "File:10: warn: message\n");

     // PANIC does not include WARN
     Logger::setLevel(Logger::PANIC);
@@ -200,7 +201,8 @@
     Logger &logger = Logger::getInfo();
     testing::internal::CaptureStderr();
     logger.print(Logger::Loc("File", 10), "message");
-    ASSERT_EQ(testing::internal::GetCapturedStderr(), "info: message\n");
+    ASSERT_EQ(testing::internal::GetCapturedStderr(),
+        "File:10: info: message\n");

     // PANIC does not include INFO
     Logger::setLevel(Logger::PANIC);
@@ -218,7 +220,8 @@
     Logger &logger = Logger::getHack();
     testing::internal::CaptureStderr();
     logger.print(Logger::Loc("File", 10), "message");
-    ASSERT_EQ(testing::internal::GetCapturedStderr(), "hack: message\n");
+    ASSERT_EQ(testing::internal::GetCapturedStderr(),
+        "File:10: hack: message\n");

     // PANIC does not include HACK
     Logger::setLevel(Logger::PANIC);
@@ -239,7 +242,7 @@
     testing::internal::CaptureStderr();
     logger.print(Logger::Loc("File", 10), "message");
     ASSERT_NE(testing::internal::GetCapturedStderr().find(
-        "fatal: message\nMemory Usage:"), std::string::npos);
+        "File:10: fatal: message\nMemory Usage:"), std::string::npos);

     // PANIC does not include FATAL
     Logger::setLevel(Logger::PANIC);
@@ -259,7 +262,7 @@
     testing::internal::CaptureStderr();
     logger.print(Logger::Loc("File", 10), "message");
     std::string error = testing::internal::GetCapturedStderr().substr();
-    ASSERT_NE(error.find("panic: message\nMemory Usage:"),
+    ASSERT_NE(error.find("File:10: panic: message\nMemory Usage:"),
         std::string::npos);

     Logger::setLevel(Logger::NUM_LOG_LEVELS);
@@ -368,14 +371,14 @@
 TEST(LoggingDeathTest, Panic)
 {
     ASSERT_DEATH(panic("message\n"),
-        ::testing::StartsWith("panic: message\nMemory Usage:"));
+        ::testing::HasSubstr("panic: message\nMemory Usage:"));
 }

 /** Test macro fatal. */
 TEST(LoggingDeathTest, Fatal)
 {
     ASSERT_DEATH(fatal("message\n"),
-        ::testing::StartsWith("fatal: message\nMemory Usage:"));
+        ::testing::HasSubstr("fatal: message\nMemory Usage:"));
 }

/** Test that panic_if only prints the message when the condition is true. */
@@ -384,7 +387,7 @@
     for (int i = 0; i < 4; i++) {
         panic_if(i == 4, "message\n");
     }
-    ASSERT_DEATH(panic_if(true, "message\n"), ::testing::StartsWith(
+    ASSERT_DEATH(panic_if(true, "message\n"), ::testing::HasSubstr(
         "panic: panic condition true occurred: message\nMemory Usage:"));
 }

@@ -394,7 +397,7 @@
     for (int i = 0; i < 4; i++) {
         fatal_if(i == 4, "message\n");
     }
-    ASSERT_DEATH(fatal_if(true, "message\n"), ::testing::StartsWith(
+    ASSERT_DEATH(fatal_if(true, "message\n"), ::testing::HasSubstr(
         "fatal: fatal condition true occurred: message\nMemory Usage:"));
 }

@@ -405,38 +408,38 @@
     // if it does not already have at least one.
     testing::internal::CaptureStderr();
     warn("message");
-    ASSERT_EQ(testing::internal::GetCapturedStderr(),
-        "warn: message\n");
+    ASSERT_THAT(testing::internal::GetCapturedStderr(),
+        ::testing::HasSubstr("warn: message\n"));

     testing::internal::CaptureStderr();
     warn("message\n");
-    ASSERT_EQ(testing::internal::GetCapturedStderr(),
-        "warn: message\n");
+    ASSERT_THAT(testing::internal::GetCapturedStderr(),
+        ::testing::HasSubstr("warn: message\n"));

     testing::internal::CaptureStderr();
     warn("sample message");
-    ASSERT_EQ(testing::internal::GetCapturedStderr(),
-        "warn: sample message\n");
+    ASSERT_THAT(testing::internal::GetCapturedStderr(),
+        ::testing::HasSubstr("warn: sample message\n"));

     testing::internal::CaptureStderr();
     warn("sample message\nwith \nthree lines");
-    ASSERT_EQ(testing::internal::GetCapturedStderr(),
-        "warn: sample message\nwith \nthree lines\n");
+    ASSERT_THAT(testing::internal::GetCapturedStderr(),
+ ::testing::HasSubstr("warn: sample message\nwith \nthree lines\n"));

     testing::internal::CaptureStderr();
     warn("sample message\n\n");
-    ASSERT_EQ(testing::internal::GetCapturedStderr(),
-        "warn: sample message\n\n");
+    ASSERT_THAT(testing::internal::GetCapturedStderr(),
+        ::testing::HasSubstr("warn: sample message\n\n"));

     testing::internal::CaptureStderr();
     warn("%s message", "sample");
-    ASSERT_EQ(testing::internal::GetCapturedStderr(),
-        "warn: sample message\n");
+    ASSERT_THAT(testing::internal::GetCapturedStderr(),
+        ::testing::HasSubstr("warn: sample message\n"));

     testing::internal::CaptureStderr();
     warn("sample %s %d\n", "message", 2);
-    ASSERT_EQ(testing::internal::GetCapturedStderr(),
-        "warn: sample message 2\n");
+    ASSERT_THAT(testing::internal::GetCapturedStderr(),
+        ::testing::HasSubstr("warn: sample message 2\n"));
 }

 /** Test macro inform. */
@@ -446,38 +449,38 @@
     // if it does not already have at least one.
     testing::internal::CaptureStderr();
     inform("message");
-    ASSERT_EQ(testing::internal::GetCapturedStderr(),
-        "info: message\n");
+    ASSERT_THAT(testing::internal::GetCapturedStderr(),
+        ::testing::HasSubstr("info: message\n"));

     testing::internal::CaptureStderr();
     inform("message\n");
-    ASSERT_EQ(testing::internal::GetCapturedStderr(),
-        "info: message\n");
+    ASSERT_THAT(testing::internal::GetCapturedStderr(),
+        ::testing::HasSubstr("info: message\n"));

     testing::internal::CaptureStderr();
     inform("sample message");
-    ASSERT_EQ(testing::internal::GetCapturedStderr(),
-        "info: sample message\n");
+    ASSERT_THAT(testing::internal::GetCapturedStderr(),
+        ::testing::HasSubstr("info: sample message\n"));

     testing::internal::CaptureStderr();
     inform("sample message\nwith \nthree lines");
-    ASSERT_EQ(testing::internal::GetCapturedStderr(),
-        "info: sample message\nwith \nthree lines\n");
+    ASSERT_THAT(testing::internal::GetCapturedStderr(),
+ ::testing::HasSubstr("info: sample message\nwith \nthree lines\n"));

     testing::internal::CaptureStderr();
     inform("sample message\n\n");
-    ASSERT_EQ(testing::internal::GetCapturedStderr(),
-        "info: sample message\n\n");
+    ASSERT_THAT(testing::internal::GetCapturedStderr(),
+        ::testing::HasSubstr("info: sample message\n\n"));

     testing::internal::CaptureStderr();
     inform("%s message", "sample");
-    ASSERT_EQ(testing::internal::GetCapturedStderr(),
-        "info: sample message\n");
+    ASSERT_THAT(testing::internal::GetCapturedStderr(),
+        ::testing::HasSubstr("info: sample message\n"));

     testing::internal::CaptureStderr();
     inform("sample %s %d\n", "message", 2);
-    ASSERT_EQ(testing::internal::GetCapturedStderr(),
-        "info: sample message 2\n");
+    ASSERT_THAT(testing::internal::GetCapturedStderr(),
+        ::testing::HasSubstr("info: sample message 2\n"));
 }

 /** Test macro hack. */
@@ -487,38 +490,38 @@
     // if it does not already have at least one.
     testing::internal::CaptureStderr();
     hack("message");
-    ASSERT_EQ(testing::internal::GetCapturedStderr(),
-        "hack: message\n");
+    ASSERT_THAT(testing::internal::GetCapturedStderr(),
+        ::testing::HasSubstr("hack: message\n"));

     testing::internal::CaptureStderr();
     hack("message\n");
-    ASSERT_EQ(testing::internal::GetCapturedStderr(),
-        "hack: message\n");
+    ASSERT_THAT(testing::internal::GetCapturedStderr(),
+        ::testing::HasSubstr("hack: message\n"));

     testing::internal::CaptureStderr();
     hack("sample message");
-    ASSERT_EQ(testing::internal::GetCapturedStderr(),
-        "hack: sample message\n");
+    ASSERT_THAT(testing::internal::GetCapturedStderr(),
+        ::testing::HasSubstr("hack: sample message\n"));

     testing::internal::CaptureStderr();
     hack("sample message\nwith \nthree lines");
-    ASSERT_EQ(testing::internal::GetCapturedStderr(),
-        "hack: sample message\nwith \nthree lines\n");
+    ASSERT_THAT(testing::internal::GetCapturedStderr(),
+ ::testing::HasSubstr("hack: sample message\nwith \nthree lines\n"));

     testing::internal::CaptureStderr();
     hack("sample message\n\n");
-    ASSERT_EQ(testing::internal::GetCapturedStderr(),
-        "hack: sample message\n\n");
+    ASSERT_THAT(testing::internal::GetCapturedStderr(),
+        ::testing::HasSubstr("hack: sample message\n\n"));

     testing::internal::CaptureStderr();
     hack("%s message", "sample");
-    ASSERT_EQ(testing::internal::GetCapturedStderr(),
-        "hack: sample message\n");
+    ASSERT_THAT(testing::internal::GetCapturedStderr(),
+        ::testing::HasSubstr("hack: sample message\n"));

     testing::internal::CaptureStderr();
     hack("sample %s %d\n", "message", 2);
-    ASSERT_EQ(testing::internal::GetCapturedStderr(),
-        "hack: sample message 2\n");
+    ASSERT_THAT(testing::internal::GetCapturedStderr(),
+        ::testing::HasSubstr("hack: sample message 2\n"));
 }

 /** Test that warn_once only prints the message once in a loop. */
@@ -527,8 +530,12 @@
     for (int i = 0; i < 10; i++) {
         testing::internal::CaptureStderr();
         warn_once("message\n");
-        ASSERT_EQ(testing::internal::GetCapturedStderr(),
-            (i == 0) ? "warn: message\n" : "");
+        if (i == 0) {
+            ASSERT_THAT(testing::internal::GetCapturedStderr(),
+                ::testing::HasSubstr("warn: message\n"));
+        } else {
+            ASSERT_EQ(testing::internal::GetCapturedStderr(), "");
+        }
     }
 }

@@ -538,8 +545,12 @@
     for (int i = 0; i < 10; i++) {
         testing::internal::CaptureStderr();
         inform_once("message\n");
-        ASSERT_EQ(testing::internal::GetCapturedStderr(),
-            (i == 0) ? "info: message\n" : "");
+        if (i == 0) {
+            ASSERT_THAT(testing::internal::GetCapturedStderr(),
+                ::testing::HasSubstr("info: message\n"));
+        } else {
+            ASSERT_EQ(testing::internal::GetCapturedStderr(), "");
+        }
     }
 }

@@ -549,8 +560,12 @@
     for (int i = 0; i < 10; i++) {
         testing::internal::CaptureStderr();
         hack_once("message\n");
-        ASSERT_EQ(testing::internal::GetCapturedStderr(),
-            (i == 0) ? "hack: message\n" : "");
+        if (i == 0) {
+            ASSERT_THAT(testing::internal::GetCapturedStderr(),
+                ::testing::HasSubstr("hack: message\n"));
+        } else {
+            ASSERT_EQ(testing::internal::GetCapturedStderr(), "");
+        }
     }
 }

@@ -560,8 +575,12 @@
     for (int i = 0; i < 10; i++) {
         testing::internal::CaptureStderr();
         warn_if(i % 4, "message\n");
-        ASSERT_EQ(testing::internal::GetCapturedStderr(),
-            (i % 4) ? "warn: message\n" : "");
+        if (i % 4) {
+            ASSERT_THAT(testing::internal::GetCapturedStderr(),
+                ::testing::HasSubstr("warn: message\n"));
+        } else {
+            ASSERT_EQ(testing::internal::GetCapturedStderr(), "");
+        }
     }
 }

@@ -571,8 +590,12 @@
     for (int i = 0; i < 10; i++) {
         testing::internal::CaptureStderr();
         warn_if_once(i == 3, "message\n");
-        ASSERT_EQ(testing::internal::GetCapturedStderr(),
-            (i == 3) ? "warn: message\n" : "");
+        if (i == 3) {
+            ASSERT_THAT(testing::internal::GetCapturedStderr(),
+                ::testing::HasSubstr("warn: message\n"));
+        } else {
+            ASSERT_EQ(testing::internal::GetCapturedStderr(), "");
+        }
     }
 }

@@ -581,7 +604,7 @@
 TEST(LoggingDeathTest, ChattyAssert)
 {
     chatty_assert(true, "message\n");
-    ASSERT_DEATH(chatty_assert(false, "message\n"), ::testing::StartsWith(
+    ASSERT_DEATH(chatty_assert(false, "message\n"), ::testing::HasSubstr(
         "panic: assert(false) failed: message\nMemory Usage:"));
 }
 #endif

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/42141
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: I74e0637b2943049134bd3e9a4bc6cab3766591a9
Gerrit-Change-Number: 42141
Gerrit-PatchSet: 1
Gerrit-Owner: Daniel Carvalho <[email protected]>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to