From e52b379928ad3e1c52a07481037885f0d12bdfd3 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Tue, 21 Jan 2020 01:52:41 +0200 Subject: [PATCH] Add operator<< for source_location --- include/boost/assert/source_location.hpp | 24 +++++++++++++++++++ test/Jamfile.v2 | 1 + test/source_location_test3.cpp | 30 ++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 test/source_location_test3.cpp diff --git a/include/boost/assert/source_location.hpp b/include/boost/assert/source_location.hpp index 422f668..a85e679 100644 --- a/include/boost/assert/source_location.hpp +++ b/include/boost/assert/source_location.hpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace boost { @@ -54,6 +55,29 @@ public: } }; +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() << '\''; + } + + return os; +} + } // namespace boost #if defined( BOOST_DISABLE_CURRENT_LOCATION ) diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 8c1ca97..0e935b6 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -37,3 +37,4 @@ run current_function_test2.cpp ; run source_location_test.cpp ; run source_location_test2.cpp ; +run source_location_test3.cpp ; diff --git a/test/source_location_test3.cpp b/test/source_location_test3.cpp new file mode 100644 index 0000000..72a2289 --- /dev/null +++ b/test/source_location_test3.cpp @@ -0,0 +1,30 @@ +// Copyright 2020 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// http://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include + +int main() +{ + { + boost::source_location loc; + + std::ostringstream os; + os << loc; + + BOOST_TEST_EQ( os.str(), std::string( "(unknown source location)" ) ); + } + + { + 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 + "'" ); + } + + return boost::report_errors(); +}