Skip to content

Commit 0f8d6ff

Browse files
committed
log: docs & tests
1 parent 70ecda9 commit 0f8d6ff

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct Greeter {
3434
}
3535

3636
fn main() {
37+
qmetaobject::log::init_qt_to_rust();
3738
qml_register_type::<Greeter>(cstr!("Greeter"), 1, 0, cstr!("Greeter"));
3839
let mut engine = QmlEngine::new();
3940
engine.load_data(r#"import QtQuick 2.6; import QtQuick.Window 2.0;

qmetaobject/src/log.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl QMessageLogContext {
6262

6363
/// Wrap Qt's QtMsgType enum
6464
#[repr(C)]
65-
#[derive(Copy, Clone, Debug)]
65+
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
6666
pub enum QtMsgType {
6767
QtDebugMsg,
6868
QtWarningMsg,
@@ -180,14 +180,15 @@ extern "C" fn log_capture(msg_type: QtMsgType,
180180
}
181181
}
182182

183-
/// Installs into Qt logging system a function which forwards messages to the
183+
/// Installs into [Qt logging system][qt-log] a function which forwards messages to the
184184
/// [Rust logging facade][log].
185185
///
186186
/// Most metadata from Qt logging context is retained and passed to [`log::Record`][].
187187
/// Logging levels are mapped with the [default][map_level] algorithm.
188188
///
189189
/// This function may be called more than once.
190190
///
191+
/// [qt-log]: https://doc.qt.io/qt-5/qtglobal.html#qInstallMessageHandler
191192
/// [log]: https://docs.rs/log
192193
/// [`log::Record`]: https://docs.rs/log/0.4.10/log/struct.Record.html
193194
/// [map_level]: ./fn.map_level.html
@@ -197,3 +198,21 @@ pub fn init_qt_to_rust() {
197198
// would want to implement the opposite forwarding logger?
198199
install_message_handler(log_capture);
199200
}
201+
202+
#[cfg(test)]
203+
mod tests {
204+
use super::*;
205+
206+
#[test]
207+
fn test_double_init() {
208+
// must not crash
209+
init_qt_to_rust();
210+
init_qt_to_rust();
211+
}
212+
213+
#[test]
214+
fn test_convert() {
215+
assert_eq!(Level::from(QtInfoMsg), Level::Info);
216+
assert_eq!(QtCriticalMsg, QtMsgType::from(Level::Error))
217+
}
218+
}

0 commit comments

Comments
 (0)