From a436301b3960aa750ec97c78e969b7f5e7161d2a Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Fri, 14 Mar 2025 08:58:56 -0400 Subject: [PATCH] cxx-qt-lib: Add two more QtLogging functions This adds two new global functions available in : qFormatLogMessage and qSetMessagePattern. qFormatLogMessage is similar to qt_message_output, but actually documented. Instead of going to the default message handler though, it outputs to a QString. This is probably not useful without message handler support, but it's easy to expose. qSetMessagePattern is very useful, as it allows CXX-Qt applications to set custom Qt message patterns for logging. --- CHANGELOG.md | 1 + crates/cxx-qt-lib/src/core/mod.rs | 4 +++- crates/cxx-qt-lib/src/core/qtlogging.rs | 17 ++++++++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fbe8d465..bea91689c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Support for `QMessageLogContext` and sending log messages to the Qt message handler. - Serde support for further types: `QByteArray`, `QSet`, `QStringList`, `QVector`, `QUrl` - Added `QEventLoop` to cxx-qt-lib-extras. +- Support for setting Qt log message patterns with `q_set_message_pattern` and formatting log messages ith `q_format_log_message`. ### Removed diff --git a/crates/cxx-qt-lib/src/core/mod.rs b/crates/cxx-qt-lib/src/core/mod.rs index 51e9acb5d..e3a725ee2 100644 --- a/crates/cxx-qt-lib/src/core/mod.rs +++ b/crates/cxx-qt-lib/src/core/mod.rs @@ -113,7 +113,9 @@ mod qvector; pub use qvector::{QVector, QVectorElement}; mod qtlogging; -pub use qtlogging::{qt_message_output, QMessageLogContext, QtMsgType}; +pub use qtlogging::{ + q_format_log_message, q_set_message_pattern, qt_message_output, QMessageLogContext, QtMsgType, +}; #[cxx::bridge] mod ffi { diff --git a/crates/cxx-qt-lib/src/core/qtlogging.rs b/crates/cxx-qt-lib/src/core/qtlogging.rs index f24c5501f..0ca320e19 100644 --- a/crates/cxx-qt-lib/src/core/qtlogging.rs +++ b/crates/cxx-qt-lib/src/core/qtlogging.rs @@ -36,6 +36,21 @@ mod ffi { /// Outputs a message in the Qt message handler. fn qt_message_output(msgType: QtMsgType, context: &QMessageLogContext, string: &QString); + /// Generates a formatted string out of the type, context, str arguments. + #[cxx_name = "qFormatLogMessage"] + fn q_format_log_message( + msgType: QtMsgType, + context: &QMessageLogContext, + string: &QString, + ) -> QString; + + /// Changes the output of the default message handler. + /// + /// # Safety + /// This function is marked as unsafe because it is not guaranteed to be thread-safe. + #[cxx_name = "qSetMessagePattern"] + unsafe fn q_set_message_pattern(pattern: &QString); + #[cxx_name = "qmessagelogcontext_line"] #[doc(hidden)] fn line(context: &QMessageLogContext) -> i32; @@ -131,4 +146,4 @@ unsafe impl ExternType for QMessageLogContext<'_> { } use crate::const_assert; -pub use ffi::{qt_message_output, QtMsgType}; +pub use ffi::{q_format_log_message, q_set_message_pattern, qt_message_output, QtMsgType};