1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use std::os::raw::c_char;
#[cfg(feature = "log")]
use log::{logger, Level, Record, RecordBuilder};
use crate::QString;
cpp! {{
#include <qmetaobject_rust.hpp>
}}
cpp_class!(
pub unsafe struct QMessageLogContext as "QMessageLogContext"
);
impl QMessageLogContext {
pub fn line(&self) -> i32 {
cpp!(unsafe [self as "QMessageLogContext*"] -> i32 as "int" { return self->line; })
}
pub fn file(&self) -> &str {
unsafe {
let x = cpp!([self as "QMessageLogContext*"] -> *const c_char as "const char*" {
return self->file;
});
if x.is_null() {
return "";
}
std::ffi::CStr::from_ptr(x).to_str().unwrap()
}
}
pub fn function(&self) -> &str {
unsafe {
let x = cpp!([self as "QMessageLogContext*"] -> *const c_char as "const char*" {
return self->function;
});
if x.is_null() {
return "";
}
std::ffi::CStr::from_ptr(x).to_str().unwrap()
}
}
pub fn category(&self) -> &str {
unsafe {
let x = cpp!([self as "QMessageLogContext*"] -> *const c_char as "const char*" {
return self->category;
});
if x.is_null() {
return "";
}
std::ffi::CStr::from_ptr(x).to_str().unwrap()
}
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum QtMsgType {
QtDebugMsg,
QtWarningMsg,
QtCriticalMsg,
QtFatalMsg,
QtInfoMsg,
}
#[cfg(feature = "log")]
impl From<QtMsgType> for Level {
fn from(lvl: QtMsgType) -> Self {
match lvl {
QtMsgType::QtDebugMsg => Level::Debug,
QtMsgType::QtInfoMsg => Level::Info,
QtMsgType::QtWarningMsg => Level::Warn,
QtMsgType::QtCriticalMsg => Level::Error,
QtMsgType::QtFatalMsg => Level::Error,
}
}
}
#[cfg(feature = "log")]
impl From<Level> for QtMsgType {
fn from(lvl: Level) -> Self {
match lvl {
Level::Error => QtMsgType::QtCriticalMsg,
Level::Warn => QtMsgType::QtWarningMsg,
Level::Info => QtMsgType::QtInfoMsg,
Level::Debug => QtMsgType::QtDebugMsg,
Level::Trace => QtMsgType::QtDebugMsg,
}
}
}
pub type QtMessageHandler = Option<extern "C" fn(QtMsgType, &QMessageLogContext, &QString)>;
pub fn install_message_handler(logger: QtMessageHandler) -> QtMessageHandler {
cpp!(unsafe [logger as "QtMessageHandler"] -> QtMessageHandler as "QtMessageHandler" {
return qInstallMessageHandler(logger);
})
}
#[cfg(feature = "log")]
extern "C" fn log_capture(msg_type: QtMsgType, context: &QMessageLogContext, message: &QString) {
let level = msg_type.into();
let target = match context.category() {
"" => "default",
x => x,
};
let file = match context.file() {
"" => None,
x => Some(x),
};
let line = match context.line() {
0 => None,
x => Some(x as _),
};
let mut record = Record::builder();
record.level(level).target(target).file(file).line(line).module_path(None);
match context.function() {
"" => finish(record, format_args!("{}", message)),
f => finish(record, format_args!("[in {}] {}", f, message)),
}
fn finish<'a>(mut record: RecordBuilder<'a>, args: std::fmt::Arguments<'a>) {
logger().log(&record.args(args).build())
}
}
#[cfg(feature = "log")]
pub fn init_qt_to_rust() {
install_message_handler(Some(log_capture));
}
#[cfg(test)]
#[cfg(feature = "log")]
mod tests {
use super::*;
#[test]
fn test_double_init() {
init_qt_to_rust();
init_qt_to_rust();
}
#[test]
fn test_convert() {
assert_eq!(Level::from(QtMsgType::QtInfoMsg), Level::Info);
assert_eq!(QtMsgType::QtCriticalMsg, QtMsgType::from(Level::Error))
}
}