[windows] Set QsDebugOutput to console

Issue #8 resolved
Alexandre Petitjean created an issue

If QsLog is included inside a project with CONFIG+=console the QsDebugOutput don't write anything in console in Release builds.

I make it works removing Q_OS_WIN specific QsDebugOutput::output function and using Q_OS_UNIX one.

I don't know if it is a bug or a feature. In the second case, how do I print log in console even in Release ?

Comments (5)

  1. raz repo owner

    Hi Alexandre, this was a design decision when building QsLog, the console output on Windows will log to e.g the Visual Studio debugger console, not to stdout/stderr. I think QtCreator can also intercept these messages and show them in the application output view.

    Are the console logs something that the users of your program are interested in, or do you just use them yourself?

    • If it's for you, I think the current behaviour is reasonable, as even a release executable can be run under a debugger and the messages should be visible.
    • If it's for the users of the program, you could try to do a small change like adding
    fprintf(stderr, "%s\n", qPrintable(message));
    fflush(stderr);
    

    to the QsDebugOutput::output function for the Q_OS_WIN case.

  2. Alexandre Petitjean reporter

    QtCreator does intercept these messages very well too.

    Console logs is for users to show what happens while running. I did theses change:

    void QsDebugOutput::output( const QString& message )
    {
    #if defined(QT_DEBUG)
       OutputDebugStringW(reinterpret_cast<const WCHAR*>(message.utf16()));
       OutputDebugStringW(L"\n");
    #else
       fprintf(stderr, "%s\n", qPrintable(message));
       fflush(stderr);
    #endif // QT_DEBUG
    }
    

    Do you think it can be added in QsLog directly or would I keep this on my own ?

  3. raz repo owner

    I cannot include the code as is, but I can probably add a new preprocessor define, such as WINDOWS_USE_PRINTF_FOR_DEBUG_OUTPUT.

  4. Alexandre Petitjean reporter

    That sound a bit heavy to me. Maybe a dedicated destination should be clearer. Currently, QsLogDestConsole implements Debug ouput, but what if it implements a real console output and adding a QsLogDestDebug class ?

  5. raz repo owner

    Uncommenting QS_LOG_WIN_PRINTF_CONSOLE in QsLog.pri will enable fprintf-based console output on Windows.

    Adding a new destination would have been significantly more effort, because each debugger would probably have its own way of receiving messages.

  6. Log in to comment