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. QString Question

QString Question

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 6 Posters 2.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.
  • C Offline
    C Offline
    Crag_Hack
    wrote on last edited by
    #1

    Hi I will be using QString to record program activity which will be written to a log after program activity is completed. Sometimes the QString may contain thousands of lines of program activity. What's the best way to handle this situation without consuming too many resources? (don't want too many expensive memory reallocations unless their resource consumption is negligible)
    Thanks :)

    JonBJ 1 Reply Last reply
    0
    • AmrCoderA Offline
      AmrCoderA Offline
      AmrCoder
      wrote on last edited by
      #2

      in this situation instead of saving all this in memory, you can use streams in QT or the stander c++ to write your log file without save all this until the programme terminates.
      something like this.

      std::ofstream mylog("logFile.txt");
      mylog << "your string you want to save here"\n;
      mylog << "..etc";
      mylog.close();
      

      and save as many as you want, if there is any chance your app will close without reach to the end which is mylog.close(); you can use mylog.flush(); from time to time to save your string and make sure it saved your string without reach close function to close the stream.
      don't forget #include <fstream>

      1 Reply Last reply
      0
      • C Crag_Hack

        Hi I will be using QString to record program activity which will be written to a log after program activity is completed. Sometimes the QString may contain thousands of lines of program activity. What's the best way to handle this situation without consuming too many resources? (don't want too many expensive memory reallocations unless their resource consumption is negligible)
        Thanks :)

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @Crag_Hack
        QStrings are stored in a shared lookup area. Somewhere it says they're good at reallocation, in that they reserve extra memory to allow for the possibility. Bit strange to store thousands of lines in a QString, but if that's what you want to do I guess it would be OK. Of course if you can do what @AmrCoder says and write it out as you go along that will be a lot cheaper, but I presume you have some reason for wanting to keep it all together in memory...

        1 Reply Last reply
        2
        • C Offline
          C Offline
          Crag_Hack
          wrote on last edited by
          #4

          Thanks guys.

          @AmrCoder I am currently using QTextStreams but they seem to write immediately. Should I use a different QT class? I prefer Qt but std::ofstream is good too.

          @JonB Actually no reason for using QString that's the only way I could think of accomplishing what I want.

          1 Reply Last reply
          0
          • AmrCoderA Offline
            AmrCoderA Offline
            AmrCoder
            wrote on last edited by
            #5

            @Crag_Hack No that is very good using QFile with QTextStream the same logic as stander fstream.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Crag_Hack
              wrote on last edited by
              #6

              How come the write happens immediately though? Here's the code:

              //setting up the stream:
              log.setFileName("log.txt");
              log.open(QIODevice::Append | QIODevice::Text);
              logStream.setDevice(&log);
              
              //...some code later as needed...
              
              logStream << statusString + "<br>" << endl;
              
              JKSHJ 1 Reply Last reply
              0
              • C Crag_Hack

                How come the write happens immediately though? Here's the code:

                //setting up the stream:
                log.setFileName("log.txt");
                log.open(QIODevice::Append | QIODevice::Text);
                logStream.setDevice(&log);
                
                //...some code later as needed...
                
                logStream << statusString + "<br>" << endl;
                
                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #7

                @Crag_Hack said in QString Question:

                How come the write happens immediately though?
                ...

                logStream << statusString + "<br>" << endl;
                

                First, std::endl flushes the buffer, which causes all buffered data to be written to the file. https://stackoverflow.com/questions/4751972/endl-and-flushing-the-buffer

                May I ask why you don't want it written immediately?

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                4
                • C Offline
                  C Offline
                  Crag_Hack
                  wrote on last edited by Crag_Hack
                  #8

                  Thanks JKSH. I don't want to write immediately because this is a data backup program and I don't want logging to happen concurrently with the data backup operations since it will most likely slow them down. Is this a justified conclusion?

                  JKSHJ 1 Reply Last reply
                  0
                  • C Crag_Hack

                    Thanks JKSH. I don't want to write immediately because this is a data backup program and I don't want logging to happen concurrently with the data backup operations since it will most likely slow them down. Is this a justified conclusion?

                    JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by
                    #9

                    @Crag_Hack said in QString Question:

                    Is this a justified conclusion?

                    The only way to know is to benchmark. Do not write complicated "optimizations" based on what you think will happen. Instead, base your optimizations on solid data. Tell us: How much faster will your backup be if you defer the log write? (run a test and give us some actual numbers)

                    If you backup and log to the same spinning disk drive, then there could be a noticeable slowdown. If you backup and log to the same M.2 SSD, or if you backup and log to different drives, you might not notice any slowdown at all. (Note: This is a general principle; this is not guaranteed fact)

                    I don't want to write immediately because this is a data backup program and I don't want logging to happen concurrently with the data backup operations since it will most likely slow them down.

                    If you find that you do need to wait till the end of the backup process before you write your log to disk, you can append each log line in memory in a QStringList, instead of a single, ever-growing QString.

                    One last thing to think about: What do you want to happen if the backup gets interrupted halfway? (If the user aborts it, or if power is lost) Do you want a partial log plus partial backup? Do you want to discard everything and start from scratch?

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    1 Reply Last reply
                    3
                    • C Offline
                      C Offline
                      Crag_Hack
                      wrote on last edited by Crag_Hack
                      #10

                      @JKSH I just tried with deferred log writing and without even timing I can see significant improvement for 128 and 512 small files in a backup.

                      I'll test more tomorrow.

                      A less than ideal detail for this scenario - it appears as if the QTextStream is automatically flushing after it reaches a certain amount of information to write. Any way to disable this behavior?

                      Perhaps I should switch to std::ofstream?

                      jsulmJ JKSHJ 2 Replies Last reply
                      0
                      • 6thC6 Offline
                        6thC6 Offline
                        6thC
                        wrote on last edited by
                        #11

                        I log all of my applications inputs and some engine activity. I can also log performance metrics.

                        I have a central buffer / queue which has multiple threads calling it with <time,> <command>, <data>

                        Every time it's called, once the add to log entry container is done, it calls another method to process.

                        This process method has:

                        std::unique_lock<std::mutex> unique_lock(mutex,std::defer_lock);
                            if(mutex.try_lock()==false)
                                return;
                        

                        This way a single process is writing the messages to disk and it just keeps processing to disk as fast as it can - it might complete and then the next message comes in starts it up again.

                        I just use QByteArray and QFile to write.

                        1 Reply Last reply
                        0
                        • C Crag_Hack

                          @JKSH I just tried with deferred log writing and without even timing I can see significant improvement for 128 and 512 small files in a backup.

                          I'll test more tomorrow.

                          A less than ideal detail for this scenario - it appears as if the QTextStream is automatically flushing after it reaches a certain amount of information to write. Any way to disable this behavior?

                          Perhaps I should switch to std::ofstream?

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

                          @Crag_Hack said in QString Question:

                          Perhaps I should switch to std::ofstream?

                          As @JKSH already suggested use QStringList.

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

                          1 Reply Last reply
                          0
                          • C Crag_Hack

                            @JKSH I just tried with deferred log writing and without even timing I can see significant improvement for 128 and 512 small files in a backup.

                            I'll test more tomorrow.

                            A less than ideal detail for this scenario - it appears as if the QTextStream is automatically flushing after it reaches a certain amount of information to write. Any way to disable this behavior?

                            Perhaps I should switch to std::ofstream?

                            JKSHJ Offline
                            JKSHJ Offline
                            JKSH
                            Moderators
                            wrote on last edited by
                            #13

                            @Crag_Hack said in QString Question:

                            it appears as if the QTextStream is automatically flushing after it reaches a certain amount of information to write.

                            Yes, this is how stream buffers work. QTextStream and std::ofstream both do it.

                            Any way to disable this behavior?

                            No. You can set the buffer size for std::ofstream, but you cannot ask it to refrain from writing to disk until the backup completes. If you want this guarantee, implement your own in-memory buffer (see my QStringList suggestion)

                            Further reading: https://stackoverflow.com/questions/10449772/does-c-ofstream-file-writing-use-a-buffer

                            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                            1 Reply Last reply
                            2

                            • Login

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