cmSystemTools: Add callback for Stderr

Factor a common callback type out of StdoutCallback.  Add an equivalent
StderrCallback.  While at it, use "size_t" for the data length instead
of "int".

Teach "ctest --build-and-test" to capture the Stderr callback because
output sent through it is part of the logical CMake process output.
This commit is contained in:
Brad King 2014-05-15 09:44:25 -04:00
parent a9ae1d7a60
commit f52b5ae3c4
3 changed files with 41 additions and 23 deletions

View File

@ -156,7 +156,7 @@ void CMakeProgressCallback(const char*msg, float , void * s)
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void CMakeStdoutCallback(const char* m, int len, void* s) void CMakeOutputCallback(const char* m, size_t len, void* s)
{ {
std::string* out = (std::string*)s; std::string* out = (std::string*)s;
out->append(m, len); out->append(m, len);
@ -170,12 +170,14 @@ public:
cmCTestBuildAndTestCaptureRAII(cmake& cm, std::string& s): CM(cm) cmCTestBuildAndTestCaptureRAII(cmake& cm, std::string& s): CM(cm)
{ {
cmSystemTools::SetMessageCallback(CMakeMessageCallback, &s); cmSystemTools::SetMessageCallback(CMakeMessageCallback, &s);
cmSystemTools::SetStdoutCallback(CMakeStdoutCallback, &s); cmSystemTools::SetStdoutCallback(CMakeOutputCallback, &s);
cmSystemTools::SetStderrCallback(CMakeOutputCallback, &s);
this->CM.SetProgressCallback(CMakeProgressCallback, &s); this->CM.SetProgressCallback(CMakeProgressCallback, &s);
} }
~cmCTestBuildAndTestCaptureRAII() ~cmCTestBuildAndTestCaptureRAII()
{ {
this->CM.SetProgressCallback(0, 0); this->CM.SetProgressCallback(0, 0);
cmSystemTools::SetStderrCallback(0, 0);
cmSystemTools::SetStdoutCallback(0, 0); cmSystemTools::SetStdoutCallback(0, 0);
cmSystemTools::SetMessageCallback(0, 0); cmSystemTools::SetMessageCallback(0, 0);
} }

View File

@ -122,10 +122,12 @@ bool cmSystemTools::s_DisableMessages = false;
bool cmSystemTools::s_ForceUnixPaths = false; bool cmSystemTools::s_ForceUnixPaths = false;
cmSystemTools::MessageCallback cmSystemTools::s_MessageCallback; cmSystemTools::MessageCallback cmSystemTools::s_MessageCallback;
cmSystemTools::OutputCallback cmSystemTools::s_StdoutCallback;
cmSystemTools::OutputCallback cmSystemTools::s_StderrCallback;
cmSystemTools::InterruptCallback cmSystemTools::s_InterruptCallback; cmSystemTools::InterruptCallback cmSystemTools::s_InterruptCallback;
void (*cmSystemTools::s_StdoutCallback)(const char*, int len, void*);
void* cmSystemTools::s_MessageCallbackClientData; void* cmSystemTools::s_MessageCallbackClientData;
void* cmSystemTools::s_StdoutCallbackClientData = 0; void* cmSystemTools::s_StdoutCallbackClientData;
void* cmSystemTools::s_StderrCallbackClientData;
void* cmSystemTools::s_InterruptCallbackClientData; void* cmSystemTools::s_InterruptCallbackClientData;
// replace replace with with as many times as it shows up in source. // replace replace with with as many times as it shows up in source.
@ -259,33 +261,42 @@ void cmSystemTools::SetMessageCallback(MessageCallback f, void* clientData)
s_MessageCallbackClientData = clientData; s_MessageCallbackClientData = clientData;
} }
void cmSystemTools::SetStdoutCallback(StdoutCallback f, void* clientData) void cmSystemTools::SetStdoutCallback(OutputCallback f, void* clientData)
{ {
s_StdoutCallback = f; s_StdoutCallback = f;
s_StdoutCallbackClientData = clientData; s_StdoutCallbackClientData = clientData;
} }
void cmSystemTools::SetStderrCallback(OutputCallback f, void* clientData)
{
s_StderrCallback = f;
s_StderrCallbackClientData = clientData;
}
void cmSystemTools::Stdout(const char* s) void cmSystemTools::Stdout(const char* s)
{ {
if(s_StdoutCallback) cmSystemTools::Stdout(s, strlen(s));
}
void cmSystemTools::Stderr(const char* s)
{
cmSystemTools::Stderr(s, strlen(s));
}
void cmSystemTools::Stderr(const char* s, size_t length)
{
if(s_StderrCallback)
{ {
(*s_StdoutCallback)(s, static_cast<int>(strlen(s)), (*s_StderrCallback)(s, length, s_StderrCallbackClientData);
s_StdoutCallbackClientData);
} }
else else
{ {
std::cout << s; std::cerr.write(s, length);
std::cout.flush(); std::cerr.flush();
} }
} }
void cmSystemTools::Stderr(const char* s, int length) void cmSystemTools::Stdout(const char* s, size_t length)
{
std::cerr.write(s, length);
std::cerr.flush();
}
void cmSystemTools::Stdout(const char* s, int length)
{ {
if(s_StdoutCallback) if(s_StdoutCallback)
{ {

View File

@ -77,14 +77,17 @@ public:
*/ */
static void Message(const char* m, const char* title=0); static void Message(const char* m, const char* title=0);
typedef void (*OutputCallback)(const char*, size_t length, void*);
///! Send a string to stdout ///! Send a string to stdout
static void Stdout(const char* s); static void Stdout(const char* s);
static void Stdout(const char* s, int length); static void Stdout(const char* s, size_t length);
typedef void (*StdoutCallback)(const char*, int length, void*); static void SetStdoutCallback(OutputCallback, void* clientData=0);
static void SetStdoutCallback(StdoutCallback, void* clientData=0);
///! Send a string to stderr. Stdout callbacks will not be invoced. ///! Send a string to stderr
static void Stderr(const char* s, int length); static void Stderr(const char* s);
static void Stderr(const char* s, size_t length);
static void SetStderrCallback(OutputCallback, void* clientData=0);
typedef bool (*InterruptCallback)(void*); typedef bool (*InterruptCallback)(void*);
@ -471,10 +474,12 @@ private:
static bool s_DisableMessages; static bool s_DisableMessages;
static bool s_DisableRunCommandOutput; static bool s_DisableRunCommandOutput;
static MessageCallback s_MessageCallback; static MessageCallback s_MessageCallback;
static StdoutCallback s_StdoutCallback; static OutputCallback s_StdoutCallback;
static OutputCallback s_StderrCallback;
static InterruptCallback s_InterruptCallback; static InterruptCallback s_InterruptCallback;
static void* s_MessageCallbackClientData; static void* s_MessageCallbackClientData;
static void* s_StdoutCallbackClientData; static void* s_StdoutCallbackClientData;
static void* s_StderrCallbackClientData;
static void* s_InterruptCallbackClientData; static void* s_InterruptCallbackClientData;
}; };