Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

Forums

OverviewFelgo 3 Support (Qt 5) › logging

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #19458

    Bas

    hi,

    I want to do some simple logging in an app.

    What is the best way to do this?

     

    thanx in advance.

    Bas Lentfert

    #19496

    Günther
    Felgo Team

    Hi Bas,

    I guess you intend to log client messages to a file/storage instead of simple console output, which you can do with console.log. So one option is to store your log messages locally with the Storage component, similar to storing other user-data of your app. The storage uses a local SQLite database, you can also create your own local database with the QtQuick Local Storage feature.

    The new Felgo update 2.17.0 also adds a new interesting feature: The FileUtils allow to work with the local file system, to e.g. write a custom log file directly from QML (otherwise, file access requires Qt C++ code).

    Best,
    Günther

    #20937

    Bas

    hi Gunther,

     

    in Release mode i want to catch console.log in a file

    is this possible?

     

    greetz,

    Bas

    #20964

    Günther
    Felgo Team

    Hi Bas,

    by default, for Release builds with config: “publish”, Felgo deactivates logging. To do so, a custom Message Handler is used, which does no logging.

    You can also set an own message handler for qDebug / console log calls. This is possible with qInstallMsgHandler  in C++ so you can e.g. write to a file instead.

    Best,
    Günther

     

    #20970

    Bas

    hi Gunther,

     

    i have qInstallMessageHandler in main.cpp

    and the following messagehandler:

    void myMessageHandler(QtMsgType type, const QMessageLogContext &, const QString & msg)
    {
        QString txt;
        switch (type) {
        case QtDebugMsg:
            txt = QString("Debug: %1").arg(msg);
            break;
        case QtWarningMsg:
            txt = QString("Warning: %1").arg(msg);
            break;
        case QtCriticalMsg:
            txt = QString("Critical: %1").arg(msg);
            break;
        case QtFatalMsg:
            txt = QString("Fatal: %1").arg(msg);
            break;
        default:
            txt = QString("Default: %1").arg(msg);
        }
        QString qs = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
    
        QFile outFile(qs + "/appname-log.txt");
        outFile.open(QIODevice::WriteOnly | QIODevice::Append);
        QTextStream ts(&outFile);
        ts << txt << endl;
    }
    

    but i can not get the console.log messages.

    should i write a custom log via setContextProperty?

     

    gr,

    Bas

     

     

    #20971

    Günther
    Felgo Team

    Hi,

    the messages are available with the msg property of the function. Please also make sure to set the custom handler after Felgo is initialized. Otherwise it is overwritten again:

    #include <QApplication>
    #include <VPApplication>
    
    #include <QQmlApplicationEngine>
    #include <QDebug>
    
    void myMessageHandler(QtMsgType type, const QMessageLogContext &, const QString & msg)
    {
      qDebug() << "MY MESSAGE HANDLER: " << msg;
    }
    
    int main(int argc, char *argv[])
    {
      QApplication app(argc, argv);
      VPApplication vplay;
    
      // Use platform-specific fonts instead of Felgo's default font
      vplay.setPreservePlatformFonts(true);
    
      // QQmlApplicationEngine is the preferred way to start qml projects since Qt 5.2
      // if you have older projects using Qt App wizards from previous QtCreator versions than 3.1, please change them to QQmlApplicationEngine
      QQmlApplicationEngine engine;
      vplay.initialize(&engine);
    
      // use this during development
      // for PUBLISHING, use the entry point below
      vplay.setMainQmlFileName(QStringLiteral("qml/Main.qml"));
    
      // use this instead of the above call to avoid deployment of the qml files and compile them into the binary with qt's resource system qrc
      // this is the preferred deployment option for publishing games to the app stores, because then your qml files and js files are protected
      // to avoid deployment of your qml files and images, also comment the DEPLOYMENTFOLDERS command in the .pro file
      // also see the .pro file for more details
      // vplay.setMainQmlFileName(QStringLiteral("qrc:/qml/Main.qml"));
      engine.load(QUrl(vplay.mainQmlFileName()));
    
      // set custom message handler
      // note: requires to be called after vplay.initialize(&engine)
      // the Felgo initialization overwrites the handler otherwise
      qInstallMessageHandler(myMessageHandler);
    
      return app.exec();
    }
    

    Best,
    Günther

    #20972

    Bas

    cool thanx, worx!

Viewing 7 posts - 1 through 7 (of 7 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded