From 2703d51b8f382f3d20abe409b116eb0afd5051ff Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 5 Jan 2009 14:14:10 -0500 Subject: [PATCH] BUG: Capture cout and cerr from internal ctest When CTest detects that a test is running its own executable it optimizes the test by using an internal instance of cmCTest instead of creating a new process. However, the internal instance was using cout and cerr directly. This redirects the output to a string stream to avoid direct display of the internal test's output. --- Source/cmCTest.cxx | 52 ++++++++++++++++++++++++++++++---------------- Source/cmCTest.h | 8 +++++++ 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/Source/cmCTest.cxx b/Source/cmCTest.cxx index 78b11cf2b..15f117889 100644 --- a/Source/cmCTest.cxx +++ b/Source/cmCTest.cxx @@ -258,6 +258,7 @@ cmCTest::cmCTest() this->OutputLogFileLastTag = -1; this->SuppressUpdatingCTestConfiguration = false; this->DartVersion = 1; + this->InitStreams(); int cc; for ( cc=0; cc < cmCTest::LAST_TEST; cc ++ ) @@ -1136,6 +1137,11 @@ int cmCTest::RunTest(std::vector argv, cmCTest inst; inst.ConfigType = this->ConfigType; inst.TimeOut = timeout; + + // Capture output of the child ctest. + cmOStringStream oss; + inst.SetStreams(&oss, &oss); + std::vector args; for(unsigned int i =0; i < argv.size(); ++i) { @@ -1166,6 +1172,7 @@ int cmCTest::RunTest(std::vector argv, } *retVal = inst.Run(args, output); + *output += oss.str(); if ( log ) { *log << output->c_str(); @@ -2602,6 +2609,13 @@ static const char* cmCTestStringLogType[] = (stream) << std::endl << file << ":" << line << " "; \ } +void cmCTest::InitStreams() +{ + // By default we write output to the process output streams. + this->StreamOut = &std::cout; + this->StreamErr = &std::cerr; +} + void cmCTest::Log(int logType, const char* file, int line, const char* msg) { if ( !msg || !*msg ) @@ -2640,47 +2654,49 @@ void cmCTest::Log(int logType, const char* file, int line, const char* msg) } if ( !this->Quiet ) { + std::ostream& out = *this->StreamOut; + std::ostream& err = *this->StreamErr; switch ( logType ) { case DEBUG: if ( this->Debug ) { - cmCTestLogOutputFileLine(std::cout); - std::cout << msg; - std::cout.flush(); + cmCTestLogOutputFileLine(out); + out << msg; + out.flush(); } break; case OUTPUT: case HANDLER_OUTPUT: if ( this->Debug || this->Verbose ) { - cmCTestLogOutputFileLine(std::cout); - std::cout << msg; - std::cout.flush(); + cmCTestLogOutputFileLine(out); + out << msg; + out.flush(); } break; case HANDLER_VERBOSE_OUTPUT: if ( this->Debug || this->ExtraVerbose ) { - cmCTestLogOutputFileLine(std::cout); - std::cout << msg; - std::cout.flush(); + cmCTestLogOutputFileLine(out); + out << msg; + out.flush(); } break; case WARNING: - cmCTestLogOutputFileLine(std::cerr); - std::cerr << msg; - std::cerr.flush(); + cmCTestLogOutputFileLine(err); + err << msg; + err.flush(); break; case ERROR_MESSAGE: - cmCTestLogOutputFileLine(std::cerr); - std::cerr << msg; - std::cerr.flush(); + cmCTestLogOutputFileLine(err); + err << msg; + err.flush(); cmSystemTools::SetErrorOccured(); break; default: - cmCTestLogOutputFileLine(std::cout); - std::cout << msg; - std::cout.flush(); + cmCTestLogOutputFileLine(out); + out << msg; + out.flush(); } } } diff --git a/Source/cmCTest.h b/Source/cmCTest.h index a9bdb62c7..10a7d5b09 100644 --- a/Source/cmCTest.h +++ b/Source/cmCTest.h @@ -331,6 +331,10 @@ public: bool GetVerbose() { return this->Verbose;} bool GetExtraVerbose() { return this->ExtraVerbose;} + + /** Direct process output to given streams. */ + void SetStreams(std::ostream* out, std::ostream* err) + { this->StreamOut = out; this->StreamErr = err; } private: std::string ConfigType; bool Verbose; @@ -402,6 +406,10 @@ private: bool CompressXMLFiles; + void InitStreams(); + std::ostream* StreamOut; + std::ostream* StreamErr; + void BlockTestErrorDiagnostics();