Skip to content

Commit 0875bab

Browse files
committed
log: impl From for QtMsgType<=>Level
1 parent 8cf67f9 commit 0875bab

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

qmetaobject/src/log.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub fn install_message_handler(logger: extern "C" fn(QtMsgType, &QMessageLogCont
8787
///
8888
/// [Level]: https://docs.rs/log/0.4.10/log/enum.Level.html
8989
/// [`QtMsgType`]: https://doc.qt.io/qt-5/qtglobal.html#QtMsgType-enum
90-
pub fn map_level(lvl: &QtMsgType) -> Level {
90+
pub fn map_level(lvl: QtMsgType) -> Level {
9191
match lvl {
9292
QtDebugMsg => Level::Debug,
9393
QtInfoMsg => Level::Info,
@@ -120,14 +120,32 @@ pub fn unmap_level(lvl: Level) -> QtMsgType {
120120
}
121121
}
122122

123+
impl From<QtMsgType> for Level {
124+
/// Delegates to [default][] mapping algorithm.
125+
///
126+
/// [default]: ./fn.map_level.html
127+
fn from(lvl: QtMsgType) -> Self {
128+
map_level(lvl)
129+
}
130+
}
131+
132+
impl From<Level> for QtMsgType {
133+
/// Delegates to [default][] mapping algorithm.
134+
///
135+
/// [default]: ./fn.unmap_level.html
136+
fn from(lvl: Level) -> Self {
137+
unmap_level(lvl)
138+
}
139+
}
140+
123141
// Logging middleware, pass-though, or just proxy function.
124142
// It is called from Qt code, then it converts Qt logging data
125143
// into Rust logging facade's log::Record object, and sends it
126144
// to the currently active logger.
127145
extern "C" fn log_capture(msg_type: QtMsgType,
128146
context: &QMessageLogContext,
129147
message: &QString) {
130-
let level = map_level(&msg_type);
148+
let level = msg_type.into();
131149
let target = match context.category() {
132150
"" => "default",
133151
x => x,
@@ -165,7 +183,7 @@ extern "C" fn log_capture(msg_type: QtMsgType,
165183
/// [Rust logging facade][log].
166184
///
167185
/// Most metadata from Qt logging context is retained and passed to [`log::Record`][].
168-
/// Logging levels are mapped with [this][map_level] algorithm.
186+
/// Logging levels are mapped with the [default][map_level] algorithm.
169187
///
170188
/// This function may be called more than once.
171189
///

0 commit comments

Comments
 (0)