Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Log
Forum Updated to NodeBB v4.3 + New Features

Log

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 4.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    glararan
    wrote on last edited by
    #1

    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:

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

    class Log : public QObject
    {
    Q_OBJECT

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

    private:
    QSettings* settings;
    Settings* setting_class;
    };@

    log.cpp
    @void Log::Init()
    {
    // Launcher Dir
    QDir dir;
    if(!dir.exists(setting_class->Launcher_Dir))
    dir.mkdir(setting_class->Launcher_Dir);

    // Create Log & Init
    const QString LogPath = setting_class->Launcher_Dir + "//" + setting_class->Launcher_LogName;
    
    std::ofstream LogStream;
    LogStream.open(LogPath.toStdString(), std::ios_base::out | std::ios_base::trunc);
    if(LogStream)
    {
        std::cout.rdbuf(LogStream.rdbuf());
        std::clog.rdbuf(LogStream.rdbuf());
        std::cerr.rdbuf(LogStream.rdbuf());
    }
    
    QLog << "Test test" << std::endl; // THIS WORKING WRITE SUCCESS
    

    }
    @

    main.cpp
    @include "Log.h"

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

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

    Where can be mistake?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      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 :-)

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • G Offline
        G Offline
        glararan
        wrote on last edited by
        #3

        How would you propose it?

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          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":http://qt-project.org/doc/qt-4.8/qdebug.html

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            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":http://qt-project.org/doc/qt-4.8/QApplication.html or "QCoreApplication":http://qt-project.org/doc/qt-4.8/qcoreapplication.html, 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.

            (Z(:^

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved