From 280b6c8e8641d53923c7321c405c548dcdaea2a7 Mon Sep 17 00:00:00 2001 From: hu-kai45 Date: Sat, 20 Jul 2024 18:45:23 +0800 Subject: [PATCH] =?UTF-8?q?hilog=20rom=20=E4=BC=98=E5=8C=96=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hu-kai45 --- interfaces/rust/src/macros.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/interfaces/rust/src/macros.rs b/interfaces/rust/src/macros.rs index cdca4f4..c3a6cee 100644 --- a/interfaces/rust/src/macros.rs +++ b/interfaces/rust/src/macros.rs @@ -18,11 +18,19 @@ #[macro_export] macro_rules! hilog { (@call $log_label:ident, $level:expr, $fmt:literal, $(,)? $($processed_args:expr),* ) => ( - let log_str = format!($fmt, $($processed_args),*); + let _ = Option::::None; // Use this to avoid `unused` warnings. + + let mut buf = [0u8; 31]; // All tags ending in `\0` must not exceed 31 bytes in length. + let tag = $log_label.tag.as_bytes(); + let min_len = std::cmp::min(tag.len(), 30); // 30 is the max length of tag. + buf[0..min_len].copy_from_slice(&tag[0..min_len]); + + let mut log_str = format!($fmt, $($processed_args),*); + log_str.push('\0'); let res = unsafe { $crate::HiLogPrint($log_label.log_type as u8, $level as u8, $log_label.domain as u32, - CString::new($log_label.tag).expect("default tag").as_ptr() as *const c_char, - CString::new(log_str).expect("default log").as_ptr() as *const c_char) + buf.as_ptr() as *const c_char, + log_str.as_ptr() as *const c_char) }; res ); -- Gitee