Skip to content

Commit 260d03d

Browse files
ratijasogoffart
authored andcommitted
log: minor code deduplication
1 parent 91521f8 commit 260d03d

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

qmetaobject/src/log.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::os::raw::c_char;
44

55
#[cfg(feature = "log")]
6-
use log::{Level, logger, Record};
6+
use log::{Level, logger, Record, RecordBuilder};
77

88
use crate::QString;
99

@@ -176,17 +176,18 @@ extern "C" fn log_capture(msg_type: QtMsgType,
176176
.file(file)
177177
.line(line)
178178
.module_path(None);
179-
// (inner) match with single all-capturing arm is a hack that allows us
179+
// Match expression with single all-capturing arm is a hack that allows
180180
// to extend the lifetime of a matched object for "a little longer".
181+
// Passing an expression with temporaries as a function argument
182+
// works exactly the same way.
181183
// Basically, it retains bounded temporary values together with their
182184
// intermediate values etc. This is also the way how println! macro works.
183185
match context.function() {
184-
"" => match format_args!("{}", message) {
185-
args => logger().log(&record.args(args).build()),
186-
},
187-
f => match format_args!("[in {}] {}", f, message) {
188-
args => logger().log(&record.args(args).build()),
189-
}
186+
"" => finish(record, format_args!("{}", message)),
187+
f => finish(record, format_args!("[in {}] {}", f, message)),
188+
}
189+
fn finish<'a>(mut record: RecordBuilder<'a>, args: std::fmt::Arguments<'a>) {
190+
logger().log(&record.args(args).build())
190191
}
191192
}
192193

0 commit comments

Comments
 (0)