diff --git a/include/boost/assert/source_location.hpp b/include/boost/assert/source_location.hpp index c6c3e78..fdf943e 100644 --- a/include/boost/assert/source_location.hpp +++ b/include/boost/assert/source_location.hpp @@ -1,16 +1,17 @@ #ifndef BOOST_ASSERT_SOURCE_LOCATION_HPP_INCLUDED #define BOOST_ASSERT_SOURCE_LOCATION_HPP_INCLUDED -// http://www.boost.org/libs/assert +// http://www.boost.org/libs/assert // -// Copyright 2019 Peter Dimov -// Distributed under the Boost Software License, Version 1.0. -// http://www.boost.org/LICENSE_1_0.txt +// Copyright 2019, 2021 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// http://www.boost.org/LICENSE_1_0.txt #include #include #include #include +#include namespace boost { @@ -53,28 +54,48 @@ public: { return column_; } + +#if defined(BOOST_MSVC) +# pragma warning( push ) +# pragma warning( disable: 4996 ) +#endif + + std::string to_string() const + { + if( line() == 0 ) + { + return "(unknown source location)"; + } + + std::string r = file_name(); + + char buffer[ 16 ]; + + sprintf( buffer, ":%ld", static_cast( line() ) ); + r += buffer; + + if( column() ) + { + sprintf( buffer, ":%ld", static_cast( column() ) ); + r += buffer; + } + + r += " in function '"; + r += function_name(); + r += '\''; + + return r; + } + +#if defined(BOOST_MSVC) +# pragma warning( pop ) +#endif + }; template std::basic_ostream & operator<<( std::basic_ostream & os, source_location const & loc ) { - os.width( 0 ); - - if( loc.line() == 0 ) - { - os << "(unknown source location)"; - } - else - { - os << loc.file_name() << ':' << loc.line(); - - if( loc.column() ) - { - os << ':' << loc.column(); - } - - os << ": in function '" << loc.function_name() << '\''; - } - + os << loc.to_string(); return os; } diff --git a/test/source_location_test3.cpp b/test/source_location_test3.cpp index 72a2289..f2247cc 100644 --- a/test/source_location_test3.cpp +++ b/test/source_location_test3.cpp @@ -8,6 +8,11 @@ int main() { + { + boost::source_location loc; + BOOST_TEST_EQ( loc.to_string(), std::string( "(unknown source location)" ) ); + } + { boost::source_location loc; @@ -17,13 +22,18 @@ int main() BOOST_TEST_EQ( os.str(), std::string( "(unknown source location)" ) ); } + { + boost::source_location loc = BOOST_CURRENT_LOCATION; + BOOST_TEST_EQ( loc.to_string(), std::string( __FILE__ ) + ":26 in function '" + BOOST_CURRENT_FUNCTION + "'" ); + } + { boost::source_location loc = BOOST_CURRENT_LOCATION; std::ostringstream os; os << loc; - BOOST_TEST_EQ( os.str(), std::string( __FILE__ ) + ":21: in function '" + BOOST_CURRENT_FUNCTION + "'" ); + BOOST_TEST_EQ( os.str(), std::string( __FILE__ ) + ":31 in function '" + BOOST_CURRENT_FUNCTION + "'" ); } return boost::report_errors();