|
| 1 | +// SPDX-FileCopyrightText: 2025 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> |
| 2 | +// SPDX-FileContributor: Joshua Goins <[email protected]> |
| 3 | +// |
| 4 | +// SPDX-License-Identifier: MIT OR Apache-2.0 |
| 5 | +use cxx::{type_id, ExternType}; |
| 6 | +use std::ffi::c_char; |
| 7 | +use std::ffi::CStr; |
| 8 | +use std::marker::PhantomData; |
| 9 | +use std::mem::size_of; |
| 10 | + |
| 11 | +#[cxx::bridge] |
| 12 | +mod ffi { |
| 13 | + /// The level the message is sent to the message handler at. |
| 14 | + #[repr(i32)] |
| 15 | + enum QtMsgType { |
| 16 | + /// A debug message. |
| 17 | + QtDebugMsg = 0, |
| 18 | + /// An info message. |
| 19 | + QtInfoMsg = 4, |
| 20 | + /// A warning message. |
| 21 | + QtWarningMsg = 1, |
| 22 | + /// A fatal message. |
| 23 | + QtFatalMsg = 3, |
| 24 | + /// A critical message. |
| 25 | + QtCriticalMsg = 2, |
| 26 | + } |
| 27 | + |
| 28 | + unsafe extern "C++" { |
| 29 | + include!("cxx-qt-lib/qstring.h"); |
| 30 | + type QString = crate::QString; |
| 31 | + |
| 32 | + include!("cxx-qt-lib/qtlogging.h"); |
| 33 | + type QMessageLogContext<'a> = crate::QMessageLogContext<'a>; |
| 34 | + type QtMsgType; |
| 35 | + |
| 36 | + /// Outputs a message in the Qt message handler. |
| 37 | + fn qt_message_output(msgType: QtMsgType, context: &QMessageLogContext, string: &QString); |
| 38 | + |
| 39 | + #[cxx_name = "qmessagelogcontext_line"] |
| 40 | + #[doc(hidden)] |
| 41 | + fn line(context: &QMessageLogContext) -> i32; |
| 42 | + |
| 43 | + #[cxx_name = "qmessagelogcontext_file"] |
| 44 | + #[doc(hidden)] |
| 45 | + unsafe fn file(context: &QMessageLogContext) -> *const c_char; |
| 46 | + |
| 47 | + #[cxx_name = "qmessagelogcontext_function"] |
| 48 | + #[doc(hidden)] |
| 49 | + unsafe fn function(context: &QMessageLogContext) -> *const c_char; |
| 50 | + |
| 51 | + #[cxx_name = "qmessagelogcontext_category"] |
| 52 | + #[doc(hidden)] |
| 53 | + unsafe fn category(context: &QMessageLogContext) -> *const c_char; |
| 54 | + } |
| 55 | + |
| 56 | + #[namespace = "rust::cxxqtlib1"] |
| 57 | + unsafe extern "C++" { |
| 58 | + include!("cxx-qt-lib/common.h"); |
| 59 | + |
| 60 | + #[doc(hidden)] |
| 61 | + #[rust_name = "construct_qmessagelogcontext"] |
| 62 | + unsafe fn construct<'a>( |
| 63 | + file_name: *const c_char, |
| 64 | + line_number: i32, |
| 65 | + function_name: *const c_char, |
| 66 | + category_name: *const c_char, |
| 67 | + ) -> QMessageLogContext<'a>; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/// The QMessageLogContext struct defines the context passed to the Qt message handler. |
| 72 | +#[repr(C)] |
| 73 | +#[derive(Clone, Copy)] |
| 74 | +pub struct QMessageLogContext<'a> { |
| 75 | + version: i32, |
| 76 | + line: i32, |
| 77 | + file: *const c_char, |
| 78 | + function: *const c_char, |
| 79 | + category: *const c_char, |
| 80 | + _phantom: PhantomData<&'a c_char>, |
| 81 | +} |
| 82 | + |
| 83 | +const_assert!( |
| 84 | + size_of::<QMessageLogContext>() == (size_of::<i32>() * 2) + (size_of::<*const c_char>() * 3) |
| 85 | +); |
| 86 | + |
| 87 | +impl<'a> QMessageLogContext<'a> { |
| 88 | + pub fn new( |
| 89 | + file: &'a CStr, |
| 90 | + line: i32, |
| 91 | + function: &'a CStr, |
| 92 | + category: &'a CStr, |
| 93 | + ) -> QMessageLogContext<'a> { |
| 94 | + unsafe { |
| 95 | + ffi::construct_qmessagelogcontext( |
| 96 | + file.as_ptr(), |
| 97 | + line, |
| 98 | + function.as_ptr(), |
| 99 | + category.as_ptr(), |
| 100 | + ) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /// The line number given to the message handler. |
| 105 | + pub fn line(&self) -> i32 { |
| 106 | + ffi::line(self) |
| 107 | + } |
| 108 | + |
| 109 | + /// The file path given to the message handler. |
| 110 | + pub fn file(&self) -> &'a CStr { |
| 111 | + unsafe { CStr::from_ptr(ffi::file(self)) } |
| 112 | + } |
| 113 | + |
| 114 | + /// The name of the function given to the message handler. |
| 115 | + pub fn function(&self) -> &'a CStr { |
| 116 | + unsafe { CStr::from_ptr(ffi::function(self)) } |
| 117 | + } |
| 118 | + |
| 119 | + /// The category given to the message handler. |
| 120 | + pub fn category(&self) -> &'a CStr { |
| 121 | + unsafe { CStr::from_ptr(ffi::category(self)) } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// Safety: |
| 126 | +// |
| 127 | +// Static checks on the C++ side ensure that QMessageLogContext is trivial. |
| 128 | +unsafe impl ExternType for QMessageLogContext<'_> { |
| 129 | + type Id = type_id!("QMessageLogContext"); |
| 130 | + type Kind = cxx::kind::Trivial; |
| 131 | +} |
| 132 | + |
| 133 | +use crate::const_assert; |
| 134 | +pub use ffi::{qt_message_output, QtMsgType}; |
0 commit comments