1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
5 #include "cmConfigure.h" // IWYU pragma: keep
12 #include "cmProcessOutput.h"
13 #include "cmUVProcessChain.h"
15 /** \class cmProcessTools
16 * \brief Helper classes for process output parsing
22 using Encoding
= cmProcessOutput::Encoding
;
23 /** Abstract interface for process output parsers. */
27 /** Process the given output data from a tool. Processing may be
28 done incrementally. Returns true if the parser is interested
29 in any more data and false if it is done. */
30 bool Process(const char* data
, int length
)
32 return this->ProcessChunk(data
, length
);
34 bool Process(const char* data
)
36 return this->Process(data
, static_cast<int>(strlen(data
)));
39 virtual ~OutputParser() = default;
42 /** Implement in a subclass to process a chunk of data. It should
43 return true only if it is interested in more data. */
44 virtual bool ProcessChunk(const char* data
, int length
) = 0;
47 /** Process output parser that extracts one line at a time. */
48 class LineParser
: public OutputParser
51 /** Construct with line separation character and choose whether to
52 ignore carriage returns. */
53 LineParser(char sep
= '\n', bool ignoreCR
= true);
55 /** Configure logging of lines as they are extracted. */
56 void SetLog(std::ostream
* log
, const char* prefix
);
59 std::ostream
* Log
= nullptr;
60 const char* Prefix
= nullptr;
65 bool ProcessChunk(const char* data
, int length
) override
;
67 /** Implement in a subclass to process one line of input. It
68 should return true only if it is interested in more data. */
69 virtual bool ProcessLine() = 0;
72 /** Trivial line handler for simple logging. */
73 class OutputLogger
: public LineParser
76 OutputLogger(std::ostream
& log
, const char* prefix
= nullptr)
78 this->SetLog(&log
, prefix
);
82 bool ProcessLine() override
{ return true; }
85 /** Run a process and send output to given parsers. */
86 static std::vector
<cmUVProcessChain::Status
> RunProcess(
87 cmUVProcessChainBuilder
& builder
, OutputParser
* out
,
88 OutputParser
* err
= nullptr, Encoding encoding
= cmProcessOutput::Auto
);