2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
// CommandOutput.h
|
|
|
|
|
|
|
|
// Declares various classes that process command output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Interface for a callback that receives command output
|
|
|
|
The Out() function is called for any output the command has produced.
|
|
|
|
Descendants override that function to provide specific processing of the output.
|
|
|
|
*/
|
|
|
|
class cCommandOutputCallback
|
|
|
|
{
|
|
|
|
public:
|
2014-07-22 18:36:13 -04:00
|
|
|
virtual ~cCommandOutputCallback() {} // Force a virtual destructor in subclasses
|
2013-07-29 07:13:03 -04:00
|
|
|
|
2014-07-22 18:36:13 -04:00
|
|
|
/// Syntax sugar function, calls Out() with Printf()-ed parameters; appends a newline"
|
2014-03-14 09:11:49 -04:00
|
|
|
void Out(const char * a_Fmt, ...) FORMATSTRING(2, 3);
|
2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
/// Called when the command wants to output anything; may be called multiple times
|
|
|
|
virtual void Out(const AString & a_Text) = 0;
|
|
|
|
|
|
|
|
/// Called when the command processing has been finished
|
2014-07-22 18:36:13 -04:00
|
|
|
virtual void Finished(void) {}
|
2013-07-29 07:13:03 -04:00
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Class that discards all command output
|
|
|
|
class cNullCommandOutputCallback :
|
|
|
|
public cCommandOutputCallback
|
|
|
|
{
|
|
|
|
// cCommandOutputCallback overrides:
|
2013-12-22 08:46:55 -05:00
|
|
|
virtual void Out(const AString & a_Text) override
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
// Do nothing
|
2013-12-22 08:46:55 -05:00
|
|
|
UNUSED(a_Text);
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-05-10 16:51:16 -04:00
|
|
|
/** Accumulates all command output into a string. */
|
|
|
|
class cStringAccumCommandOutputCallback:
|
2013-07-29 07:13:03 -04:00
|
|
|
public cCommandOutputCallback
|
|
|
|
{
|
2015-05-10 16:51:16 -04:00
|
|
|
typedef cCommandOutputCallback super;
|
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
public:
|
|
|
|
// cCommandOutputCallback overrides:
|
|
|
|
virtual void Out(const AString & a_Text) override;
|
2015-05-10 16:51:16 -04:00
|
|
|
virtual void Finished(void) override {}
|
|
|
|
|
|
|
|
/** Returns the accumulated command output in a string. */
|
|
|
|
const AString & GetAccum(void) const { return m_Accum; }
|
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
protected:
|
2015-05-10 16:51:16 -04:00
|
|
|
/** Output is stored here until the command finishes processing */
|
|
|
|
AString m_Accum;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Sends all command output to a log, line by line, when the command finishes processing
|
|
|
|
class cLogCommandOutputCallback :
|
|
|
|
public cStringAccumCommandOutputCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// cStringAccumCommandOutputCallback overrides:
|
|
|
|
virtual void Finished(void) override;
|
2013-07-29 07:13:03 -04:00
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Sends all command output to a log, line by line; deletes self when command finishes processing
|
|
|
|
class cLogCommandDeleteSelfOutputCallback :
|
|
|
|
public cLogCommandOutputCallback
|
|
|
|
{
|
|
|
|
typedef cLogCommandOutputCallback super;
|
|
|
|
|
|
|
|
virtual void Finished(void) override
|
|
|
|
{
|
|
|
|
super::Finished();
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|