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. How to use qInstallMessageHandler in an in-Process Axserver

How to use qInstallMessageHandler in an in-Process Axserver

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 818 Views 2 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.
  • T Offline
    T Offline
    TheCrowKaka
    wrote on last edited by
    #1

    I want to direct all debug messages from a Axserver DLL that I have written to a file.

    qInstallMessageHandler works for me when it is a QApplication. However, when I use that in the constructor of the Axserver DLL, it works only for the debug messages inside the constructor. The moment I call any other slot defined in the DLL, the DLL crashes. The DLL works very well without the qInstallMessageHandler code.

    My Header file has the following.
    static void myMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString& msg);

    My class constructor has the following.

    static QTextStream output_ts;
    
    void MyClassSDK::myMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
    {
        switch (type) {
        case QtDebugMsg:
            output_ts << QString("Debug: %1").arg(msg) << endl;
            break;
        case QtWarningMsg:
            output_ts << QString("Warning: %1").arg(msg) << endl;
            break;
        case QtCriticalMsg:
            output_ts << QString("Critical: %1").arg(msg) << endl;
            break;
        case QtFatalMsg:
            output_ts << QString("Fatal: %1").arg(msg) << endl;
            abort();
        }
    }
    
    MyClassSDK::MyClassSDK(QObject *parent)
    {
        QString logfilePath = "DLLDebugLogFile.txt";/
        logfilePath.prepend(default_setting_path);
        QFile outFile(logfilePath);
        outFile.open(QIODevice::WriteOnly | QIODevice::Append);
        output_ts.setDevice(&outFile);
        qInstallMessageHandler(MyClassSDK::myMessageHandler);
    
    qDebug()<<"Test Message 1";
    
    // followed by the rest of the class...
    
    // Somewhere another slot defined...
    void MyClassSDK::DebugTest()
    {
        qDebug()<< "Used for debug testing";
    }
    
    

    Here, the "Test Message 1" is output to the log file. However the DLL crashes at the call to DebugTest() function.

    Is it even possible to use this in a Axserver especially the Library or In-Process one?
    If Yes, then Unable to find any example or clue of how to do it.

    A Qt Enthusiastic...

    Paul ColbyP 1 Reply Last reply
    0
    • T TheCrowKaka

      I want to direct all debug messages from a Axserver DLL that I have written to a file.

      qInstallMessageHandler works for me when it is a QApplication. However, when I use that in the constructor of the Axserver DLL, it works only for the debug messages inside the constructor. The moment I call any other slot defined in the DLL, the DLL crashes. The DLL works very well without the qInstallMessageHandler code.

      My Header file has the following.
      static void myMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString& msg);

      My class constructor has the following.

      static QTextStream output_ts;
      
      void MyClassSDK::myMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
      {
          switch (type) {
          case QtDebugMsg:
              output_ts << QString("Debug: %1").arg(msg) << endl;
              break;
          case QtWarningMsg:
              output_ts << QString("Warning: %1").arg(msg) << endl;
              break;
          case QtCriticalMsg:
              output_ts << QString("Critical: %1").arg(msg) << endl;
              break;
          case QtFatalMsg:
              output_ts << QString("Fatal: %1").arg(msg) << endl;
              abort();
          }
      }
      
      MyClassSDK::MyClassSDK(QObject *parent)
      {
          QString logfilePath = "DLLDebugLogFile.txt";/
          logfilePath.prepend(default_setting_path);
          QFile outFile(logfilePath);
          outFile.open(QIODevice::WriteOnly | QIODevice::Append);
          output_ts.setDevice(&outFile);
          qInstallMessageHandler(MyClassSDK::myMessageHandler);
      
      qDebug()<<"Test Message 1";
      
      // followed by the rest of the class...
      
      // Somewhere another slot defined...
      void MyClassSDK::DebugTest()
      {
          qDebug()<< "Used for debug testing";
      }
      
      

      Here, the "Test Message 1" is output to the log file. However the DLL crashes at the call to DebugTest() function.

      Is it even possible to use this in a Axserver especially the Library or In-Process one?
      If Yes, then Unable to find any example or clue of how to do it.

      Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      Hi @TheCrowKaka,

      I don't know anything about Axserver, but in your MyClassSDK constructor, this line:

         output_ts.setDevice(&outFile);
      

      Is taking a local pointer, and passing to a global object. That is &outFile is a pointer to your outFile variable, but outFile is a local variable, which will be automatically deleted once the constructor finishes. So when the later qDebug() runs, the output_ts will try to access the non-longer-valid outFile pointer.

      You should either allocate outFile on the heap, or provide some other way of maintaining the output stream.

      Cheers.

      T 1 Reply Last reply
      4
      • Paul ColbyP Paul Colby

        Hi @TheCrowKaka,

        I don't know anything about Axserver, but in your MyClassSDK constructor, this line:

           output_ts.setDevice(&outFile);
        

        Is taking a local pointer, and passing to a global object. That is &outFile is a pointer to your outFile variable, but outFile is a local variable, which will be automatically deleted once the constructor finishes. So when the later qDebug() runs, the output_ts will try to access the non-longer-valid outFile pointer.

        You should either allocate outFile on the heap, or provide some other way of maintaining the output stream.

        Cheers.

        T Offline
        T Offline
        TheCrowKaka
        wrote on last edited by
        #3

        @Paul-Colby Hello, Thanks for this tip. I just made the QFile outFile also static and that solved my problem. This thing worked. Thanks again.

        A Qt Enthusiastic...

        jsulmJ 1 Reply Last reply
        0
        • T TheCrowKaka has marked this topic as solved on
        • T TheCrowKaka

          @Paul-Colby Hello, Thanks for this tip. I just made the QFile outFile also static and that solved my problem. This thing worked. Thanks again.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @TheCrowKaka said in How to use qInstallMessageHandler in an in-Process Axserver:

          static

          Static? Why?
          Simply make it class member.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          T 1 Reply Last reply
          1
          • jsulmJ jsulm

            @TheCrowKaka said in How to use qInstallMessageHandler in an in-Process Axserver:

            static

            Static? Why?
            Simply make it class member.

            T Offline
            T Offline
            TheCrowKaka
            wrote on last edited by
            #5

            @jsulm That does not work. Static works as both output_ts and outFile are required across multiple classes.

            A Qt Enthusiastic...

            jsulmJ 1 Reply Last reply
            0
            • T TheCrowKaka

              @jsulm That does not work. Static works as both output_ts and outFile are required across multiple classes.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @TheCrowKaka said in How to use qInstallMessageHandler in an in-Process Axserver:

              Static works as both output_ts and outFile are required across multiple classes

              Sounds like bad design

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1

              • Login

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