From 5f10a8e0e362b09bf826b90f61112cdd850e3d65 Mon Sep 17 00:00:00 2001 From: Emil Dotchevski Date: Sun, 1 Sep 2024 19:50:49 -0700 Subject: [PATCH] Added all configurations to diagnostics_test1.cpp --- include/boost/leaf/diagnostics.hpp | 6 +- include/boost/leaf/handle_errors.hpp | 4 +- test/diagnostics_test1.cpp | 210 +++++++++++++++++++-------- 3 files changed, 157 insertions(+), 63 deletions(-) diff --git a/include/boost/leaf/diagnostics.hpp b/include/boost/leaf/diagnostics.hpp index 492d66dc..69d95c0b 100644 --- a/include/boost/leaf/diagnostics.hpp +++ b/include/boost/leaf/diagnostics.hpp @@ -85,7 +85,7 @@ class diagnostic_info: public error_info template friend std::ostream & operator<<( std::basic_ostream & os, diagnostic_info const & x ) { - return os << "diagnostic_info not available due to BOOST_LEAF_CFG_DIAGNOSTICS=0.\n" << static_cast(x); + return os << static_cast(x) << "boost::leaf::diagnostic_info N/A due to BOOST_LEAF_CFG_DIAGNOSTICS=0\n"; } }; @@ -216,7 +216,7 @@ class diagnostic_details: public diagnostic_info template friend std::ostream & operator<<( std::basic_ostream & os, diagnostic_details const & x ) { - return os << "diagnostic_details not available due to BOOST_LEAF_CFG_CAPTURE=0. Basic diagnostic_info follows.\n" << static_cast(x); + return os << static_cast(x) << "boost::leaf::diagnostic_details N/A due to BOOST_LEAF_CFG_CAPTURE=0\n"; } }; @@ -260,7 +260,7 @@ class diagnostic_details: public diagnostic_info template friend std::ostream & operator<<( std::basic_ostream & os, diagnostic_details const & x ) { - return os << "diagnostic_details not available due to BOOST_LEAF_CFG_DIAGNOSTICS=0. Basic error_info follows.\n" << static_cast(x); + return os << "boost::leaf::diagnostic_details N/A due to BOOST_LEAF_CFG_DIAGNOSTICS=0\n"; } }; diff --git a/include/boost/leaf/handle_errors.hpp b/include/boost/leaf/handle_errors.hpp index d3e245bd..891bf6af 100644 --- a/include/boost/leaf/handle_errors.hpp +++ b/include/boost/leaf/handle_errors.hpp @@ -73,13 +73,15 @@ class BOOST_LEAF_SYMBOL_VISIBLE error_info void print(std::basic_ostream & os) const { os << "Error serial #" << err_id_; -#if BOOST_LEAF_CFG_DIAGNOSTICS && !defined(BOOST_LEAF_NO_EXCEPTIONS) +#ifndef BOOST_LEAF_NO_EXCEPTIONS if( ex_ ) { os << "\nCaught C++ exception:\n\tType: "; +#if BOOST_LEAF_CFG_DIAGNOSTICS if( auto eb = dynamic_cast(ex_) ) eb->print_type_name(os); else +#endif leaf_detail::demangle_and_print(os, typeid(*ex_).name()); os << "\n\tstd::exception::what(): " << ex_->what(); } diff --git a/test/diagnostics_test1.cpp b/test/diagnostics_test1.cpp index 18719f85..f0ca6c47 100644 --- a/test/diagnostics_test1.cpp +++ b/test/diagnostics_test1.cpp @@ -112,25 +112,29 @@ template <> struct show_in_diagnostics"); - return s; + return; + s.replace(s1 + name.size(), s2 - s1 - name.size(), ""); + } + + void remove_variance(std::string & s) + { + s.erase(std::remove(s.begin(), s.end(), '\r'), s.end()); + remove_value(s, "boost::leaf::e_source_location: "); + remove_value(s, "Caught C++ exception:\n\tType: "); } - bool cmp(std::string a, std::string b) noexcept + bool cmp(std::string a, std::string const & b) noexcept { - a = remove_variance(a); - b = remove_variance(b); - auto i = a.begin(), j = b.begin(); + remove_variance(a); + auto i = a.begin(); + auto j = b.begin(); for( auto e = i + std::min(a.size(), b.size()); i != e; ++i, ++j ) if( *i != *j ) { @@ -142,6 +146,10 @@ namespace } #endif +#define BOOST_LEAF_DIAGNOSTIC_INFO_NO_BOOST_LEAF_CFG_CAPTURE "boost::leaf::diagnostic_info N/A due to BOOST_LEAF_CFG_DIAGNOSTICS=0\n" +#define BOOST_LEAF_DIAGNOSTIC_DETAILS_NO_BOOST_LEAF_CFG_CAPTURE "boost::leaf::diagnostic_details N/A due to BOOST_LEAF_CFG_CAPTURE=0\n" +#define BOOST_LEAF_DIAGNOSTIC_DETAILS_NO_BOOST_LEAF_CFG_DIAGNOSTICS "boost::leaf::diagnostic_details N/A due to BOOST_LEAF_CFG_DIAGNOSTICS=0\n" + int main() { std::cout << __LINE__ << " ---- result / error_info\n"; @@ -229,6 +237,11 @@ int main() "\n\tenum_class: 1" "\n\tboost::leaf::e_errno: 2, \"No such file or directory\"\n" )); + else + BOOST_TEST(cmp(s, + "Error serial #2\n" + BOOST_LEAF_DIAGNOSTIC_INFO_NO_BOOST_LEAF_CFG_CAPTURE + )); #endif }, []() @@ -270,19 +283,38 @@ int main() std::string s = st.str(); std::cout << s << std::endl; if( BOOST_LEAF_CFG_DIAGNOSTICS ) + if( BOOST_LEAF_CFG_CAPTURE ) + BOOST_TEST(cmp(s, + "Error serial #3\n" + "Caught:" + "\n\tboost::leaf::e_source_location: " + "\n\tprintable_info_printable_value: printed printable_value" + "\n\tprintable_info_non_printable_value: *** printable_info non_printable_value ***" + "\n\tnon_printable_info_printable_value: printed printable_value" + "\n\tnon_printable_info_non_printable_value" + "\n\tenum_class: 0" + "\n\tboost::leaf::e_errno: 2, \"No such file or directory\"\n" + "Diagnostic details:" + "\n\tunexpected_test<1>: 1" + "\n\tunexpected_test<2>: 2\n" + )); + else + BOOST_TEST(cmp(s, + "Error serial #3\n" + "Caught:" + "\n\tboost::leaf::e_source_location: " + "\n\tprintable_info_printable_value: printed printable_value" + "\n\tprintable_info_non_printable_value: *** printable_info non_printable_value ***" + "\n\tnon_printable_info_printable_value: printed printable_value" + "\n\tnon_printable_info_non_printable_value" + "\n\tenum_class: 0" + "\n\tboost::leaf::e_errno: 2, \"No such file or directory\"\n" + BOOST_LEAF_DIAGNOSTIC_DETAILS_NO_BOOST_LEAF_CFG_CAPTURE + )); + else BOOST_TEST(cmp(s, "Error serial #3\n" - "Caught:" - "\n\tboost::leaf::e_source_location: " - "\n\tprintable_info_printable_value: printed printable_value" - "\n\tprintable_info_non_printable_value: *** printable_info non_printable_value ***" - "\n\tnon_printable_info_printable_value: printed printable_value" - "\n\tnon_printable_info_non_printable_value" - "\n\tenum_class: 0" - "\n\tboost::leaf::e_errno: 2, \"No such file or directory\"\n" - "Diagnostic details:" - "\n\tunexpected_test<1>: 1" - "\n\tunexpected_test<2>: 2\n" + BOOST_LEAF_DIAGNOSTIC_DETAILS_NO_BOOST_LEAF_CFG_DIAGNOSTICS )); #endif }, @@ -315,18 +347,29 @@ int main() std::string s = st.str(); std::cout << s << std::endl; if( BOOST_LEAF_CFG_DIAGNOSTICS ) + if( BOOST_LEAF_CFG_CAPTURE ) + BOOST_TEST(cmp(s, + "Error serial #4\n" + "Diagnostic details:" + "\n\tprintable_info_printable_value: printed printable_value" + "\n\tprintable_info_non_printable_value: *** printable_info non_printable_value ***" + "\n\tnon_printable_info_printable_value: printed printable_value" + "\n\tnon_printable_info_non_printable_value" + "\n\tunexpected_test<1>: 1" + "\n\tunexpected_test<2>: 2" + "\n\tenum_class: 0" + "\n\tboost::leaf::e_errno: 2, \"No such file or directory\"" + "\n\tboost::leaf::e_source_location: " + )); + else + BOOST_TEST(cmp(s, + "Error serial #4\n" + BOOST_LEAF_DIAGNOSTIC_INFO_NO_BOOST_LEAF_CFG_CAPTURE + )); + else BOOST_TEST(cmp(s, "Error serial #4\n" - "Diagnostic details:" - "\n\tprintable_info_printable_value: printed printable_value" - "\n\tprintable_info_non_printable_value: *** printable_info non_printable_value ***" - "\n\tnon_printable_info_printable_value: printed printable_value" - "\n\tnon_printable_info_non_printable_value" - "\n\tunexpected_test<1>: 1" - "\n\tunexpected_test<2>: 2" - "\n\tenum_class: 0" - "\n\tboost::leaf::e_errno: 2, \"No such file or directory\"" - "\n\tboost::leaf::e_source_location: " + BOOST_LEAF_DIAGNOSTIC_DETAILS_NO_BOOST_LEAF_CFG_DIAGNOSTICS )); #endif }, @@ -373,7 +416,7 @@ int main() BOOST_TEST(cmp(s, "Error serial #5\n" "Caught C++ exception:" - "\n\tType: my_exception" + "\n\tType: " "\n\tstd::exception::what(): my_exception what\n" )); #endif @@ -414,7 +457,7 @@ int main() BOOST_TEST(cmp(s, "Error serial #7\n" "Caught C++ exception:" - "\n\tType: my_exception" + "\n\tType: " "\n\tstd::exception::what(): my_exception what" "\n\tboost::leaf::e_source_location: " "\n\tprintable_info_printable_value: printed printable_value" @@ -424,6 +467,14 @@ int main() "\n\tenum_class: 0" "\n\tboost::leaf::e_errno: 2, \"No such file or directory\"\n" )); + else + BOOST_TEST(cmp(s, + "Error serial #7\n" + "Caught C++ exception:" + "\n\tType: " + "\n\tstd::exception::what(): my_exception what" + BOOST_LEAF_DIAGNOSTIC_INFO_NO_BOOST_LEAF_CFG_CAPTURE + )); #endif } ); @@ -461,21 +512,45 @@ int main() std::string s = st.str(); std::cout << s << std::endl; if( BOOST_LEAF_CFG_DIAGNOSTICS ) + if( BOOST_LEAF_CFG_CAPTURE ) + BOOST_TEST(cmp(s, + "Error serial #9\n" + "Caught C++ exception:" + "\n\tType: " + "\n\tstd::exception::what(): my_exception what" + "\n\tboost::leaf::e_source_location: " + "\n\tprintable_info_printable_value: printed printable_value" + "\n\tprintable_info_non_printable_value: *** printable_info non_printable_value ***" + "\n\tnon_printable_info_printable_value: printed printable_value" + "\n\tnon_printable_info_non_printable_value" + "\n\tenum_class: 1" + "\n\tboost::leaf::e_errno: 2, \"No such file or directory\"\n" + "Diagnostic details:" + "\n\tunexpected_test<1>: 1" + "\n\tunexpected_test<2>: 2\n" + )); + else + BOOST_TEST(cmp(s, + "Error serial #9\n" + "Caught C++ exception:" + "\n\tType: " + "\n\tstd::exception::what(): my_exception what" + "\n\tboost::leaf::e_source_location: " + "\n\tprintable_info_printable_value: printed printable_value" + "\n\tprintable_info_non_printable_value: *** printable_info non_printable_value ***" + "\n\tnon_printable_info_printable_value: printed printable_value" + "\n\tnon_printable_info_non_printable_value" + "\n\tenum_class: 1" + "\n\tboost::leaf::e_errno: 2, \"No such file or directory\"\n" + BOOST_LEAF_DIAGNOSTIC_DETAILS_NO_BOOST_LEAF_CFG_CAPTURE + )); + else BOOST_TEST(cmp(s, "Error serial #9\n" "Caught C++ exception:" - "\n\tType: my_exception" + "\n\tType: " "\n\tstd::exception::what(): my_exception what" - "\n\tboost::leaf::e_source_location: " - "\n\tprintable_info_printable_value: printed printable_value" - "\n\tprintable_info_non_printable_value: *** printable_info non_printable_value ***" - "\n\tnon_printable_info_printable_value: printed printable_value" - "\n\tnon_printable_info_non_printable_value" - "\n\tenum_class: 1" - "\n\tboost::leaf::e_errno: 2, \"No such file or directory\"\n" - "Diagnostic details:" - "\n\tunexpected_test<1>: 1" - "\n\tunexpected_test<2>: 2\n" + BOOST_LEAF_DIAGNOSTIC_DETAILS_NO_BOOST_LEAF_CFG_DIAGNOSTICS )); #endif } ); @@ -504,21 +579,38 @@ int main() std::string s = st.str(); std::cout << s << std::endl; if( BOOST_LEAF_CFG_DIAGNOSTICS ) + if( BOOST_LEAF_CFG_CAPTURE ) + BOOST_TEST(cmp(s, + "Error serial #11\n" + "Caught C++ exception:" + "\n\tType: " + "\n\tstd::exception::what(): my_exception what\n" + "Diagnostic details:" + "\n\tprintable_info_printable_value: printed printable_value" + "\n\tprintable_info_non_printable_value: *** printable_info non_printable_value ***" + "\n\tnon_printable_info_printable_value: printed printable_value" + "\n\tnon_printable_info_non_printable_value" + "\n\tenum_class: 1" + "\n\tunexpected_test<1>: 1" + "\n\tunexpected_test<2>: 2" + "\n\tboost::leaf::e_errno: 2, \"No such file or directory\"" + "\n\tboost::leaf::e_source_location: " + )); + else + BOOST_TEST(cmp(s, + "Error serial #11\n" + "Caught C++ exception:" + "\n\tType: " + "\n\tstd::exception::what(): my_exception what" + BOOST_LEAF_DIAGNOSTIC_DETAILS_NO_BOOST_LEAF_CFG_CAPTURE + )); + else BOOST_TEST(cmp(s, "Error serial #11\n" "Caught C++ exception:" - "\n\tType: my_exception" - "\n\tstd::exception::what(): my_exception what\n" - "Diagnostic details:" - "\n\tprintable_info_printable_value: printed printable_value" - "\n\tprintable_info_non_printable_value: *** printable_info non_printable_value ***" - "\n\tnon_printable_info_printable_value: printed printable_value" - "\n\tnon_printable_info_non_printable_value" - "\n\tenum_class: 1" - "\n\tunexpected_test<1>: 1" - "\n\tunexpected_test<2>: 2" - "\n\tboost::leaf::e_errno: 2, \"No such file or directory\"" - "\n\tboost::leaf::e_source_location: " + "\n\tType: " + "\n\tstd::exception::what(): my_exception what" + BOOST_LEAF_DIAGNOSTIC_DETAILS_NO_BOOST_LEAF_CFG_DIAGNOSTICS )); #endif } );