Skip to content

Commit b24934b

Browse files
committed
cxx-qt-lib: Add two more QtLogging functions
This adds two new global functions available in <QtLogging>: 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.
1 parent 20dd5ad commit b24934b

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3535
- Support for `QMessageLogContext` and sending log messages to the Qt message handler.
3636
- Serde support for further types: `QByteArray`, `QSet`, `QStringList`, `QVector`, `QUrl`
3737
- Added `QEventLoop` to cxx-qt-lib-extras.
38+
- Support for setting Qt log message patterns with `set_message_pattern`.
3839

3940
### Removed
4041

crates/cxx-qt-lib/src/core/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ mod qvector;
113113
pub use qvector::{QVector, QVectorElement};
114114

115115
mod qtlogging;
116-
pub use qtlogging::{qt_message_output, QMessageLogContext, QtMsgType};
116+
pub use qtlogging::{
117+
q_format_log_message, qt_message_output, q_set_message_pattern, QMessageLogContext, QtMsgType,
118+
};
117119

118120
#[cxx::bridge]
119121
mod ffi {

crates/cxx-qt-lib/src/core/qtlogging.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ mod ffi {
3636
/// Outputs a message in the Qt message handler.
3737
fn qt_message_output(msgType: QtMsgType, context: &QMessageLogContext, string: &QString);
3838

39+
/// Generates a formatted string out of the type, context, str arguments.
40+
#[cxx_name = "qFormatLogMessage"]
41+
fn q_format_log_message(
42+
msgType: QtMsgType,
43+
context: &QMessageLogContext,
44+
string: &QString,
45+
) -> QString;
46+
47+
/// Changes the output of the default message handler.
48+
#[cxx_name = "qSetMessagePattern"]
49+
unsafe fn q_set_message_pattern(pattern: &QString);
50+
3951
#[cxx_name = "qmessagelogcontext_line"]
4052
#[doc(hidden)]
4153
fn line(context: &QMessageLogContext) -> i32;
@@ -131,4 +143,4 @@ unsafe impl ExternType for QMessageLogContext<'_> {
131143
}
132144

133145
use crate::const_assert;
134-
pub use ffi::{qt_message_output, QtMsgType};
146+
pub use ffi::{q_format_log_message, qt_message_output, q_set_message_pattern, QtMsgType};

0 commit comments

Comments
 (0)