Skip to content

Commit 61a082e

Browse files
authored
Merge pull request gtk-rs#770 from sdroege/logging-improvements
glib: Reduce number of allocations when writing non-structured logs v…
2 parents 3221869 + 037994c commit 61a082e

File tree

8 files changed

+38
-31
lines changed

8 files changed

+38
-31
lines changed

cairo/src/surface.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ mod tests {
421421
/* Initially the data for any mime type has to be none */
422422
assert!(data.is_none());
423423

424-
assert!(surface.set_mime_data(MIME_TYPE_PNG, &[1u8, 10u8]).is_ok());
424+
assert!(surface.set_mime_data(MIME_TYPE_PNG, [1u8, 10u8]).is_ok());
425425
let data = surface.mime_data(MIME_TYPE_PNG).unwrap();
426426
assert_eq!(data, &[1u8, 10u8]);
427427
}

cairo/sys/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ impl cairo_bool_t {
240240

241241
impl From<bool> for cairo_bool_t {
242242
fn from(b: bool) -> cairo_bool_t {
243-
let value = if b { 1 } else { 0 };
243+
let value = c_int::from(b);
244244
cairo_bool_t { value }
245245
}
246246
}

gio/src/list_store.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ mod tests {
161161
let item0 = ListStore::new(ListStore::static_type());
162162
let item1 = ListStore::new(ListStore::static_type());
163163
let mut list = ListStore::new(ListStore::static_type());
164-
list.extend(&[&item0, &item1]);
164+
list.extend([&item0, &item1]);
165165
assert_eq!(list.item(0).as_ref(), Some(item0.upcast_ref()));
166166
assert_eq!(list.item(1).as_ref(), Some(item1.upcast_ref()));
167-
list.extend(&[item0.clone(), item1.clone()]);
167+
list.extend([item0.clone(), item1.clone()]);
168168
assert_eq!(list.item(2).as_ref(), Some(item0.upcast_ref()));
169169
assert_eq!(list.item(3).as_ref(), Some(item1.upcast_ref()));
170170

gio/src/settings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ mod test {
180180
fn set_env() {
181181
INIT.call_once(|| {
182182
let output = Command::new("glib-compile-schemas")
183-
.args(&[
183+
.args([
184184
&format!("{}/tests", env!("CARGO_MANIFEST_DIR")),
185185
"--targetdir",
186186
env!("OUT_DIR"),

glib/src/bridged_logging.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,21 @@ impl GlibLogger {
115115
}
116116

117117
#[doc(alias = "g_log")]
118-
fn write_log(domain: Option<&str>, level: rs_log::Level, message: &str) {
118+
fn write_log(domain: Option<&str>, level: rs_log::Level, message: &std::fmt::Arguments<'_>) {
119119
unsafe {
120+
use std::fmt::Write;
121+
122+
let mut message_builder = crate::GStringBuilder::default();
123+
if write!(&mut message_builder, "{}", message).is_err() {
124+
return;
125+
}
126+
let message = message_builder.into_string();
127+
120128
crate::ffi::g_log(
121129
domain.to_glib_none().0,
122130
GlibLogger::level_to_glib(level).into_glib(),
123131
b"%s\0".as_ptr() as *const _,
124-
ToGlibPtr::<*const std::os::raw::c_char>::to_glib_none(message).0,
132+
ToGlibPtr::<*const std::os::raw::c_char>::to_glib_none(&message).0,
125133
);
126134
}
127135
}
@@ -168,30 +176,26 @@ impl rs_log::Log for GlibLogger {
168176

169177
match self.format {
170178
GlibLoggerFormat::Plain => {
171-
let args = record.args();
172-
if let Some(s) = args.as_str() {
173-
GlibLogger::write_log(domain, record.level(), s);
174-
} else {
175-
GlibLogger::write_log(domain, record.level(), &args.to_string());
176-
}
179+
GlibLogger::write_log(domain, record.level(), record.args());
177180
}
178181
GlibLoggerFormat::LineAndFile => {
179182
match (record.file(), record.line()) {
180183
(Some(file), Some(line)) => {
181-
let s = format!("{}:{}: {}", file, line, record.args());
182-
GlibLogger::write_log(domain, record.level(), &s);
184+
GlibLogger::write_log(
185+
domain,
186+
record.level(),
187+
&format_args!("{}:{}: {}", file, line, record.args()),
188+
);
183189
}
184190
(Some(file), None) => {
185-
let s = format!("{}: {}", file, record.args());
186-
GlibLogger::write_log(domain, record.level(), &s);
191+
GlibLogger::write_log(
192+
domain,
193+
record.level(),
194+
&format_args!("{}: {}", file, record.args()),
195+
);
187196
}
188197
_ => {
189-
let args = record.args();
190-
if let Some(s) = args.as_str() {
191-
GlibLogger::write_log(domain, record.level(), s);
192-
} else {
193-
GlibLogger::write_log(domain, record.level(), &args.to_string());
194-
}
198+
GlibLogger::write_log(domain, record.level(), record.args());
195199
}
196200
};
197201
}

glib/src/gstring.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,7 @@ impl_from_glib_container_as_vec_string!(GString, *const c_char);
10801080
impl_from_glib_container_as_vec_string!(GString, *mut c_char);
10811081

10821082
#[cfg(test)]
1083-
#[allow(clippy::blacklisted_name)]
1083+
#[allow(clippy::disallowed_names)]
10841084
mod tests {
10851085
use super::*;
10861086
use std::ffi::CString;

glib/src/variant_type.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ impl Clone for VariantType {
135135

136136
impl Deref for VariantType {
137137
type Target = VariantTy;
138+
139+
#[allow(clippy::cast_slice_from_raw_parts)]
138140
fn deref(&self) -> &VariantTy {
139141
unsafe {
140142
&*(slice::from_raw_parts(self.ptr.as_ptr() as *const u8, self.len) as *const [u8]
@@ -449,6 +451,7 @@ impl VariantTy {
449451
/// Creates `&VariantTy` with a wildcard lifetime from a `GVariantType`
450452
/// pointer.
451453
#[doc(hidden)]
454+
#[allow(clippy::cast_slice_from_raw_parts)]
452455
pub unsafe fn from_ptr<'a>(ptr: *const ffi::GVariantType) -> &'a VariantTy {
453456
assert!(!ptr.is_null());
454457
let len = ffi::g_variant_type_get_string_length(ptr) as usize;

glib/tests/log.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ fn check_log_handlers() {
3838
let count = Arc::new(Mutex::new(Counters::default()));
3939
log_set_default_handler(clone!(@weak count => move |_, level, _| {
4040
match level {
41-
LogLevel::Critical => { (*count.lock().expect("failed to lock 3")).criticals += 1; }
42-
LogLevel::Warning => { (*count.lock().expect("failed to lock 4")).warnings += 1; }
43-
LogLevel::Message => { (*count.lock().expect("failed to lock 5")).messages += 1; }
44-
LogLevel::Info => { (*count.lock().expect("failed to lock 6")).infos += 1; }
45-
LogLevel::Debug => { (*count.lock().expect("failed to lock 7")).debugs += 1; }
41+
LogLevel::Critical => { count.lock().expect("failed to lock 3").criticals += 1; }
42+
LogLevel::Warning => { count.lock().expect("failed to lock 4").warnings += 1; }
43+
LogLevel::Message => { count.lock().expect("failed to lock 5").messages += 1; }
44+
LogLevel::Info => { count.lock().expect("failed to lock 6").infos += 1; }
45+
LogLevel::Debug => { count.lock().expect("failed to lock 7").debugs += 1; }
4646
_ => unreachable!(),
4747
}
4848
}));
@@ -76,8 +76,8 @@ fn check_log_handlers() {
7676
false,
7777
clone!(@weak count => move |_, level, _| {
7878
match level {
79-
LogLevel::Warning => { (*count.lock().expect("failed to lock 4")).warnings += 1; }
80-
LogLevel::Debug => { (*count.lock().expect("failed to lock 7")).debugs += 1; }
79+
LogLevel::Warning => { count.lock().expect("failed to lock 4").warnings += 1; }
80+
LogLevel::Debug => { count.lock().expect("failed to lock 7").debugs += 1; }
8181
_ => unreachable!(),
8282
}
8383
}),

0 commit comments

Comments
 (0)