Voici le code avec une fonction simple permettant de rechercher dans le flux d'erreur redirigé.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 
#include <iostream>
#include <string>
using namespace std;
using namespace boost;
 
streambuf* errorBuffer = NULL;
stringbuf* errorBufTemp = NULL;
string outputErrorString;
 
void Redirect_cerr_Begin()
{
	if ( ! errorBufTemp )
	{
		// create temp string error buffer and assign to cerr stream
		outputErrorString = "";
		errorBufTemp = new stringbuf( ios_base::out );
		errorBuffer = cerr.rdbuf( errorBufTemp );
	}
}
 
void Redirect_cerr_End()
{
	if ( errorBufTemp )
	{
		// copy error stream to errorBufTemp
		outputErrorString = errorBufTemp->str();
 
		// destroy temp string error buffer
		cerr.rdbuf( errorBuffer );
		delete errorBufTemp;
		errorBufTemp = NULL;
	}
}
 
bool Find_error( string errorStr )
{
	size_t foundPos = string::npos;
	foundPos = outputErrorString.find( errorStr );
 
	return ( foundPos != string::npos );
}
voici un test

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
#include <boost/test/unit_test.hpp>
#include <boost/test/auto_unit_test.hpp>
#include <iostream>
#include <string>
using namespace std;
using namespace boost;
 
BOOST_AUTO_TEST_CASE( ErrorStreamCase )
{
	Redirect_cerr_Begin();
	{
		cerr << "Envoi d'un message dans le flux d'erreur\nLe message est celui-ci\n";
		cerr << "Message caché";
	}
	Redirect_cerr_End();
 
	BOOST_CHECK_EQUAL( Find_error( "Le message est"), 1 );
	BOOST_CHECK_EQUAL( Find_error( "Le message n'est pas"), 0 );
}