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 rules in .ini file with QSetting and asterisk
Forum Update on Monday, May 27th 2025

Logging rules in .ini file with QSetting and asterisk

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 4.4k 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.
  • B Offline
    B Offline
    beecksche
    wrote on 20 Jul 2018, 08:35 last edited by beecksche
    #1

    Hi,
    I want to use the logging rules in my configuration file (config.ini).

    I write the logging rules with:

    void setLoggingRules(const QString& category, const bool &value)
    {
        QSettings settings("config.ini", QSettings::IniFormat);
        settings.beginGroup("Rules");
        settings.setValue(category, value);
        settings.endGroup();
    }
    

    If the category contains an asterisk, eg. module.general.* the asterisk will be encoded to %2A in my ini file. So the rule won't be applied.

    I tried to set the ini codec to UTF-8, but the asterisk is still saved as %2A

    void setLoggingRules(const QString& category, const bool &value)
    {
        QSettings settings("config.ini", QSettings::IniFormat);
        settings.setIniCodec("UTF-8");
        settings.beginGroup("Rules");
        settings.setValue(category, value);
        settings.endGroup();
    }
    

    Is there a way to store the asterisk in the file?

    J 1 Reply Last reply 20 Jul 2018, 09:11
    0
    • B beecksche
      20 Jul 2018, 08:35

      Hi,
      I want to use the logging rules in my configuration file (config.ini).

      I write the logging rules with:

      void setLoggingRules(const QString& category, const bool &value)
      {
          QSettings settings("config.ini", QSettings::IniFormat);
          settings.beginGroup("Rules");
          settings.setValue(category, value);
          settings.endGroup();
      }
      

      If the category contains an asterisk, eg. module.general.* the asterisk will be encoded to %2A in my ini file. So the rule won't be applied.

      I tried to set the ini codec to UTF-8, but the asterisk is still saved as %2A

      void setLoggingRules(const QString& category, const bool &value)
      {
          QSettings settings("config.ini", QSettings::IniFormat);
          settings.setIniCodec("UTF-8");
          settings.beginGroup("Rules");
          settings.setValue(category, value);
          settings.endGroup();
      }
      

      Is there a way to store the asterisk in the file?

      J Offline
      J Offline
      JonB
      wrote on 20 Jul 2018, 09:11 last edited by
      #2

      @beecksche
      Basically, no. People keep asking about why Qt .ini file is not identical to Windows. It isn't, it's only "approximate". Qt .ini files work consistently if you only write/read them from Qt, but there are incompatibilities if you mix Qt read/write with non-Qt-app write/read.

      K 1 Reply Last reply 20 Jul 2018, 09:44
      2
      • J JonB
        20 Jul 2018, 09:11

        @beecksche
        Basically, no. People keep asking about why Qt .ini file is not identical to Windows. It isn't, it's only "approximate". Qt .ini files work consistently if you only write/read them from Qt, but there are incompatibilities if you mix Qt read/write with non-Qt-app write/read.

        K Offline
        K Offline
        kkoehne
        Moderators
        wrote on 20 Jul 2018, 09:44 last edited by
        #3

        @JonB , he writes a file though that is most likely to be consumed by the Qt Logging Framework (https://doc.qt.io/qt-5/qloggingcategory.html#configuring-categories). So it is consumed by Qt, after all.

        For internal reasons, the logging file is not parsed by QSettings, which is probably why it doesn't handle this. But Qt could arguably work around this by normalizing %2A when parsing; feel free to create a suggestion about this on bugreports.qt.io .

        Director R&D, The Qt Company

        J 1 Reply Last reply 20 Jul 2018, 09:48
        2
        • K kkoehne
          20 Jul 2018, 09:44

          @JonB , he writes a file though that is most likely to be consumed by the Qt Logging Framework (https://doc.qt.io/qt-5/qloggingcategory.html#configuring-categories). So it is consumed by Qt, after all.

          For internal reasons, the logging file is not parsed by QSettings, which is probably why it doesn't handle this. But Qt could arguably work around this by normalizing %2A when parsing; feel free to create a suggestion about this on bugreports.qt.io .

          J Offline
          J Offline
          JonB
          wrote on 20 Jul 2018, 09:48 last edited by JonB
          #4

          @kkoehne
          Then he's going to have a problem between Categories allowing * character while QSettings does not!

          Sorry, I phrased that badly. Does the Qt logging actually read categories from that file itself, or will he be doing that via his own QSettings reading and passing on the results to logging categories function calls? If it's the latter, then it's all done by QSettings and will be decoded properly. It just won't "look" right if he inspects the file.

          K B 2 Replies Last reply 20 Jul 2018, 10:10
          0
          • J JonB
            20 Jul 2018, 09:48

            @kkoehne
            Then he's going to have a problem between Categories allowing * character while QSettings does not!

            Sorry, I phrased that badly. Does the Qt logging actually read categories from that file itself, or will he be doing that via his own QSettings reading and passing on the results to logging categories function calls? If it's the latter, then it's all done by QSettings and will be decoded properly. It just won't "look" right if he inspects the file.

            K Offline
            K Offline
            kkoehne
            Moderators
            wrote on 20 Jul 2018, 10:10 last edited by
            #5

            @JonB said in Logging rules in .ini file with QSetting and asterisk:

            Does the Qt logging actually read categories from that file itself, or will he be doing that via his own QSettings reading and passing on the results to logging categories function calls?

            Qt Core does parse the file manually, not using QSettings. This is mainly to avoid deadlocks when QSettings does start to use logging ... But as I said, we can look into making the parsing compatible with whatever QSettings writes.

            Director R&D, The Qt Company

            J 1 Reply Last reply 20 Jul 2018, 10:11
            0
            • K kkoehne
              20 Jul 2018, 10:10

              @JonB said in Logging rules in .ini file with QSetting and asterisk:

              Does the Qt logging actually read categories from that file itself, or will he be doing that via his own QSettings reading and passing on the results to logging categories function calls?

              Qt Core does parse the file manually, not using QSettings. This is mainly to avoid deadlocks when QSettings does start to use logging ... But as I said, we can look into making the parsing compatible with whatever QSettings writes.

              J Offline
              J Offline
              JonB
              wrote on 20 Jul 2018, 10:11 last edited by
              #6

              @kkoehne
              So should the OP wait for Qt code changes before he can proceed now?

              1 Reply Last reply
              0
              • J JonB
                20 Jul 2018, 09:48

                @kkoehne
                Then he's going to have a problem between Categories allowing * character while QSettings does not!

                Sorry, I phrased that badly. Does the Qt logging actually read categories from that file itself, or will he be doing that via his own QSettings reading and passing on the results to logging categories function calls? If it's the latter, then it's all done by QSettings and will be decoded properly. It just won't "look" right if he inspects the file.

                B Offline
                B Offline
                beecksche
                wrote on 20 Jul 2018, 10:16 last edited by
                #7

                @JonB

                The configuration file for the logging is set with the neviroment vairable QT_LOGGING_CONF.

                qputenv("QT_LOGGING_CONF", "config.ini");
                

                In this file Qt"searches" for a "Rules" group and read the keys and values, example from https://doc.qt.io/qt-5/qloggingcategory.html#configuring-categories:

                The asterisk is a wildcard for the rules.

                [Rules]
                *.debug=false
                driver.usb.debug=true
                

                In my applikation i already use a configuration file. So my idea was to implement the logging rules in this configuration file. To read/write this file i use QSettings, so for me it would be nice, to write the logging rules also with QSettings.

                J 1 Reply Last reply 20 Jul 2018, 10:33
                0
                • B beecksche
                  20 Jul 2018, 10:16

                  @JonB

                  The configuration file for the logging is set with the neviroment vairable QT_LOGGING_CONF.

                  qputenv("QT_LOGGING_CONF", "config.ini");
                  

                  In this file Qt"searches" for a "Rules" group and read the keys and values, example from https://doc.qt.io/qt-5/qloggingcategory.html#configuring-categories:

                  The asterisk is a wildcard for the rules.

                  [Rules]
                  *.debug=false
                  driver.usb.debug=true
                  

                  In my applikation i already use a configuration file. So my idea was to implement the logging rules in this configuration file. To read/write this file i use QSettings, so for me it would be nice, to write the logging rules also with QSettings.

                  J Offline
                  J Offline
                  JonB
                  wrote on 20 Jul 2018, 10:33 last edited by
                  #8

                  @beecksche said in Logging rules in .ini file with QSetting and asterisk:

                  so for me it would be nice, to write the logging rules also with QSettings

                  And as I understand @kkoehne to be saying, this cannot be done till something is changed in Qt library code?

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kkoehne
                    Moderators
                    wrote on 20 Jul 2018, 11:03 last edited by
                    #9

                    Well, you can hack around it and write the file in QSettings, but then 'un-mask' the character manually using QFile :)

                    Not that this is particularly elegant ...

                    Director R&D, The Qt Company

                    B 1 Reply Last reply 20 Jul 2018, 11:20
                    0
                    • K kkoehne
                      20 Jul 2018, 11:03

                      Well, you can hack around it and write the file in QSettings, but then 'un-mask' the character manually using QFile :)

                      Not that this is particularly elegant ...

                      B Offline
                      B Offline
                      beecksche
                      wrote on 20 Jul 2018, 11:20 last edited by beecksche
                      #10

                      @kkoehne

                      Now I save all rules with one key, the value sperated with semicolons. I don't use the QT_LOGGING_CONF enviroment variable.

                      [Rules]
                      LoggingRules="module.io.low=true;module.db.low=true;"
                      

                      Read/Write the keys and values from LoggingRules with QSettings and set the QLoggingCategory::setFilterRules().

                      void setLoggingRules(const QString& category, const bool &value)
                      {
                          QMap<QString, bool> rules = loggingRules();
                      
                          if (rules.contains(category))
                              rules[category] = value;
                          else
                              rules.insert(category, value);
                      
                          setLoggingRules(rules, true);
                      }
                      
                      void setLoggingRules(const QMap<QString, bool> &loggingRules, bool save)
                      {
                          QString qt_logging_rules;
                      
                          QMapIterator<QString, bool> i(loggingRules);
                          while (i.hasNext()) {
                              i.next();
                      
                              qt_logging_rules.append(QString("%1=%2").arg(i.key()).arg(i.value() ? "true" : "false"));
                      
                              if (i.hasNext())
                                  qt_logging_rules.append(";");
                          }
                      
                          if (save) {
                              QSettings settings("config.ini", QSettings::IniFormat;
                              settings.beginGroup("Rules");
                              settings.setValue("LoggingRules", qt_logging_rules);
                              settings.endGroup();
                          }
                          qt_logging_rules.replace(";", "\n");
                          QLoggingCategory::setFilterRules(qt_logging_rules);
                      }
                      
                      QMap<QString, bool> loggingRules()
                      {
                          QSettings settings("config.ini", QSettings::IniFormat);
                          settings.beginGroup("Rules");
                      
                          QString rulesString = settings.value("LoggingRules").toString();
                      
                          QStringList rules = rulesString.split(";");
                      
                          QMap<QString, bool> loggingRules;
                      
                          for (auto rule : rules) {
                              QStringList ruleAndValue = rule.split("=");
                      
                              if (ruleAndValue.size() != 2)
                                  continue;
                      
                              QString category = ruleAndValue.at(0);
                              bool value = ruleAndValue.at(1) == "true";
                      
                              loggingRules.insert(category, value);
                          }
                      
                          return loggingRules;
                      }
                      

                      Ugly but it works ;)

                      1 Reply Last reply
                      0
                      • aha_1980A Offline
                        aha_1980A Offline
                        aha_1980
                        Lifetime Qt Champion
                        wrote on 22 Jul 2018, 16:46 last edited by
                        #11

                        Corresponding bugreport: https://bugreports.qt.io/browse/QTBUG-69548

                        Qt has to stay free or it will die.

                        1 Reply Last reply
                        2
                        • B Offline
                          B Offline
                          beecksche
                          wrote on 21 Sept 2018, 08:45 last edited by
                          #12

                          The "problem" is solved in the new patch 5.11.2

                          Thanks a lot!

                          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