cmCTestBuildAndTestHandler: Refactor output capture

Use an RAII class to add and remove callbacks.
This commit is contained in:
Brad King 2014-05-15 10:19:29 -04:00
parent b1b4d761a1
commit 7577a542df
1 changed files with 22 additions and 19 deletions

View File

@ -161,28 +161,29 @@ void CMakeStdoutCallback(const char* m, int len, void* s)
std::string* out = (std::string*)s; std::string* out = (std::string*)s;
out->append(m, len); out->append(m, len);
} }
struct cmSetupOutputCaptureCleanup
//----------------------------------------------------------------------
class cmCTestBuildAndTestCaptureRAII
{ {
~cmSetupOutputCaptureCleanup() cmake& CM;
{ public:
cmSystemTools::SetErrorCallback(0, 0); cmCTestBuildAndTestCaptureRAII(cmake& cm, std::string& s): CM(cm)
{
cmSystemTools::SetErrorCallback(CMakeMessageCallback, &s);
cmSystemTools::SetStdoutCallback(CMakeStdoutCallback, &s);
this->CM.SetProgressCallback(CMakeProgressCallback, &s);
}
~cmCTestBuildAndTestCaptureRAII()
{
this->CM.SetProgressCallback(0, 0);
cmSystemTools::SetStdoutCallback(0, 0); cmSystemTools::SetStdoutCallback(0, 0);
} cmSystemTools::SetErrorCallback(0, 0);
}
}; };
//---------------------------------------------------------------------- //----------------------------------------------------------------------
int cmCTestBuildAndTestHandler::RunCMakeAndTest(std::string* outstring) int cmCTestBuildAndTestHandler::RunCMakeAndTest(std::string* outstring)
{ {
std::string cmakeOutString;
cmSystemTools::SetErrorCallback(CMakeMessageCallback, &cmakeOutString);
cmSystemTools::SetStdoutCallback(CMakeStdoutCallback, &cmakeOutString);
// make sure SetStdoutCallback and SetErrorCallback are set to null
// after this function exits so that they do not point at a destroyed
// string cmakeOutString
cmSetupOutputCaptureCleanup cleanup;
static_cast<void>(cleanup);
cmOStringStream out;
// if the generator and make program are not specified then it is an error // if the generator and make program are not specified then it is an error
if (!this->BuildGenerator.size()) if (!this->BuildGenerator.size())
{ {
@ -196,6 +197,12 @@ int cmCTestBuildAndTestHandler::RunCMakeAndTest(std::string* outstring)
return 1; return 1;
} }
cmake cm;
std::string cmakeOutString;
cmCTestBuildAndTestCaptureRAII captureRAII(cm, cmakeOutString);
static_cast<void>(captureRAII);
cmOStringStream out;
if ( this->CTest->GetConfigType().size() == 0 && if ( this->CTest->GetConfigType().size() == 0 &&
this->ConfigSample.size()) this->ConfigSample.size())
{ {
@ -232,10 +239,6 @@ int cmCTestBuildAndTestHandler::RunCMakeAndTest(std::string* outstring)
} }
cmSystemTools::ChangeDirectory(this->BinaryDir.c_str()); cmSystemTools::ChangeDirectory(this->BinaryDir.c_str());
// should we cmake?
cmake cm;
cm.SetProgressCallback(CMakeProgressCallback, &cmakeOutString);
if(this->BuildNoCMake) if(this->BuildNoCMake)
{ {
// Make the generator available for the Build call below. // Make the generator available for the Build call below.