27#include "core/LogEntry.h"
28#include "core/ProcessLogParser.h"
36using LogEmitter = std::function<void(
const LogEntry &)>;
38inline void emitParsedOutput(QString &buffer,
const QString &chunk,
39 const QString &source,
bool isStdErr,
40 const LogEmitter &emitLog) {
41 const QVector<LogEntry> entries =
42 parseProcessOutput(buffer, chunk, source, isStdErr);
43 for (
const LogEntry &entry : entries) {
48inline void emitParsedOutput(QHash<QProcess *, QString> &buffers,
49 QProcess *process,
const QString &chunk,
50 const QString &source,
bool isStdErr,
51 const LogEmitter &emitLog) {
55 QString &buffer = buffers[process];
56 emitParsedOutput(buffer, chunk, source, isStdErr, emitLog);
59inline void drainProcessOutput(QProcess *process, QString &stdoutBuffer,
60 QString &stderrBuffer,
const QString &source,
61 const LogEmitter &emitLog) {
66 const QString stdoutChunk =
67 QString::fromUtf8(process->readAllStandardOutput());
68 const QString stderrChunk =
69 QString::fromUtf8(process->readAllStandardError());
71 emitParsedOutput(stdoutBuffer, stdoutChunk, source,
false, emitLog);
72 emitParsedOutput(stderrBuffer, stderrChunk, source,
true, emitLog);
73 if (!stdoutBuffer.isEmpty()) {
74 emitParsedOutput(stdoutBuffer,
"\n", source,
false, emitLog);
76 if (!stderrBuffer.isEmpty()) {
77 emitParsedOutput(stderrBuffer,
"\n", source,
true, emitLog);
81inline void drainProcessOutput(QProcess *process, QString &stdoutBuffer,
82 QString &stderrBuffer,
const QString &source,
83 const LogEmitter &emitLog,
84 QString *stdoutCapture,
85 QString *stderrCapture) {
90 const QString stdoutChunk =
91 QString::fromUtf8(process->readAllStandardOutput());
92 const QString stderrChunk =
93 QString::fromUtf8(process->readAllStandardError());
95 stdoutCapture->append(stdoutChunk);
98 stderrCapture->append(stderrChunk);
101 emitParsedOutput(stdoutBuffer, stdoutChunk, source,
false, emitLog);
102 emitParsedOutput(stderrBuffer, stderrChunk, source,
true, emitLog);
103 if (!stdoutBuffer.isEmpty()) {
104 emitParsedOutput(stdoutBuffer,
"\n", source,
false, emitLog);
106 if (!stderrBuffer.isEmpty()) {
107 emitParsedOutput(stderrBuffer,
"\n", source,
true, emitLog);
111inline void drainProcessOutput(QProcess *process,
112 QHash<QProcess *, QString> &stdoutBuffers,
113 QHash<QProcess *, QString> &stderrBuffers,
114 const QString &source,
115 const LogEmitter &emitLog) {
119 QString &stdoutBuffer = stdoutBuffers[process];
120 QString &stderrBuffer = stderrBuffers[process];
121 drainProcessOutput(process, stdoutBuffer, stderrBuffer, source, emitLog);
124inline void drainProcessOutput(QProcess *process,
125 QHash<QProcess *, QString> &stdoutBuffers,
126 QHash<QProcess *, QString> &stderrBuffers,
127 const QString &source,
128 const LogEmitter &emitLog,
129 QString *stdoutCapture,
130 QString *stderrCapture) {
134 QString &stdoutBuffer = stdoutBuffers[process];
135 QString &stderrBuffer = stderrBuffers[process];
136 drainProcessOutput(process, stdoutBuffer, stderrBuffer, source, emitLog,
137 stdoutCapture, stderrCapture);
140inline void emitErrorLog(
const QString &source,
const QString &message,
141 const LogEmitter &emitLog) {
143 entry.level = LogLevel::Error;
144 entry.source = source;
145 entry.message = message;
146 entry.timestamp = QDateTime::currentDateTime();