mirror of
https://github.com/boostorg/assert.git
synced 2025-09-26 15:50:59 +02:00
Add operator<< for source_location
This commit is contained in:
@@ -10,6 +10,7 @@
|
|||||||
#include <boost/current_function.hpp>
|
#include <boost/current_function.hpp>
|
||||||
#include <boost/config.hpp>
|
#include <boost/config.hpp>
|
||||||
#include <boost/cstdint.hpp>
|
#include <boost/cstdint.hpp>
|
||||||
|
#include <iosfwd>
|
||||||
|
|
||||||
namespace boost
|
namespace boost
|
||||||
{
|
{
|
||||||
@@ -54,6 +55,29 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<class E, class T> std::basic_ostream<E, T> & operator<<( std::basic_ostream<E, T> & 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
|
} // namespace boost
|
||||||
|
|
||||||
#if defined( BOOST_DISABLE_CURRENT_LOCATION )
|
#if defined( BOOST_DISABLE_CURRENT_LOCATION )
|
||||||
|
@@ -37,3 +37,4 @@ run current_function_test2.cpp ;
|
|||||||
|
|
||||||
run source_location_test.cpp ;
|
run source_location_test.cpp ;
|
||||||
run source_location_test2.cpp ;
|
run source_location_test2.cpp ;
|
||||||
|
run source_location_test3.cpp ;
|
||||||
|
30
test/source_location_test3.cpp
Normal file
30
test/source_location_test3.cpp
Normal file
@@ -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 <boost/assert/source_location.hpp>
|
||||||
|
#include <boost/core/lightweight_test.hpp>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
Reference in New Issue
Block a user