How can i toggle debug output *at runtime*?

My nickname is attah, and I’m a qDebug() addict.

Yes, it can be toggled at compile time, but for asking people for logs that is quite crap.
Do i really have to make my own qDebug shim to be able to turn it on and off at runtime?
The google results seems to say this, but there are lots of smart people here so i thought i should ask none the less.

Not an expert at all but doesn’t ~/.config/QtProject/qtlogging.ini control this?

Also, the QT_LOGGING_RULES environment variable?

Exactly, when you create file ~/.config/QtProject/qtlogging.ini with this content:


it will turn on debug logging for all applications, when you switch even qt.*.debug to true, it will enable debug messages from Qt internal classes, you will see huge noise from QML components…

It seems that QT_LOGGING_RULES has no effect with Qt 5.6 used on Sailfish OS. Beside debug toggle, you may control format of log messages by environment variable QT_MESSAGE_PATTERN. It is nicely described on this post: Nicer debug output in Qt using QT_MESSAGE_PATTERN I really like the usage of red color for warning messages :wink:

1 Like

@nephros , @karry Interesting; that instinctively felt compile-time too, but i see now i was mistaken.
However; that will apply to all applications, right?
I’m more after something for just my application, and that i can tell users to enable if they have problems.

But none the less, thanks for educating me.

If you look in ~/.config/QtProject/ you may see profiles for specific applications. Or can create some.

Like and parts of Sailfish OS Cheat Sheet | Sailfish OS Documentation describe.


I finally found what i needed:

QLoggingCategory::defaultCategory()->setEnabled(QtMsgType::QtDebugMsg, MyBooleanSetting);

Basically the same thing as @nephros suggests, but through a C++ interface, and something i can plumb in to make it accessible to regular users.

That’s line 33 in the main of seaprint

    QLoggingCategory::defaultCategory()->setEnabled(QtMsgType::QtDebugMsg, Settings::instance()->debugLog());

with settings in the settings class:

Q_PROPERTY(bool debugLogDefault MEMBER _debugLogDefault CONSTANT)
    bool _debugLogDefault = false;

which, I blieve, allows you do do stuff like:

bool verbose = QLoggingCategory::defaultCategory()->isDebugEnabled();

But otherwise it’s just qDebug() as it was, or? I’m thinking I maybe should implement this.

Not sure if this is a yes or no answer; qDebug() is affected, but/and it needed no updates in its usage.
Setting enabled/disabled for debug messages in the default category toggles all regular qDebug() and even QML console.log() on or off.

The reading it back is just for the parts that are not Qt.

Cool, thanks. I’ll just give that a go!

1 Like

On second thought… The part about console.log might have been fake news. Need to double-check but lacking time.

In c++ land, there is no fake news. Just undefined behavior.