Skip to content

Commit 83557ec

Browse files
committed
glib: Use actual function name for structured logging and also log source file/line number
1 parent 2774f40 commit 83557ec

File tree

2 files changed

+62
-28
lines changed

2 files changed

+62
-28
lines changed

glib/src/log.rs

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -859,36 +859,57 @@ macro_rules! g_printerr {
859859
/// ```
860860
#[macro_export]
861861
macro_rules! log_structured {
862-
($log_domain:expr, $log_level:expr, {$($key:expr => $format:expr $(,$arg:expr)* $(,)?);+ $(;)?} $(,)?) => {
863-
(|| {
864-
let log_domain = <Option<&str> as std::convert::From<_>>::from($log_domain);
865-
let log_domain_str = log_domain.unwrap_or_default();
866-
let level: $crate::LogLevel = $log_level;
867-
let field_count =
868-
<[()]>::len(&[$($crate::log_structured_inner!(@clear $key)),+])
869-
+ log_domain.map(|_| 2usize).unwrap_or(1usize);
870-
871-
$crate::log_structured_array(
872-
level,
873-
&[
874-
$crate::LogField::new(
875-
$crate::gstr!("PRIORITY"),
876-
level.priority().as_bytes(),
877-
),
878-
$(
879-
$crate::LogField::new(
880-
$crate::log_structured_inner!(@key $key),
881-
$crate::log_structured_inner!(@value $format $(,$arg)*),
882-
),
883-
)+
862+
($log_domain:expr, $log_level:expr, {$($key:expr => $format:expr $(,$arg:expr)* $(,)?);+ $(;)?} $(,)?) => {{
863+
let log_domain = <Option<&str> as std::convert::From<_>>::from($log_domain);
864+
let log_domain_str = log_domain.unwrap_or_default();
865+
let level: $crate::LogLevel = $log_level;
866+
let field_count =
867+
<[()]>::len(&[$($crate::log_structured_inner!(@clear $key)),+])
868+
+ log_domain.map(|_| 2usize).unwrap_or(1usize)
869+
+ 3;
870+
871+
let mut line = [0u8; 32]; // 32 decimal digits of line numbers should be enough!
872+
let line = {
873+
use std::io::Write;
874+
875+
let mut cursor = std::io::Cursor::new(&mut line[..]);
876+
std::write!(&mut cursor, "{}", line!()).unwrap();
877+
let pos = cursor.position() as usize;
878+
&line[..pos]
879+
};
880+
881+
$crate::log_structured_array(
882+
level,
883+
&[
884+
$crate::LogField::new(
885+
$crate::gstr!("PRIORITY"),
886+
level.priority().as_bytes(),
887+
),
888+
$crate::LogField::new(
889+
$crate::gstr!("CODE_FILE"),
890+
file!().as_bytes(),
891+
),
892+
$crate::LogField::new(
893+
$crate::gstr!("CODE_LINE"),
894+
line,
895+
),
896+
$crate::LogField::new(
897+
$crate::gstr!("CODE_FUNC"),
898+
$crate::function_name!().as_bytes(),
899+
),
900+
$(
884901
$crate::LogField::new(
885-
$crate::gstr!("GLIB_DOMAIN"),
886-
log_domain_str.as_bytes(),
902+
$crate::log_structured_inner!(@key $key),
903+
$crate::log_structured_inner!(@value $format $(,$arg)*),
887904
),
888-
][0..field_count],
889-
)
890-
})()
891-
};
905+
)+
906+
$crate::LogField::new(
907+
$crate::gstr!("GLIB_DOMAIN"),
908+
log_domain_str.as_bytes(),
909+
),
910+
][0..field_count],
911+
)
912+
}};
892913
}
893914

894915
#[doc(hidden)]

glib/tests/structured_log.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,22 @@ fn structured_log() {
8282
)
8383
})
8484
.collect::<Vec<_>>();
85+
86+
let path = if cfg!(windows) {
87+
"glib\\tests\\structured_log.rs"
88+
} else {
89+
"glib/tests/structured_log.rs"
90+
};
91+
8592
assert_eq!(
8693
log[0],
8794
(
8895
LogLevel::Message,
8996
vec![
9097
("PRIORITY", "5" as &str),
98+
("CODE_FILE", path as &str),
99+
("CODE_LINE", "30" as &str),
100+
("CODE_FUNC", "structured_log::structured_log" as &str),
91101
("MY_META", "abc"),
92102
("MESSAGE", "normal with meta"),
93103
("MY_META2", "def"),
@@ -101,6 +111,9 @@ fn structured_log() {
101111
LogLevel::Message,
102112
vec![
103113
("PRIORITY", "5" as &str),
114+
("CODE_FILE", path as &str),
115+
("CODE_LINE", "40" as &str),
116+
("CODE_FUNC", "structured_log::structured_log" as &str),
104117
("MY_META", "abc"),
105118
("MESSAGE", "formatted with meta: 123 456"),
106119
("MY_META2", "defghi"),

0 commit comments

Comments
 (0)