Important: Please read the Qt Code of Conduct -

How to redirect stdout & stderr to a file in a Qt console App (QCoreApplication) ?

  • Hello.

    I am running a Qt console app in Windows with a detached console. The app has a server to handle some requests and also to act upon some file-watchers. Internally I call some external executables which print messages to stdout.

    I want all the output messages of the app (and executables called internally) to be logged in a file instead of stdout. I have used "qInstallMessageHandler" to capture the logs. But this only logs the messages logged through "qDebug()" and the other messages which are directed to stdout are left out.

    I tired using "freopen" and it didn't redirect the stdout to the specified file even though I got proper return ptr value from the call.

    Is it possible to redirect all output to stdout and stderr to a file when running an console app using QCoreApplication?

    Thank you in advance.

  • Lifetime Qt Champion

    @eupuser00 said in How to redirect stdout & stderr to a file in a Qt console App (QCoreApplication) ?:

    Internally I call some external executables which print messages to stdout.

    See QProcess::readAllStandardOutput() and it's corresponding signals.

  • @eupuser00
    You can do as @Christian-Ehrlicher has just posted, but that will only affect stdout writing from child processes. I believe you are asking for redirection of anything written within your calling program itself, libraries etc.

    I'm not a C++-er, but I think your current problem is: stuff written to C++ cout is not the same thing as stuff written to stdout? See and

    If the streams are synchronized, a program can mix iostream operations with stdio operations, and their observable effects are guaranteed to follow the same order as used in the thread.

    I don't know what the "synchronized" & "observable effects" add up to, but it may be that cout uses file handle #1 like stdout does but it does not actually use stdout --- so freopen() won't be enough.

    You either ought do your redirection on low-level file handles 0/1/2 (fdopen(), and possibly some dup()-ping, IIRC) instead of just on stdin/out/err, or perhaps cout has its own thing. Googling for something about "redirecting stdout cout" might help you.

  • @Christian-Ehrlicher

    Thanks. But your suggestion works when I am using QProcess to execute the external binaries. And as @JonB says I need to redirect everything from my main program.

  • Lifetime Qt Champion

    @eupuser00 said in How to redirect stdout & stderr to a file in a Qt console App (QCoreApplication) ?:

    I need to redirect everything from my main program.

    Why do you use cout in your own program in the first place? Esp. when it has no console...

  • No, I am not using cout at all (only qDebug, qWarning,...). But the external tools or the libs use it and they log to stdout and these messages are only visible in Qt app output console(if i run the tool from Qt Creator) and not in my redirected log files.

  • Hi, what happens if you launch your console app with an ancient "DOS"-style redirection, e.g.

    yourconsoleapp.exe > filename.txt

  • @hskoglund

    Not output at all. freopen is not logging anything when i use:
    freopen("log_file.txt", "w", stdout);
    freopen("log_file.txt", "w", (FILE*)1);

    Also Qt keeps complaining for freopen :

    main.cpp:16:1: warning: 'freopen' is deprecated: This function or variable may be unsafe. Consider using freopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    stdio.h:242:20: note: 'freopen' has been explicitly marked deprecated here
    vcruntime.h:255:55: note: expanded from macro '_CRT_INSECURE_DEPRECATE'
    vcruntime.h:245:47: note: expanded from macro '_CRT_DEPRECATE_TEXT'

  • Same if you omit all freopen() calls?

  • @eupuser00
    Is there any reason you don't want to look at low-level e.g. fdopen() when I've already explained that you need to do that to achieve what you want, and why stdout does not work for you? Makes me wonder what the point of answering was.

  • Lifetime Qt Champion

    @eupuser00 said in How to redirect stdout & stderr to a file in a Qt console App (QCoreApplication) ?:

    But the external tools or the libs use it and they log to stdout and these messages

    So how do you call them when not with QProcess? And when you use something else - why not use QProcess?

  • @hskoglund
    Command line style redirection didn't work when I used "2>&1 > file" as one of the arguments to the QProcess::start().
    With "freopen()" even the qDebug messages which were logged through my custom msg handler was failing. (Without "freopen()" the qDebug messages were logged.)

    I was calling the external programs through the static function in "QProcess::execute()". But now switched to use the "QProcess::start()" so that I can use "QProcess::setProcessChannelMode()" as shown below.

    I was able to get it working with File Handles and dup(). Here is the code snippet:

    // Global scope
    // Setting same file for stdout & stderr (and qDebug from custom msg handler 
    // func, "customMessageOutput")
    QFile outFile("log_file.txt");
      QProcess extTool;
      extTool.start(program, arg);
    redirect_stdout_stderr() { | QIODevice::Append)
      fout = outFile.handle();
      // redirect stdout 
      _dup2(fout, _fileno(stdout))
      // redirect stderr 
      _dup2(_fileno(stdout), _fileno(stderr))
      // Installing a custom message handler
      // Note: Definition for "customMessageOutput" is not shown here.
    • For brevity, I have not included error handling.

    • I had trouble to redirect both stdout and stderr to the same file. I was opening the same file twice and trying to use 2 file descriptors/ handles. This got solved when I used:
      _dup2(_fileno(stdout), _fileno(stderr))

    Thank you all for the suggestions and pointing me towards the right direction to achieve a solution.

    I would like to know whether there is a better/elegant solution in Qt? If yes, then I would like to adapt to it.

    PS: I will mark this thread as "solved" once I get some or no suggestions.

  • @eupuser00
    Well done! For heeding the advice, and for getting the code right :)

    Yes, dup/dup2 is for merging stdout & stderr.

    From a GUI I did something like the following during start up in old code

    open("/dev/null", O_RDONLY);
    open(logfile, O_WRONLY);

    I like having stdin (0) opened from a null device, so any attempt to read from it delivers EOF.

    Qt isn't going to provide a better solution. This operates at the lowest level.

    You might have to check your code on Windows. Hopefully it works, I suspect I did that stuff via native calls as their will be some situation where it's better.

  • Late to the party, but figured out some more of this situation that may help others.

    The straight duping to fd 1 or 2 and/or use of _fileno() with stdout and stderr don't work when the app is launched normally within Windows by say a double-click in File Explorer (and thus has no console). The reason why is in that situation stdout and stderr both have an fd of -2 which I suppose indicates they are open for purposes of not breaking calling code but are not open in terms of actually doing anything when used. In other words, sort of a CRT /dev/null setup.

    More info on that situation here:

    So, this general pattern worked for me:

            FILE *loggingStreamStdout;
            FILE *loggingStreamStderr;
            QFile loggingFile("app.log");
   | QIODevice::Append | QIODevice::Unbuffered);
            int fd = loggingFile.handle();
            freopen_s(&loggingStreamStdout, "\\\\.\\NUL", "w", stdout);
            setvbuf(loggingStreamStdout, NULL, _IONBF, 0);
            freopen_s(&loggingStreamStderr, "\\\\.\\NUL", "w", stderr);
            setvbuf(loggingStreamStderr, NULL, _IONBF, 0);
            _dup2(fd, _fileno(stdout));
            _dup2(fd, _fileno(stderr));

    That uses a QFile to handle your log file and then freopens stdout and stderr on Window's version of /dev/null. This gives them valid file descriptors (instead of the -2). Then you can _dup2() the log file fd to them. I also like to set the streams as non-buffered so that tools like Baretail.exe show real-time output from the exe.