woboq/qmetaobject-rs

console.log() qml not working

Closed this issue ยท 12 comments

I am using env_logger as backend. I can see log messages from the Rust side. However, I cannot see the messages logged from QML.
I thought this was added in #86 but I also found #163 so I'm not sure if it still works.
So is it broken in QT6 or are there some additional steps required which are not mentioned in the README?

System Information:
QT: 6.1.2
Display Server: X11
Distro: Fedroa 34
Kernel: 5.12.17-300.fc34

It's not "broken", it's a feature that Qt sends logs to system journal instead of stdout/stderr by default. Read more at links posted in #162 (which was moved to #163). You gotta use set QT_FORCE_STDERR_LOGGING=1 or just launch from a terminal.

I mean, wait, hold on a second. QT_FORCE_STDERR_LOGGING is just for logging on Qt side.

You wrote that you are using env_logger in Rust. But did you set up Qt to Rust log forwarding afterwards as described in README?

fn main() {
    qmetaobject::log::init_qt_to_rust();
    // don't forget to set up env_logger or any other logging backend.
}

I mean, wait, hold on a second. QT_FORCE_STDERR_LOGGING is just for logging on Qt side.

You wrote that you are using env_logger in Rust. But did you set up Qt to Rust log forwarding afterwards as described in README?

fn main() {
    qmetaobject::log::init_qt_to_rust();
    // don't forget to set up env_logger or any other logging backend.
}

Yes, I did that, tried interchanging the order of initialization too but no luck.

It's not "broken", it's a feature that Qt sends logs to system journal instead of stdout/stderr by default. Read more at links posted in #162 (which was moved to #163). You gotta use set QT_FORCE_STDERR_LOGGING=1 or just launch from a terminal.

Tried that too, still can't get any output on the console.

For more context, my qml is as follows:

import QtQuick 2.6
import QtQuick.Window 2.0
import QtQuick.Controls 2.12

Window {
    id: window
    visible: true
    title: qsTr("WeebTK")

    Button {
        text: qsTr("Ok")
        onClicked: console.log("clicked")
    }
}

Sorry for that, mistakenly closed the issue.

I don't know then. Maybe logging levels, or env logger ignores "qml" messages. There is also a global Qt logging configuration somewhere in ~/.config, but I'm not sure.

Yes, I did that, tried interchanging the order of initialization too but no luck.

Order of initialization shouldn't matter (unless you are logging something in the middle), since it only registers a function in Qt, and doesn't do anything with Rust logging framework per se.

I don't know then. Maybe logging levels, or env logger ignores "qml" messages. There is also a global Qt logging configuration somewhere in ~/.config, but I'm not sure.

Well, I did set RUST_LOG variable to show all logs, and confirmed that it is working by logging from the rust side. So only the QML logs don't seem to work. They do work in projects created with QT Creator, so it shouldn't be a problem with Qt install, I think.

It's not a problem with Qt install, sure. It's all about its runtime, and how it detects whether it needs to pass logs to registered handler or not. You might even want to check their source code (especially qtbase/src/corelib/global/qlogging.cpp) and trace calls, and play around with environment variables and configuration and stuff.

Check environment section in Qt Creator, it might help understand why it works from IDE.

It's not a problem with Qt install, sure. It's all about its runtime, and how it detects whether it needs to pass logs to registered handler or not. You might even want to check their source code (especially qtbase/src/corelib/global/qlogging.cpp) and trace calls, and play around with environment variables and configuration and stuff.

Check environment section in Qt Creator, it might help understand why it works from IDE.

Ok, Ill try and see what I can find.

FWIW, I had to set two levels in my logger (fern) to see all log messages from Qt, after setting the default level to error:

    .level(log::LevelFilter::Error)
    // "default" is the category that the qmetaobject log integration sets by default
    .level_for("default", log::LevelFilter::Debug)
    // "qml" is the category for console.log & friends from qml
    .level_for("qml", log::LevelFilter::Debug)

One more thing! I had to set the following:

export QT_LOGGING_RULES="*.debug=true; qt.*.debug=false"

This can also be set in ~/.config/QtProject/qtlogging.ini as

[Rules]
*.debug=true
qt.*.debug=false

The second rule filters out the logging from Qt itself (this would be very noisy otherwise).

Qt documentation for this: https://doc.qt.io/qt-5/qloggingcategory.html#logging-rules

I was able to fix the issue by setting RUST_LOG properly. It either needs to be set as

RUST_LOG=qml # Other options can be added

or

RUST_LOG=debug # Other options can be added

to work.