Skip to content

Conversation

jnbooth
Copy link
Contributor

@jnbooth jnbooth commented Jun 25, 2025

Instead of exposing undocumented internal features of Qt, qtlogging.rs now exports q_critical, q_debug, q_fatal, q_info, and q_warning, equivalent to Qt's qCritical, qDebug, qFatal, qInfo, and qWarning. So instead of doing this:

qt_message_output(
  QtMsgType::QtInfoMsg,
  &QMessageLogContext::new(c"myfile.rs", 1, c"", c"default"),
  &QString::from(&format!("Test message: {x} / {y}"))
);

You can simply do this:

q_info!("Test message: {x} / {y}");

The file and line number are automatically filled in by the file and line built-in macros.

Copy link

codecov bot commented Jun 25, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (c6710b7) to head (0f31a78).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1300   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           75        75           
  Lines        12772     12772           
=========================================
  Hits         12772     12772           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@LeonMatthesKDAB
Copy link
Collaborator

Hi @jnbooth,
overall I'm in favor of adding the appropriate logging functions.
However, I think the internal API that was already exposed should not be removed.

The internal API is suited to a different use-case and allows more fine-grained access.
One good use-case for it would be to implement a custom log backend with the Qt logging infrastructure.
This would be quite difficult with just q_info!, etc.

So please add the already exposed API back.

@jnbooth
Copy link
Contributor Author

jnbooth commented Jul 14, 2025

@LeonMatthesKDAB Do you mean something like qInstallMessageHandler? Whatever your use case is, I think there will be a way to do it using Qt's public API rather than undocumented internals.

@LeonMatthesKDAB
Copy link
Collaborator

I actually mean the other way around :)

What you're referring to is installing a custom log handler in Qts logging system.

What I was referring to is installing Qts logging system as a custom log handler in Rusts log ecosystem (e.g. the log crate )

@jnbooth
Copy link
Contributor Author

jnbooth commented Jul 17, 2025

@LeonMatthesKDAB Looking at their docs, I think you would do it like this:

use std::ffi::CString;
use cxx_qt_lib::{QString, q_critical, q_debug, q_fatal, q_info, q_warning};
use log::{Level, Log, Record};

struct QtLogger;

impl Log for SimpleLogger {
    fn log(&self, record: &Record) {
        let file = CString::new(record.file().unwrap_or_default());
        let line = record.line().unwrap_or_default() as i32;
        let message = QString::from(record.args());
        match record.level() {
            Level::Error => q_critical(file.as_ptr(), line, &message),
            Level::Warn => q_warn(file.as_ptr(), line, &message),
            Level::Info => q_info(file.as_ptr(), line, &message),
            Level::Debug | Level::Trace => q_debug(file.as_ptr(), line, &message),
        }
    }
}

No need for undocumented internals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants