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 Updated to NodeBB v4.3 + New Features

Logging rules in .ini file with QSetting and asterisk

Scheduled Pinned Locked Moved Solved General and Desktop
12 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.
  • beeckscheB beecksche

    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?

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on 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.

    kkoehneK 1 Reply Last reply
    2
    • JonBJ JonB

      @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.

      kkoehneK Offline
      kkoehneK Offline
      kkoehne
      Moderators
      wrote on 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

      JonBJ 1 Reply Last reply
      2
      • kkoehneK kkoehne

        @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 .

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 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.

        kkoehneK beeckscheB 2 Replies Last reply
        0
        • JonBJ JonB

          @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.

          kkoehneK Offline
          kkoehneK Offline
          kkoehne
          Moderators
          wrote on 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

          JonBJ 1 Reply Last reply
          0
          • kkoehneK kkoehne

            @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.

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

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

            1 Reply Last reply
            0
            • JonBJ JonB

              @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.

              beeckscheB Offline
              beeckscheB Offline
              beecksche
              wrote on 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.

              JonBJ 1 Reply Last reply
              0
              • beeckscheB beecksche

                @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.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 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
                • kkoehneK Offline
                  kkoehneK Offline
                  kkoehne
                  Moderators
                  wrote on 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

                  beeckscheB 1 Reply Last reply
                  0
                  • kkoehneK kkoehne

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

                    beeckscheB Offline
                    beeckscheB Offline
                    beecksche
                    wrote on 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 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
                      • beeckscheB Offline
                        beeckscheB Offline
                        beecksche
                        wrote on 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