From 8d6fa9526e076c7cf6e805fc0d73630162e0a81c Mon Sep 17 00:00:00 2001 From: Junkui Zhang <364772080@qq.com> Date: Fri, 4 Oct 2024 04:00:33 +0800 Subject: [PATCH] windows: Fix sometimes log error messages don't show the crate name (#18706) On windows, path could be something like `C:\path\to\the\crate`. Hence, `split('/')` would refuse to work in this case. ### Before ![Screenshot 2024-10-04 023652](https://github.com/user-attachments/assets/9c14fb24-5ee0-4b56-8fbd-313abb28f134) ### After ![Screenshot 2024-10-04 024115](https://github.com/user-attachments/assets/217e175c-b0e1-4589-9c3d-98670882b185) Release Notes: - N/A --- crates/util/src/util.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/util/src/util.rs b/crates/util/src/util.rs index a161b8bac2b59fd1280ebd8e8667f1f99bc9337b..50fc070009ebf67b4f1bb3b29954e6437a91fe7d 100644 --- a/crates/util/src/util.rs +++ b/crates/util/src/util.rs @@ -378,9 +378,13 @@ fn log_error_with_caller(caller: core::panic::Location<'_>, error: E, level: where E: std::fmt::Debug, { + #[cfg(not(target_os = "windows"))] + let file = caller.file(); + #[cfg(target_os = "windows")] + let file = caller.file().replace('\\', "/"); // In this codebase, the first segment of the file path is // the 'crates' folder, followed by the crate name. - let target = caller.file().split('/').nth(1); + let target = file.split('/').nth(1); log::logger().log( &log::Record::builder()