Skip to content

Commit

Permalink
fix(common): logger source for component that have generic where inco…
Browse files Browse the repository at this point in the history
…rrect

ie: for a `MithrilProverService::<MKTreeStoreInMemory>` the source name
generated was `MKTreeStoreInMemory>` instead of `MithrilProverService`.
  • Loading branch information
Alenar committed Oct 15, 2024
1 parent 2f5652f commit b922244
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion mithril-common/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ impl LoggerExtensions for Logger {

fn component_name<T>() -> &'static str {
let complete_name = std::any::type_name::<T>();
complete_name.split("::").last().unwrap_or(complete_name)
let without_generic = {
if complete_name.contains('<') {
complete_name.split('<').next().unwrap_or("")
} else {
complete_name
}
};
without_generic.split("::").last().unwrap_or(complete_name)
}

#[cfg(test)]
Expand All @@ -38,6 +45,10 @@ mod tests {
struct TestStructWithLifetime<'a>(&'a str);
enum TestEnum {}

struct TestStructWithGeneric<T> {
_phantom: std::marker::PhantomData<T>,
}

mod test_mod {
pub struct ScopedTestStruct;
pub enum ScopedTestEnum {}
Expand Down Expand Up @@ -66,6 +77,14 @@ mod tests {
component_name::<TestStructWithLifetime>(),
"TestStructWithLifetime"
);
assert_eq!(
component_name::<TestStructWithGeneric<test_mod::ScopedTestStruct>>(),
"TestStructWithGeneric"
);
assert_eq!(
component_name::<TestStructWithGeneric<&str>>(),
"TestStructWithGeneric"
);
}

#[test]
Expand Down

0 comments on commit b922244

Please sign in to comment.