Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. write log information into a file (performed)
Forum Updated to NodeBB v4.3 + New Features

write log information into a file (performed)

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
11 Posts 5 Posters 1.2k 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.
  • 0xNull0 Offline
    0xNull0 Offline
    0xNull
    wrote on last edited by
    #1

    a day ago i had a doubt about how to save log information into a file... searching on qt forum i already found answers....

    first found this... code but the problem is that idk where is saved the file.... 'context.file'

    const char*const applicationName="myapp";
    
    #ifdef ANDROIDQUIRKS  // Set in my myapp.pro file for android builds
    #include <android/log.h>
    
    void myMessageHandler(
      QtMsgType type,
      const QMessageLogContext& context,
      const QString& msg
    ) {
      QString report=msg;
      if (context.file && !QString(context.file).isEmpty()) {
        report+=" in file ";
        report+=QString(context.file);
        report+=" line ";
        report+=QString::number(context.line);
      }
      if (context.function && !QString(context.function).isEmpty()) {
        report+=+" function ";
        report+=QString(context.function);
      }
      const char*const local=report.toLocal8Bit().constData();
      switch (type) {
      case QtDebugMsg:
        __android_log_write(ANDROID_LOG_DEBUG,applicationName,local);
        break;
      case QtInfoMsg:
        __android_log_write(ANDROID_LOG_INFO,applicationName,local);
        break;
      case QtWarningMsg:
        __android_log_write(ANDROID_LOG_WARN,applicationName,local);
        break;
      case QtCriticalMsg:
        __android_log_write(ANDROID_LOG_ERROR,applicationName,local);
        break;
      case QtFatalMsg:
      default:
        __android_log_write(ANDROID_LOG_FATAL,applicationName,local);
        abort();    
      }
    }
    #endif
    
    ...
    
    int main(int argc,char* argv[]) {
    
      QGuiApplication app(argc,argv);  
    #ifdef ANDROIDQUIRKS
      qInstallMessageHandler(myMessageHandler);
    #endif
      app.setApplicationName(applicationName);
      ..
    

    later i found this solution that save file on Documents folders.... but sometimes this crashes and maybe because open and close the file everytime...
    if anyone know how to solve both problems could be nice for me....

    void myMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
    {
        QString folder=QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
        static QFile logFile(folder+"/BUFFER_PPT.txt");
        static QTextStream ts(&logFile);
    
        if(logFile.open(QFile::ReadWrite | QFile::Append))
        {
          ts << context.file << ":" << context.line << ":"
             << context.function << ": " << msg << endl;
          logFile.close();
        }
    }
    
    piervalliP 1 Reply Last reply
    0
    • 0xNull0 0xNull

      a day ago i had a doubt about how to save log information into a file... searching on qt forum i already found answers....

      first found this... code but the problem is that idk where is saved the file.... 'context.file'

      const char*const applicationName="myapp";
      
      #ifdef ANDROIDQUIRKS  // Set in my myapp.pro file for android builds
      #include <android/log.h>
      
      void myMessageHandler(
        QtMsgType type,
        const QMessageLogContext& context,
        const QString& msg
      ) {
        QString report=msg;
        if (context.file && !QString(context.file).isEmpty()) {
          report+=" in file ";
          report+=QString(context.file);
          report+=" line ";
          report+=QString::number(context.line);
        }
        if (context.function && !QString(context.function).isEmpty()) {
          report+=+" function ";
          report+=QString(context.function);
        }
        const char*const local=report.toLocal8Bit().constData();
        switch (type) {
        case QtDebugMsg:
          __android_log_write(ANDROID_LOG_DEBUG,applicationName,local);
          break;
        case QtInfoMsg:
          __android_log_write(ANDROID_LOG_INFO,applicationName,local);
          break;
        case QtWarningMsg:
          __android_log_write(ANDROID_LOG_WARN,applicationName,local);
          break;
        case QtCriticalMsg:
          __android_log_write(ANDROID_LOG_ERROR,applicationName,local);
          break;
        case QtFatalMsg:
        default:
          __android_log_write(ANDROID_LOG_FATAL,applicationName,local);
          abort();    
        }
      }
      #endif
      
      ...
      
      int main(int argc,char* argv[]) {
      
        QGuiApplication app(argc,argv);  
      #ifdef ANDROIDQUIRKS
        qInstallMessageHandler(myMessageHandler);
      #endif
        app.setApplicationName(applicationName);
        ..
      

      later i found this solution that save file on Documents folders.... but sometimes this crashes and maybe because open and close the file everytime...
      if anyone know how to solve both problems could be nice for me....

      void myMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
      {
          QString folder=QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
          static QFile logFile(folder+"/BUFFER_PPT.txt");
          static QTextStream ts(&logFile);
      
          if(logFile.open(QFile::ReadWrite | QFile::Append))
          {
            ts << context.file << ":" << context.line << ":"
               << context.function << ": " << msg << endl;
            logFile.close();
          }
      }
      
      piervalliP Offline
      piervalliP Offline
      piervalli
      wrote on last edited by
      #2

      @0xNull Try to add a QMutex with lock and unclock because if the application is multi thread you can can a conflit. I use the same approce withouth crash.

      0xNull0 1 Reply Last reply
      0
      • 0xNull0 Offline
        0xNull0 Offline
        0xNull
        wrote on last edited by
        #3

        @piervalli i found that the reason of crashes is that in other thread im trying to "clean" the file but... removing file from device, do you know a way to clean a text file without deleting file?

        M 1 Reply Last reply
        0
        • 0xNull0 0xNull

          @piervalli i found that the reason of crashes is that in other thread im trying to "clean" the file but... removing file from device, do you know a way to clean a text file without deleting file?

          M Offline
          M Offline
          mpergand
          wrote on last edited by
          #4

          @0xNull said in write log information into a file (performed):

          do you know a way to clean a text file without deleting file?

          logFile.resize(0);

          1 Reply Last reply
          1
          • piervalliP piervalli

            @0xNull Try to add a QMutex with lock and unclock because if the application is multi thread you can can a conflit. I use the same approce withouth crash.

            0xNull0 Offline
            0xNull0 Offline
            0xNull
            wrote on last edited by
            #5

            @piervalli said in write log information into a file (performed):

            QMutex

            it could fix this error

            F libc : Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x450042004d0041 in tid 21304 (QThread), pid 21119

            M piervalliP 2 Replies Last reply
            0
            • 0xNull0 0xNull

              @piervalli said in write log information into a file (performed):

              QMutex

              it could fix this error

              F libc : Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x450042004d0041 in tid 21304 (QThread), pid 21119

              M Offline
              M Offline
              mpergand
              wrote on last edited by mpergand
              #6

              @0xNull said in write log information into a file (performed):

              QMutex

              it could fix this error

              I think you can't escape it.

              1 Reply Last reply
              0
              • 0xNull0 0xNull

                @piervalli said in write log information into a file (performed):

                QMutex

                it could fix this error

                F libc : Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x450042004d0041 in tid 21304 (QThread), pid 21119

                piervalliP Offline
                piervalliP Offline
                piervalli
                wrote on last edited by piervalli
                #7

                @mpergand 041 in tid 21304 (QThread), pid 21119 is id of main thread? You can rename/delete in startup of the app.
                This is nice link on Logger https://www.youtube.com/watch?v=GDtrbIs6JA0

                e3b60ac8-5047-4eec-a448-01e9bea1d1a2-image.png

                I have the same approce on my app, I rename/delete file on start appup of the function. I don't know why logfile is not closed. but it works.

                0xNull0 1 Reply Last reply
                0
                • piervalliP piervalli

                  @mpergand 041 in tid 21304 (QThread), pid 21119 is id of main thread? You can rename/delete in startup of the app.
                  This is nice link on Logger https://www.youtube.com/watch?v=GDtrbIs6JA0

                  e3b60ac8-5047-4eec-a448-01e9bea1d1a2-image.png

                  I have the same approce on my app, I rename/delete file on start appup of the function. I don't know why logfile is not closed. but it works.

                  0xNull0 Offline
                  0xNull0 Offline
                  0xNull
                  wrote on last edited by
                  #8

                  @piervalli where is saved logfile.txt?

                  jsulmJ JonBJ piervalliP 3 Replies Last reply
                  0
                  • 0xNull0 0xNull

                    @piervalli where is saved logfile.txt?

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

                    @0xNull said in write log information into a file (performed):

                    where is saved logfile.txt?

                    Since a relative path is used it will be in current working directory.

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

                    1 Reply Last reply
                    1
                    • 0xNull0 0xNull

                      @piervalli where is saved logfile.txt?

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by
                      #10

                      @0xNull
                      As @jsulm says. And since you do not know what the current directory will be at runtime, and it might not even be writable by your application, do not use such a relative path in production. You might have a look at QStandardPaths Class and pick a suitable QStandardPaths::StandardLocation to use to build an absolute path for your log file location.

                      1 Reply Last reply
                      2
                      • 0xNull0 0xNull

                        @piervalli where is saved logfile.txt?

                        piervalliP Offline
                        piervalliP Offline
                        piervalli
                        wrote on last edited by piervalli
                        #11

                        @0xNull I save the file in folder "log" inside the current directory, instead in Android on the external storage. My directory is always writeble with you use can use QStandardPaths::AppLocalDataLocation so it is not visible

                        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