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. Create Logging file Using Qfile
QtWS25 Last Chance

Create Logging file Using Qfile

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 3.0k Views
  • 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.
  • M Offline
    M Offline
    Mc6bullet
    wrote on 28 Jul 2021, 07:09 last edited by
    #1

    Hello,
    actually i created one file in that i want to save all the application output of qt?
    anyone have idea how to create that file
    please help me?

    J C 2 Replies Last reply 28 Jul 2021, 07:10
    0
    • M Mc6bullet
      28 Jul 2021, 07:09

      Hello,
      actually i created one file in that i want to save all the application output of qt?
      anyone have idea how to create that file
      please help me?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 28 Jul 2021, 07:10 last edited by
      #2

      @Mc6bullet said in Create Logging file Using Qfile:

      all the application output

      What exact output do you mean? std::cout? qDebug()? Something else?

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

      M 1 Reply Last reply 28 Jul 2021, 07:46
      2
      • J jsulm
        28 Jul 2021, 07:10

        @Mc6bullet said in Create Logging file Using Qfile:

        all the application output

        What exact output do you mean? std::cout? qDebug()? Something else?

        M Offline
        M Offline
        Mc6bullet
        wrote on 28 Jul 2021, 07:46 last edited by
        #3

        @jsulm Means whole console of the QT save in the .log file
        example api requests etc.

        J J 2 Replies Last reply 28 Jul 2021, 07:52
        0
        • M Mc6bullet
          28 Jul 2021, 07:46

          @jsulm Means whole console of the QT save in the .log file
          example api requests etc.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 28 Jul 2021, 07:52 last edited by jsulm
          #4

          @Mc6bullet said in Create Logging file Using Qfile:

          Means whole console of the QT

          What is Qt console? It is still not really clear what you mean.
          I guess you want to write everything printed to stdout in a file?
          There are different ways.
          Simplest solution is to forward the stdout output to a file:

          ./name_of_your_exe > log.txt
          

          Or, you can do it internally in your app, see https://stackoverflow.com/questions/9211298/redirect-stdcout-to-qtextedit/45507241 for inspiration.

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

          1 Reply Last reply
          5
          • M Mc6bullet
            28 Jul 2021, 07:46

            @jsulm Means whole console of the QT save in the .log file
            example api requests etc.

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 28 Jul 2021, 07:53 last edited by
            #5

            @Mc6bullet additionally to what @jsulm said, there's also

            https://doc.qt.io/qt-5/qtglobal.html#qInstallMessageHandler

            that would allow you to redirect everything to a file


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            5
            • M Mc6bullet
              28 Jul 2021, 07:09

              Hello,
              actually i created one file in that i want to save all the application output of qt?
              anyone have idea how to create that file
              please help me?

              C Offline
              C Offline
              CP71
              wrote on 28 Jul 2021, 07:55 last edited by
              #6

              @Mc6bullet
              Hi,
              if you have several qDebug() that help you for diagnostic, you can introduce the following code:

              in main.cpp

              ////////////////////////////////////////////////////////////////////////////////////////////
              ///
              void myMessageOutput(QtMsgType type, const char *msg)
              {
              ...
              // Here we can send text in the fileLog and/or console via application configuration
              switch (type) {
              case QtDebugMsg:
              ...
              break;
              case QtWarningMsg:
              ...
              break;
              case QtCriticalMsg:
              ....
              break;
              case QtFatalMsg:
              if ( errConsoleEnable ) fprintf(stderr, "Fatal: %s\r\n", msg);
              abort();
              }

              if(errLogEnable) {
                  outErrLog->write(text.toLatin1(), text.length());
                  outErrLog->flush();
              }
              

              }

              in int main(int argc, char *argv[])
              {
              ...
              outErrLog = new QFile(F_GET_FILE_LOG);
              outErrLog->open( QIODevice::WriteOnly | QIODevice::Append );

              qInstallMsgHandler(myMessageOutput);
              ...
              

              }

              outErrLog is QFile created once at startup
              Be careful, above is Qt4 code, I think in Qt5 the called qInstallMsgHandler is obsolete, but you have to check.

              Another way is creating a log manager where you can store messages wherever you want (txt, csv, sql and so on). For me, for this object, I think is a good way a single instance object

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Mc6bullet
                wrote on 28 Jul 2021, 08:32 last edited by
                #7

                Thank you so much @CP71 @J-Hilk @jsulm You help me lot

                1 Reply Last reply
                1

                2/7

                28 Jul 2021, 07:10

                topic:navigator.unread, 5
                • Login

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