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. Logging - Delete files after n days, make a new one, if size greater than 20MByte, code row inc ase of exception
QtWS25 Last Chance

Logging - Delete files after n days, make a new one, if size greater than 20MByte, code row inc ase of exception

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 77 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.
  • R Offline
    R Offline
    RobertSommer
    wrote last edited by
    #1

    Hello,
    The qDebug is sufficient for me if there were two options.
    Delete files after n days
    if size greater than 20MByte,than create additional with the name of current file and prefix "-01.Log"
    Log exception code row

    Does anyone have an example?

    void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
    {
        static QFile file("C:\\MyProjectInk\\log\\_Qt\\log_" + QDateTime::currentDateTime().toString("yyyy-MM-dd") + ".Log");
        if (!file.isOpen())
        {
            file.open(QFile::WriteOnly | QFile::Append);
        }
    
        QDateTime jetzt = QDateTime::currentDateTime();
        QString dateTime = jetzt.toString("yyyy-MM-dd hh:mm:ss.zzz");
    
        QTextStream stream(&file);
        stream << dateTime << " - " << msg << endl;
    }
    
    int main(int argc, char *argv[])
    {
        qInstallMessageHandler(myMessageOutput);
    //..
    	QString help = QString("[CUSTOM][ToInk] initialized= '%1'    socket->state()= '%2'")
                .arg(initialized ? "true" : "false")
                .arg(socket->state());
        qDebug() << help;
    
    Christian EhrlicherC 1 Reply Last reply
    0
    • R RobertSommer

      Hello,
      The qDebug is sufficient for me if there were two options.
      Delete files after n days
      if size greater than 20MByte,than create additional with the name of current file and prefix "-01.Log"
      Log exception code row

      Does anyone have an example?

      void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
      {
          static QFile file("C:\\MyProjectInk\\log\\_Qt\\log_" + QDateTime::currentDateTime().toString("yyyy-MM-dd") + ".Log");
          if (!file.isOpen())
          {
              file.open(QFile::WriteOnly | QFile::Append);
          }
      
          QDateTime jetzt = QDateTime::currentDateTime();
          QString dateTime = jetzt.toString("yyyy-MM-dd hh:mm:ss.zzz");
      
          QTextStream stream(&file);
          stream << dateTime << " - " << msg << endl;
      }
      
      int main(int argc, char *argv[])
      {
          qInstallMessageHandler(myMessageOutput);
      //..
      	QString help = QString("[CUSTOM][ToInk] initialized= '%1'    socket->state()= '%2'")
                  .arg(initialized ? "true" : "false")
                  .arg(socket->state());
          qDebug() << help;
      
      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote last edited by
      #2

      @RobertSommer said in Logging - Delete files after n days, make a new one, if size greater than 20MByte, code row inc ase of exception:

      Does anyone have an example?

      Look here: https://github.com/MEONMedical/Log4Qt

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      R 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @RobertSommer said in Logging - Delete files after n days, make a new one, if size greater than 20MByte, code row inc ase of exception:

        Does anyone have an example?

        Look here: https://github.com/MEONMedical/Log4Qt

        R Offline
        R Offline
        RobertSommer
        wrote last edited by
        #3

        @Christian-Ehrlicher
        Thanks for the quick reply, isn't there a simple solution? So without a third-party provider. Just deleting the file after 10 (n) days, the line number would be good in case of an exception.

        JonBJ 1 Reply Last reply
        0
        • R RobertSommer

          @Christian-Ehrlicher
          Thanks for the quick reply, isn't there a simple solution? So without a third-party provider. Just deleting the file after 10 (n) days, the line number would be good in case of an exception.

          JonBJ Online
          JonBJ Online
          JonB
          wrote last edited by JonB
          #4

          @RobertSommer
          If you don't want to use a third party's code then you have to write it yourself, maybe using some of the code.

          Delete files after n days

          if size greater than 20MByte,than create additional with the name of current file and prefix "-01.Log"

          Log exception code row

          Qt does not pre-supply dedicated functions to do these, though you can write code with the help of Qt to do so. qDebug() certainly does not do such things.

          1 Reply Last reply
          1
          • C Offline
            C Offline
            CassD
            wrote last edited by CassD
            #5

            To delete the old log files, you need

            • first to get the list of files in the appropriate directory (QDir::entryList)
            • and filter them by date

            So as QDir::entryList offers the possibility to pass filter arguments, this should be possible to do those two steps at once. In fact no, those filters don't allow for file size filtering. You have to loop over the list and remove those too new (reminder, don't remove list elements in a while or for loop, you skip the element next to the one that has been removed !!!) Yours to see if you perform it only at app startup (if your app has a relatively short runtime) or if you also need to set a timer to perform the process at regular interval (which should be needed in case of a server for example).

            To create a new file when the current one is too big, you would have to monitor the size of the current output file, and once the threshold has been reached, close it and create a new file with a new file name.
            I would create a dedicated class which all logs pass through and holds the responsability of creating and closing the files. After each write in it, check the file size, and once the threshold is reached, close it and recreate a new one. All this transparently to the rest of the application.

            and as @JonB said, qDebug() is not what you need to write to a file.

            1 Reply Last reply
            0
            • JonBJ Online
              JonBJ Online
              JonB
              wrote last edited by JonB
              #6

              @RobertSommer
              If you want a model to copy, Python's RotatingFileHandler and more specifically TimedRotatingFileHandler seem to provide what you are speaking about. As you can see it requires a library and a bunch of code to implement, you could write similar using C++ and some Qt calls.

              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