summaryrefslogtreecommitdiffstats
path: root/src/CommandOutput.h
blob: 0c9641cde49cbfd876d2c2e4e3d217b95b785686 (plain) (blame)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

// 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:
	virtual ~cCommandOutputCallback() {}  // Force a virtual destructor in subclasses
	
	/** Syntax sugar function, calls Out() with Printf()-ed parameters; appends a newline" */
	void Out(const char * a_Fmt, ...) FORMATSTRING(2, 3);
	
	/** 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 */
	virtual void Finished(void) {}
} ;





/** Class that discards all command output */
class cNullCommandOutputCallback :
	public cCommandOutputCallback
{
	// cCommandOutputCallback overrides:
	virtual void Out(const AString & a_Text) override
	{
		// Do nothing
		UNUSED(a_Text);
	}
} ;






/** Accumulates all command output into a string. */
class cStringAccumCommandOutputCallback:
	public cCommandOutputCallback
{
	typedef cCommandOutputCallback super;

public:
	// cCommandOutputCallback overrides:
	virtual void Out(const AString & a_Text) override;
	virtual void Finished(void) override {}

	/** Returns the accumulated command output in a string. */
	const AString & GetAccum(void) const { return m_Accum; }

protected:
	/** 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;
} ;





/** 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;
	}
} ;