I am writing a Qt GUI application that uses the libxslt library. libxslt always prints errors to stderr, but in certain cases it would be helpful for the user to see those messages displayed. Is there a way to capture the stderr output and put it in a QString?
Asked
Active
Viewed 2,158 times
3
-
Have you tried QProcess::readStandardOutput()? – László Papp May 20 '14 at 20:53
-
That wouldn't seem to apply here. The OP says they're using a library in-process. – nobody May 20 '14 at 20:57
-
See [this question](http://stackoverflow.com/questions/3202654/will-loading-a-dll-dynamically-reconcile-its-stderr-to-a-main-application-if-so) for someone solving the exact same problem in Qt and [this one](http://stackoverflow.com/questions/9189461/how-to-read-from-a-library-standard-error) for more general discussion. – nobody May 20 '14 at 20:57
-
1@AndrewMedico: have you ever used it? I do not see where the OP mentioned "library in-process" whatever that means. – László Papp May 20 '14 at 21:03
-
1I take "writing a ... application that uses the libxslt library" to mean they are writing an application that links in a library - not running a child process. – nobody May 20 '14 at 21:09
-
@AndrewMedico: you can run your program through a wrapper? How is that not applicable? It is probably the quickest and simplest way to get it done. – László Papp May 20 '14 at 21:12
-
It's all in the same process. Ironically, I had earlier used QProcess to invoke xsltproc, but switched to using the library since it seemed like better form. – adam.baker May 21 '14 at 02:11
1 Answers
2
There are two quick options:
1) Wrap your usage around with an external process and use QProcess.
This will introduce an external wrapper. This will not allow you to process the error "sometimes" off-hand. You would need some external processing for that.
QProcess process;
process.start("wrapper");
process.waitForFinished();
qDebug() << process.readAllStandardError();
2) As per the famous Linux Programming Interface book example: redirect the stderr (2) file descriptor.
This is a bit more complex, but it is better for avoiding the external process wrapper. It is also only posix compliant, so may not work without that. If you plan to use msvc, this may be a show-stopper. You would need to write the Windows backend then separately or use mingw.
int pfd[2];
pipe(pfd);
// Do other things here.
close(STDERR_FILENO);
if (pfd[1] != STDERR_FILENO) {
dup2(pfd[1], STDERR_FILENO);
close(pfd[1]);
}
read(...);

László Papp
- 51,870
- 39
- 111
- 135
-
Thank you. I had wanted to avoid using an external process. If option (2) involves compiler-specific code, then I think it might be better to create a temporary file and then read from it. I'm glad to know the potential solutions. – adam.baker May 21 '14 at 02:12