Important: Please read the Qt Code of Conduct -


  • Hi, because im new in Qt i use old method from std to create log, i try project with logging

    But always big problem.
    After i init log (create and write main infos)
    When i next use example "QLog() << "Test123";" It shut down app

    My current code:

    @// LOG CLASS & Log
    std::ostream& _Log();
    #define QLog _Log()

    class Log : public QObject

    Log(QObject* parent = 0);
    void Init();
    void InitText();

    QSettings* settings;
    Settings* setting_class;

    @void Log::Init()
    // Launcher Dir
    QDir dir;

    // Create Log & Init
    const QString LogPath = setting_class->Launcher_Dir + "//" + setting_class->Launcher_LogName;
    std::ofstream LogStream;, std::ios_base::out | std::ios_base::trunc);
    QLog << "Test test" << std::endl; // THIS WORKING WRITE SUCCESS


    @include "Log.h"

    int main(int argc, char* argv[])
    Log log;
    QLog << "TEst 3333" << std::endl; // SHUT APP :/

    Other code i remove for better preview.. ofc i have MainWindow.

    Where can be mistake?

  • For my personal opinion there are a couple of things requiring redesign. The logic is not complete to my understanding.
    In case QLog is somehow connected to LogStream opened in your Init routine, it is that your ofstream is only local.
    However, this is more crystal ball reading :-)

  • How would you propose it?

  • I would propose to use plain ofstream. You try to make it fancy with reassigning rdbuf. I guess this makes it too complicated for yourself. As already explained in the post above. You are creating an ofstream "LogStream" and it is killed as soon as leave the Init routine.

    Line 2 and 3 of your header: What is this for? Dangling something without relation to the rest. I do not see a relation to your Log class.

    May be you should have a look to "QDebug":

  • Moderators

    Apart from redesign issues:

    your code exits on line 7 (8 actually) because that is where your main() ends - so it should not surprise you. In Qt, we usually use "QApplication": or "QCoreApplication":, esp. their exec() methods. Take a look at some examples to get to know how to use them, or look how Qt Creator auto-generates code there.

Log in to reply