Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Passing logging information from C++ to QML; plugins
Forum Updated to NodeBB v4.3 + New Features

Passing logging information from C++ to QML; plugins

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 1 Posters 253 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.
  • C Offline
    C Offline
    cswingle
    wrote on last edited by
    #1

    I'm trying to diagnose serialnmea/GPS issues on a client's computer and would like to be able to pass logging information from the serialnmea plugin's C++ code to my QML components so I can write this information to a file.

    For example, the plugin code includes lines like this:

    qCDebug(lcSerial) << "Opening serial port" << portName;
    ...
    qWarning("serialnmea: No known GPS device found. Specify the COM port via QT_NMEA_SERIAL_PORT.");
    

    And I've added some of my own qWarning() messages to the plugin and rebuilt a custom version. These are very useful when running the program via QtCreator, but now I need access to this information in a release executable.

    What are the available mechanisms for me to get these messages from the plugin to my QML components so I can write them to a log file?

    C 1 Reply Last reply
    0
    • C cswingle

      I'm trying to diagnose serialnmea/GPS issues on a client's computer and would like to be able to pass logging information from the serialnmea plugin's C++ code to my QML components so I can write this information to a file.

      For example, the plugin code includes lines like this:

      qCDebug(lcSerial) << "Opening serial port" << portName;
      ...
      qWarning("serialnmea: No known GPS device found. Specify the COM port via QT_NMEA_SERIAL_PORT.");
      

      And I've added some of my own qWarning() messages to the plugin and rebuilt a custom version. These are very useful when running the program via QtCreator, but now I need access to this information in a release executable.

      What are the available mechanisms for me to get these messages from the plugin to my QML components so I can write them to a log file?

      C Offline
      C Offline
      cswingle
      wrote on last edited by cswingle
      #2

      Looks like one option is to use qInstallMessageHandler and write a handler in main.cpp that writes log messages to a file instead of stderr. That would appear to require a global file object, which isn't super clean, but it does seem like it works. I haven't tried it after building an executable, however, and I have a feeling the message handler may not get called at all in a release build.

      Am I on the right track? Any ideas on how to make the file path for this logging more dynamic/set via Qt.labs.settings Settings?

      For example, in main.cpp:

      static QPointer<QFile> logFile = nullptr;
      
      void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
      {
          QTextStream stream(logFile);
          QByteArray localMsg = msg.toLocal8Bit();
          const char *file = context.file ? context.file : "";
          const char *function = context.function ? context.function : "";
          switch (type) {
          case QtDebugMsg:
              stream << "Debug: " << localMsg.constData() << " (" << file << ":" << context.line << ", " << function << ")" << endl;
              break;
          case QtWarningMsg:
          ...
          }
      }
      
      int main(int argc, char *argv[])
      {
          QString filename = "/tmp/console_messages.txt";
          QFile file(filename);
          logFile = &file;
          if (logFile->open(QFile::Append)) {
            qInstallMessageHandler(myMessageOutput);
          }
          ...
      }
      
      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