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
11 #include "cmProcessOutput.h"
13 /** \class cmProcessTools
14 * \brief Helper classes for process output parsing
20 using Encoding = cmProcessOutput::Encoding;
21 /** Abstract interface for process output parsers. */
25 /** Process the given output data from a tool. Processing may be
26 done incrementally. Returns true if the parser is interested
27 in any more data and false if it is done. */
28 bool Process(const char* data, int length)
30 return this->ProcessChunk(data, length);
32 bool Process(const char* data)
34 return this->Process(data, static_cast<int>(strlen(data)));
37 virtual ~OutputParser() = default;
40 /** Implement in a subclass to process a chunk of data. It should
41 return true only if it is interested in more data. */
42 virtual bool ProcessChunk(const char* data, int length) = 0;
45 /** Process output parser that extracts one line at a time. */
46 class LineParser : public OutputParser
49 /** Construct with line separation character and choose whether to
50 ignore carriage returns. */
51 LineParser(char sep = '\n', bool ignoreCR = true);
53 /** Configure logging of lines as they are extracted. */
54 void SetLog(std::ostream* log, const char* prefix);
57 std::ostream* Log = nullptr;
58 const char* Prefix = nullptr;
63 bool ProcessChunk(const char* data, int length) override;
65 /** Implement in a subclass to process one line of input. It
66 should return true only if it is interested in more data. */
67 virtual bool ProcessLine() = 0;
70 /** Trivial line handler for simple logging. */
71 class OutputLogger : public LineParser
74 OutputLogger(std::ostream& log, const char* prefix = nullptr)
76 this->SetLog(&log, prefix);
80 bool ProcessLine() override { return true; }
83 /** Run a process and send output to given parsers. */
84 static void RunProcess(struct cmsysProcess_s* cp, OutputParser* out,
85 OutputParser* err = nullptr,
86 Encoding encoding = cmProcessOutput::Auto);