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. Qt 5.4 - Sending qDebug() to a file (aka logging) - (SOLVED)
Forum Updated to NodeBB v4.3 + New Features

Qt 5.4 - Sending qDebug() to a file (aka logging) - (SOLVED)

Scheduled Pinned Locked Moved General and Desktop
19 Posts 6 Posters 14.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.
  • A Offline
    A Offline
    alex_malyu
    wrote on last edited by
    #7

    Somewhere means anywhere. this is global function.

    You can put function above your main or add it anywhere but add declaration
    void releaseMessageOutput( QtMsgType type, const char *msg ); above main.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      ad5xj
      wrote on last edited by
      #8

      OK here is the progress I have made so far:

      main.cpp
      @
      #include <stdio.h>
      #include <stdlib.h>

      #include <QtCore/QDebug>
      #include <QtCore/QtGlobal>
      #include <QtCore/QFile>
      #include <QtCore/QMessageLogger>
      #include <QtCore/QMessageLogContext>
      #include <QtCore/QtMessageHandler>
      #include <QtWidgets/QApplication>

      #include "mainwindow.hpp"

      #define QT_MESSAGE_PATTERN = "[%{type}] %{function}:%{line} - %{message}"

      void debugMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
      {
      Q_UNUSED(context);
      QByteArray txt = QString("").toLocal8Bit();
      // Switch structure left to be converted to write into the file in the future
      switch ( type )
      {
      case QtDebugMsg:
      txt += QString("{Debug} \t\t %1").arg(msg);
      break;
      case QtWarningMsg:
      txt += QString("{Warning:} \t %1").arg(msg);
      break;
      case QtCriticalMsg:
      txt += QString("{Critical:} \t %1").arg(msg);
      break;
      case QtFatalMsg:
      txt += QString("{Fatal:} \t %1").arg(msg);
      abort(); // deliberately core dump
      }
      QFile outFile("Debug.log");
      outFile.open(QIODevice::WriteOnly | QIODevice::Append);

      QTextStream textStream(&outFile);
      textStream << txt << endl;
      

      }

      int main(int argc, char *argv[])
      {
      qInstallMessageHandler(debugMessageOutput);

      QApplication a(argc, argv);
      
      qDebug() << "Loading from Main()";
      MainWindow w;
      
      w.show();
      
      return a.exec&#40;&#41;;
      

      }
      @

      The problem is that the compiler complains of undefined reference to QDebug::QDebug()

      As can be seen in the main.cpp there is a include for QDebug.

      Actually this error occurs for any use of qDebug() anywhere.

      Any help...what am I doing wrong?

      Ken AD5XJ

      1 Reply Last reply
      0
      • A Offline
        A Offline
        alex_malyu
        wrote on last edited by
        #9

        Could you show exact compiler message?
        My code works fine in QT 4 and it looks like you made required for qt 5 changes.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          alex_malyu
          wrote on last edited by
          #10

          Also I would suggest to open file and keep it opened once instead of opening it for every string output, especially cause you current directory can be changed and you will and up with parts of the log in different locations.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            alex_malyu
            wrote on last edited by
            #11

            By the way have you tried:
            #include <QDebug>

            instead of

            #include <QtCore/QDebug>

            1 Reply Last reply
            0
            • A Offline
              A Offline
              ad5xj
              wrote on last edited by
              #12

              Alex_malyu

              The only thing missing from the compiler message is the line number - which is the qDebug() statement.

              main.cpp:49: undefined reference to `QDebug::~QDebug()'

              I am using Qt 5.4 so the recommendation from the docs is to be specific by using <QtCore/QDebug> rather than the more general <QDebug> from earlier versions.

              I do plan to open once and write many, but I have to get past this error first.

              Ken AD5XJ

              1 Reply Last reply
              0
              • A Offline
                A Offline
                alex_malyu
                wrote on last edited by
                #13

                You might have problem with old libraries picked up.
                At least people with similar issues solved it by cleaning and/or rebuilding.

                Check below link for example:
                "https://bugreports.qt.io/browse/QTBUG-40458":https://bugreports.qt.io/browse/QTBUG-40458

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  ad5xj
                  wrote on last edited by
                  #14

                  Alex_Malyu

                  Well I rather doubt that is the case since Qt 5.4 is the only version I am referencing in the tool chain. However, I will check just to be sure.

                  BTW without the redirection, QDebug() works fine.

                  Ken AD5XJ

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    ad5xj
                    wrote on last edited by
                    #15

                    As suspected...no diff after a thorough clean and re-compile attempt.

                    Ken AD5XJ

                    1 Reply Last reply
                    0
                    • clogwogC Offline
                      clogwogC Offline
                      clogwog
                      wrote on last edited by
                      #16

                      i just grabbed your code in main.cpp and it worked.. output in log file:

                      @$ cat Debug.log
                      {Debug} Loading from Main()
                      @

                      so it must be environmental

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        gkochar
                        wrote on last edited by
                        #17

                        [quote author="ad5xj" date="1423516765"]As can be seen in the main.cpp there is a include for QDebug.

                        Actually this error occurs for any use of qDebug() anywhere.

                        Any help...what am I doing wrong?[/quote]

                        You have to #include <QDebug> (or, #include <QtCore/QDebug>) in every file you want to use qDebug() from.

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          ad5xj
                          wrote on last edited by
                          #18

                          The include does not clear the error.

                          An environmental error as suggested would not allow QDebug() to work normally as it does.
                          The only problem I have is the redirection as stated earlier.

                          Ken AD5XJ

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            ad5xj
                            wrote on last edited by
                            #19

                            I think I have come up with at least a minimal solution:

                            main.cpp
                            @
                            #include <stdio.h>
                            #include <stdlib.h>

                            #include <QtCore/QDebug>
                            #include <QtCore/QtGlobal>
                            #include <QtCore/QFile>
                            #include <QtCore/QDateTime>
                            #include <QtCore/QString>
                            #include <QtCore/QMessageLogger>
                            #include <QtCore/QMessageLogContext>
                            #include <QtCore/QtMessageHandler>
                            #include <QtWidgets/QApplication>

                            #include "myGUI.hpp"

                            void debugMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
                            {
                            //Q_UNUSED(context);
                            QString timestr;
                            timestr = QDateTime::currentDateTime().toString("ddd d MMMM yyyy hh:mm:ss ");
                            QByteArray txt;
                            txt += timestr;
                            // Switch structure left to be converted to write into the file in the future
                            switch ( type )
                            {
                            case QtDebugMsg:
                            txt += " Debug ";
                            txt += context.file;
                            txt += ":";
                            txt += QString("%1").arg(context.line);
                            txt += QString(" - %1").arg(msg);
                            break;
                            case QtWarningMsg:
                            txt += context.function;
                            txt += ":";
                            txt += context.line;
                            txt += QString(" - Warning: \t %1").arg(msg);
                            break;
                            case QtCriticalMsg:
                            txt += context.function;
                            txt += ":";
                            txt += context.line;
                            txt += QString(" - Critical: \t %1").arg(msg);
                            break;
                            case QtFatalMsg:
                            txt += context.function;
                            txt += ":";
                            txt += context.line;
                            txt += QString(" - Fatal: \t %1").arg(msg);
                            abort(); // deliberately core dump
                            }
                            QFile outFile("Debug.log");
                            outFile.open(QIODevice::WriteOnly | QIODevice::Append);

                            QTextStream textStream(&outFile);
                            textStream << txt << endl;
                            

                            }

                            int main(int argc, char *argv[])
                            {
                            qInstallMessageHandler(debugMessageOutput);

                            QApplication a(argc, argv);
                            QMessageLogger("MAIN",58,"main").debug("Loading from MAIN");
                            MyGui g;
                            
                            g.show();
                            
                            return a.exec&#40;&#41;;
                            

                            }
                            @

                            then in any module you want to log from...
                            @

                            include <QtCore/QDebug>

                            include <QtCore/QMessageLogger>

                            ...
                            void myFunc()
                            {
                            #ifdef DEBUG_MYDEF
                            QString errmsg
                            errmsg += "This is my message - ";
                            errmsg += QString("%1").arg(errno);

                            QMessageLogger("MyModule",int mylineno,"funcname").debug(errmsg.toLocal8Bit());
                            

                            #endif

                            }
                            @

                            It creates a Debug.log file with time stamped entries.

                            I have conditioned the logger with DEFINES I created but there are a number of other ways this could work just as well.

                            Ken AD5XJ

                            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