[Development] Cake and eating it too for qDebug printing to system log - https://codereview.qt-project.org/89357

Joerg Bornemann joerg.bornemann at digia.com
Fri Jul 11 14:14:12 CEST 2014


On 10-Jul-14 20:03, Thiago Macieira wrote:

> That means we can replace the GetConsoleWindow() call with
> AttachConsole(ATTACH_PARENT_PROCESS). If it succeeds or if it returns
> ERROR_ACCESS_DENIED, we have a console.
>
> For the Windows folks around: should we do this?

I've just played around with this and unfortunately channel redirection 
does not work with this approach.

     myapp > foo.txt
starts myapp in the background like "myapp &" on *nix would do.
The log is written to the console window and doesn't appear in the file.
     cmd /c myapp > foo.txt
Blocks the shell as expected but redirection still doesn't work.

Here's the code. Maybe I missed something.

---snip---
#include <QApplication>
#include <qt_windows.h>
#include <cstdio>
#include <fcntl.h>
#include <io.h>

int main(int argc, char *argv[])
{
     QApplication a(argc, argv);
     if (!AttachConsole(ATTACH_PARENT_PROCESS))
         return 1;
     HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
     int fd = _open_osfhandle((intptr_t)h, _O_TEXT);
     if (fd <= 0)
         return 2;
     *stdout = *_fdopen(fd, "w");
     setvbuf(stdout, NULL, _IONBF, 0 );
     printf("Hello World from GUI app!\n");
     QMetaObject::invokeMethod(&a, "quit", Qt::QueuedConnection);
     return a.exec();
}
---snap---


Joerg



More information about the Development mailing list