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. Small problem with a log file
Forum Updated to NodeBB v4.3 + New Features

Small problem with a log file

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 4.4k 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.
  • B bask185

    o my bad: I updated the OP

    artwawA Offline
    artwawA Offline
    artwaw
    wrote on last edited by
    #4

    @bask185 QIODevice::WriteOnly implies truncate of the file. You should open with QIODevice::Append instead - that would just... well... append new data at the end of the file.

    For more information please re-read.

    Kind Regards,
    Artur

    1 Reply Last reply
    3
    • B bask185

      o my bad: I updated the OP

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

      @bask185 To add to @artwaw : why don't you keep the file open? Opening and closing it for each incoming byte sounds like a huge waste of resources.

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

      B 1 Reply Last reply
      2
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #6

        Side note: you should also include the QIODevice::Text flag

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        B 1 Reply Last reply
        2
        • B Offline
          B Offline
          bask185
          wrote on last edited by
          #7

          I find it vague that file.open() can actually deletes the content rather than...just open it when given the wrong argument.

          Regardless I succeeded:

          Bullseye set at 6.33333 7.33333
          << 80
          << 3
          0x80 command case 3
          << 37
          Bullseye set at 7.33333 7.33333
          << 80
          << 3
          0x80 command case 3
          << 38
          Bullseye set at 8.33333 7.33333
          

          In the setup I open the file with Write Only and close it so it becomes.. empty :D

          But the output is almost the exact same as qDebug. So tnx. I'd like to mark your answer as the correct one, but one can only mark his own ;)

          1 Reply Last reply
          0
          • VRoninV VRonin

            Side note: you should also include the QIODevice::Text flag

            B Offline
            B Offline
            bask185
            wrote on last edited by bask185
            #8

            @VRonin said in Small problem with a log file:

            Side note: you should also include the QIODevice::Text flag

            If you say something like: "you should do...". Arguments 'why' are not redundant. Not long ago I held a short speech about it ;) and I came across a nice saying a few days ago: "Give a man a fish and you feed him one day, teach a man how to fish and you feed him for his life"

            Right now I have a perfectly fine working log file, so I don't feel like including something called 'Text flag' ;)

            VRoninV 1 Reply Last reply
            0
            • B bask185

              @VRonin said in Small problem with a log file:

              Side note: you should also include the QIODevice::Text flag

              If you say something like: "you should do...". Arguments 'why' are not redundant. Not long ago I held a short speech about it ;) and I came across a nice saying a few days ago: "Give a man a fish and you feed him one day, teach a man how to fish and you feed him for his life"

              Right now I have a perfectly fine working log file, so I don't feel like including something called 'Text flag' ;)

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #9

              @bask185 said in Small problem with a log file:

              "Give a man a fish and you feed him one day, teach a man how to fish and you feed him for his life"

              that's what the docs are for. it takes 5 seconds to Google QIODevice::Text

              @bask185 said in Small problem with a log file:

              Right now I have a perfectly fine working log file, so I don't feel like including something called 'Text flag' ;)

              As long as you don't care about newlines.

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              B 1 Reply Last reply
              2
              • jsulmJ jsulm

                @bask185 To add to @artwaw : why don't you keep the file open? Opening and closing it for each incoming byte sounds like a huge waste of resources.

                B Offline
                B Offline
                bask185
                wrote on last edited by
                #10

                @jsulm said in Small problem with a log file:

                @bask185 To add to @artwaw : why don't you keep the file open? Opening and closing it for each incoming byte sounds like a huge waste of resources.

                Now you mention it... I turned it off but I have another question.

                I would like to write data to the log also in other functions but it is a local variable. I tried working with the header file making it more global but I could not get it to work. I also tried

                QFile log("logboek.txt");
                QTextStream LOG(&log);
                LOG  << cAsHex << " >>";
                

                in other functions, but no luck :(

                jsulmJ 1 Reply Last reply
                0
                • B bask185

                  @jsulm said in Small problem with a log file:

                  @bask185 To add to @artwaw : why don't you keep the file open? Opening and closing it for each incoming byte sounds like a huge waste of resources.

                  Now you mention it... I turned it off but I have another question.

                  I would like to write data to the log also in other functions but it is a local variable. I tried working with the header file making it more global but I could not get it to work. I also tried

                  QFile log("logboek.txt");
                  QTextStream LOG(&log);
                  LOG  << cAsHex << " >>";
                  

                  in other functions, but no luck :(

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

                  @bask185 Implement a class which handles the logging. This class then creates a private static QFile instance and provides public static methods to write logs. Be careful if you use different threads - then you will need to serialize log writing.

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

                  B 1 Reply Last reply
                  2
                  • VRoninV VRonin

                    @bask185 said in Small problem with a log file:

                    "Give a man a fish and you feed him one day, teach a man how to fish and you feed him for his life"

                    that's what the docs are for. it takes 5 seconds to Google QIODevice::Text

                    @bask185 said in Small problem with a log file:

                    Right now I have a perfectly fine working log file, so I don't feel like including something called 'Text flag' ;)

                    As long as you don't care about newlines.

                    B Offline
                    B Offline
                    bask185
                    wrote on last edited by
                    #12

                    @VRonin said in Small problem with a log file:

                    @bask185 said in Small problem with a log file:

                    "Give a man a fish and you feed him one day, teach a man how to fish and you feed him for his life"

                    that's what the docs are for. it takes 5 seconds to Google QIODevice::Text

                    @bask185 said in Small problem with a log file:

                    Right now I have a perfectly fine working log file, so I don't feel like including something called 'Text flag' ;)

                    As long as you don't care about newlines.

                    I do care about newlines alot, that is why add them like: LOG << "\nText to translate : "
                    I've googled and read about it but I am not getting the usage for me, from what I understand it is a windows thingy and it is used for reading files. It sets \r\n to \n for use with C++.

                    VRoninV 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @bask185 Implement a class which handles the logging. This class then creates a private static QFile instance and provides public static methods to write logs. Be careful if you use different threads - then you will need to serialize log writing.

                      B Offline
                      B Offline
                      bask185
                      wrote on last edited by
                      #13

                      @jsulm said in Small problem with a log file:

                      you will need to serialize log writing

                      You mean working with connects and signals? when class X emits a signal to mainwindow with a string so mainwindow can puts the string in the log using the log-class??

                      jsulmJ 1 Reply Last reply
                      0
                      • B bask185

                        @VRonin said in Small problem with a log file:

                        @bask185 said in Small problem with a log file:

                        "Give a man a fish and you feed him one day, teach a man how to fish and you feed him for his life"

                        that's what the docs are for. it takes 5 seconds to Google QIODevice::Text

                        @bask185 said in Small problem with a log file:

                        Right now I have a perfectly fine working log file, so I don't feel like including something called 'Text flag' ;)

                        As long as you don't care about newlines.

                        I do care about newlines alot, that is why add them like: LOG << "\nText to translate : "
                        I've googled and read about it but I am not getting the usage for me, from what I understand it is a windows thingy and it is used for reading files. It sets \r\n to \n for use with C++.

                        VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #14

                        @bask185 said in Small problem with a log file:

                        it is a windows thingy

                        Mainly yes

                        and it is used for reading files

                        Mainly no

                        From http://doc.qt.io/qt-5/qiodevice.html:

                        When reading, the end-of-line terminators are translated to '\n'. When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32.

                        That means that \n will be converted to "\r\n" on windows automatically if you add | QIODevice::Text to the open argument

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        1 Reply Last reply
                        0
                        • B bask185

                          @jsulm said in Small problem with a log file:

                          you will need to serialize log writing

                          You mean working with connects and signals? when class X emits a signal to mainwindow with a string so mainwindow can puts the string in the log using the log-class??

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

                          @bask185 No, I did not write anything about signals/slots. I mean a class which manages the log writing. There is no need to do it in main window. Just provide static methods:

                          class Logger
                          {
                          public:
                              static void log(const QString &text);
                          private:
                              static QFile logFile;
                          };
                          ...
                          Logger::log("Some text");
                          

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

                          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