lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master 6fbb81b: Shorten diagnostics presented to end


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 6fbb81b: Shorten diagnostics presented to end users
Date: Thu, 26 Apr 2018 14:10:43 -0400 (EDT)

branch: master
commit 6fbb81bd4c640bd47a3ab7543fa79b9a8aef5e03
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>

    Shorten diagnostics presented to end users
    
    In the separate line appended to most diagnostics presented to end
    users, replaced (e.g.)
      [file /opt/lmi/src/lmi/foo.cpp, line 1234]
    with
      [foo.cpp : 1234]
    Accordingly adapted unit tests that filter out that added line to avoid
    embrittlement (e.g., adding a comment to a unit-tested file can change
    the line number); documented that filtering '\n[' instead of '\n[file'
    is reasonable because diagnostics seen by end users needn't (and in lmi
    don't) otherwise contain any line beginning with '['.
    
    Did not similarly alter diagnostics that the unit-testing framework
    displays: developers generally prefer more information, and might even
    find it helpful that the format differs.
---
 alert.hpp               |  7 ++++---
 assert_lmi.hpp          | 35 ++++++++++++++++++-----------------
 assert_lmi_test.cpp     |  2 +-
 input_sequence_test.cpp |  2 +-
 test_tools.hpp          |  5 +++--
 5 files changed, 27 insertions(+), 24 deletions(-)

diff --git a/alert.hpp b/alert.hpp
index 114d494..3f2a0cc 100644
--- a/alert.hpp
+++ b/alert.hpp
@@ -29,6 +29,7 @@
 // Instead of <iosfwd>, include <ostream> to make std::flush available
 // to modules that use the LMI_FLUSH macro.
 
+#include <cstring>
 #include <exception>
 #include <ostream>
 #include <string>
@@ -252,9 +253,9 @@ void LMI_SO test_catastrophe_report();
 
 /// Write file name and line number to diagnostic stream, and flush.
 
-#define LMI_FLUSH                     \
-       "\n[file " << __FILE__         \
-    << ", line " << __LINE__ << "]\n" \
+#define LMI_FLUSH                                   \
+       "\n[" << 1 + std::strrchr("/" __FILE__, '/') \
+    << " : " << __LINE__ << "]\n"                   \
     << std::flush
 
 #endif // alert_hpp
diff --git a/assert_lmi.hpp b/assert_lmi.hpp
index 7f852a8..beb0482 100644
--- a/assert_lmi.hpp
+++ b/assert_lmi.hpp
@@ -24,6 +24,7 @@
 
 #include "config.hpp"
 
+#include <cstring>
 #include <sstream>
 #include <stdexcept>
 
@@ -49,8 +50,8 @@
             std::ostringstream assert_message;                  \
             assert_message                                      \
                 << "Assertion '" << (#condition) << "' failed." \
-                << "\n[file " << __FILE__                       \
-                << ", line " << __LINE__ << "]\n"               \
+                << "\n[" << 1 + std::strrchr("/" __FILE__, '/') \
+                << " : " << __LINE__ << "]\n"                   \
                 ;                                               \
             throw std::runtime_error(assert_message.str());     \
             }                                                   \
@@ -63,21 +64,21 @@
 /// that it can include streaming operators, e.g.:
 ///   "value is " << value
 
-#define LMI_ASSERT_WITH_MSG(condition,message)                 \
-    do                                                         \
-        {                                                      \
-        if(!(condition))                                       \
-            {                                                  \
-            std::ostringstream assert_message;                 \
-            assert_message                                     \
-                << "Assertion '" << (#condition) << "' failed" \
-                << "\n(" << message << ")."                    \
-                << "\n[file " << __FILE__                      \
-                << ", line " << __LINE__ << "]\n"              \
-                ;                                              \
-            throw std::runtime_error(assert_message.str());    \
-            }                                                  \
-        }                                                      \
+#define LMI_ASSERT_WITH_MSG(condition,message)                  \
+    do                                                          \
+        {                                                       \
+        if(!(condition))                                        \
+            {                                                   \
+            std::ostringstream assert_message;                  \
+            assert_message                                      \
+                << "Assertion '" << (#condition) << "' failed"  \
+                << "\n(" << message << ")."                     \
+                << "\n[" << 1 + std::strrchr("/" __FILE__, '/') \
+                << " : " << __LINE__ << "]\n"                   \
+                ;                                               \
+            throw std::runtime_error(assert_message.str());     \
+            }                                                   \
+        }                                                       \
     while(0)
 
 /// This LMI_ASSERT variant displays both its parameters if unequal.
diff --git a/assert_lmi_test.cpp b/assert_lmi_test.cpp
index 3395c3a..32f5156 100644
--- a/assert_lmi_test.cpp
+++ b/assert_lmi_test.cpp
@@ -29,7 +29,7 @@
 #define DOUBLY_STRINGIFY(Z) SINGLY_STRINGIFY(Z)
 
 #define LMI_LOCATION \
-    "\n[file " __FILE__ ", line " DOUBLY_STRINGIFY(__LINE__) "]\n"
+    "\n[assert_lmi_test.cpp : " DOUBLY_STRINGIFY(__LINE__) "]\n"
 
 int test_main(int, char*[])
 {
diff --git a/input_sequence_test.cpp b/input_sequence_test.cpp
index 627f6fa..2b18e1a 100644
--- a/input_sequence_test.cpp
+++ b/input_sequence_test.cpp
@@ -124,7 +124,7 @@ void input_sequence_test::check
     catch(std::exception const& x)
         {
         std::string const y = x.what();
-        std::string const i = y.substr(0, y.find("\n[file "));
+        std::string const i = y.substr(0, y.find("\n["));
         std::string const j = (nullptr == m) ? std::string() : std::string(m);
         bool const b = i == j;
         if(!b)
diff --git a/test_tools.hpp b/test_tools.hpp
index cdb7e1f..d7f1ab4 100644
--- a/test_tools.hpp
+++ b/test_tools.hpp
@@ -123,14 +123,15 @@ namespace lmi_test
 ///   - 'WHAT' matches the actual exception's what() up to but not
 ///      including any lmi exception-location string. (Some lmi
 ///      exceptions add a newline and the file name and line number,
-///      always beginning "\n[file "--cf. LMI_FLUSH).
+///      always beginning "\n["--cf. LMI_FLUSH--which sequence is
+///      assumed otherwise not to occur in what().)
 
 inline bool whats_what(std::string const& observed, std::string const& 
expected)
 {
     return
            expected.empty()
         || observed == expected
-        || 0 == observed.compare(0, observed.find("\n[file "), expected)
+        || 0 == observed.compare(0, observed.find("\n["), expected)
         ;
 }
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]