comparison align/ExternalProgramAligner.cpp @ 779:5de2b710cfae

Log external program stderr to our log file
author Chris Cannam
date Wed, 01 Jul 2020 11:41:07 +0100
parents 699b5b130ea2
children ee430e9ffccc
comparison
equal deleted inserted replaced
778:83a7b10b7415 779:5de2b710cfae
97 97
98 m_alignmentModel = ModelById::add(alignmentModel); 98 m_alignmentModel = ModelById::add(alignmentModel);
99 other->setAlignment(m_alignmentModel); 99 other->setAlignment(m_alignmentModel);
100 100
101 m_process = new QProcess; 101 m_process = new QProcess;
102 m_process->setProcessChannelMode(QProcess::ForwardedErrorChannel); 102 m_process->setProcessChannelMode(QProcess::SeparateChannels);
103 103
104 connect(m_process, 104 connect(m_process,
105 SIGNAL(finished(int, QProcess::ExitStatus)), 105 SIGNAL(finished(int, QProcess::ExitStatus)),
106 this, 106 this,
107 SLOT(programFinished(int, QProcess::ExitStatus))); 107 SLOT(programFinished(int, QProcess::ExitStatus)));
108 108
109 connect(m_process,
110 SIGNAL(readyReadStandardError()),
111 this,
112 SLOT(logStderrOutput()));
113
109 QStringList args; 114 QStringList args;
110 args << refPath << otherPath; 115 args << refPath << otherPath;
111 116
112 SVCERR << "ExternalProgramAligner: Starting program \"" 117 SVCERR << "ExternalProgramAligner: Starting program \""
113 << m_program << "\" with args: "; 118 << m_program << "\" with args: ";
138 m_document->addNonDerivedModel(m_alignmentModel); 143 m_document->addNonDerivedModel(m_alignmentModel);
139 } 144 }
140 } 145 }
141 146
142 void 147 void
148 ExternalProgramAligner::logStderrOutput()
149 {
150 if (!m_process) return;
151
152 m_process->setReadChannel(QProcess::StandardError);
153
154 qint64 byteCount = m_process->bytesAvailable();
155 if (byteCount == 0) {
156 m_process->setReadChannel(QProcess::StandardOutput);
157 return;
158 }
159
160 QByteArray buffer = m_process->read(byteCount);
161 while (buffer.endsWith('\n') || buffer.endsWith('\r')) {
162 buffer.chop(1);
163 }
164
165 QString str = QString::fromUtf8(buffer);
166
167 cerr << str << endl;
168
169 QString pfx = QString("[pid%1] ").arg(m_process->processId());
170 str.replace("\r", "\\r");
171 str.replace("\n", "\n" + pfx);
172
173 SVDEBUG << pfx << str << endl;
174
175 m_process->setReadChannel(QProcess::StandardOutput);
176 }
177
178 void
143 ExternalProgramAligner::programFinished(int exitCode, 179 ExternalProgramAligner::programFinished(int exitCode,
144 QProcess::ExitStatus status) 180 QProcess::ExitStatus status)
145 { 181 {
146 SVCERR << "ExternalProgramAligner::programFinished" << endl; 182 SVCERR << "ExternalProgramAligner::programFinished" << endl;
147 183
151 SVCERR << "ERROR: ExternalProgramAligner: Emitting process " << process 187 SVCERR << "ERROR: ExternalProgramAligner: Emitting process " << process
152 << " is not my process!" << endl; 188 << " is not my process!" << endl;
153 return; 189 return;
154 } 190 }
155 191
192 logStderrOutput();
193
156 auto alignmentModel = ModelById::getAs<AlignmentModel>(m_alignmentModel); 194 auto alignmentModel = ModelById::getAs<AlignmentModel>(m_alignmentModel);
157 if (!alignmentModel) { 195 if (!alignmentModel) {
158 SVCERR << "ExternalProgramAligner: AlignmentModel no longer exists" 196 SVCERR << "ExternalProgramAligner: AlignmentModel no longer exists"
159 << endl; 197 << endl;
160 return; 198 return;