12498b56bSopenharmony_ci/* 22498b56bSopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd. 32498b56bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 42498b56bSopenharmony_ci * you may not use this file except in compliance with the License. 52498b56bSopenharmony_ci * You may obtain a copy of the License at 62498b56bSopenharmony_ci * 72498b56bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 82498b56bSopenharmony_ci * 92498b56bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 102498b56bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 112498b56bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 122498b56bSopenharmony_ci * See the License for the specific language governing permissions and 132498b56bSopenharmony_ci * limitations under the License. 142498b56bSopenharmony_ci */ 152498b56bSopenharmony_ci 162498b56bSopenharmony_ci//! macros crate for Rust. 172498b56bSopenharmony_ci 182498b56bSopenharmony_ci/// hilog macros 192498b56bSopenharmony_ci 202498b56bSopenharmony_ci#[macro_export] 212498b56bSopenharmony_cimacro_rules! hilog { 222498b56bSopenharmony_ci (@call $log_label:ident, $level:expr, $fmt:literal, $(,)? $($processed_args:expr),* ) => ( 232498b56bSopenharmony_ci let _ = Option::<CString>::None; // Use this to avoid `unused` warnings. 242498b56bSopenharmony_ci 252498b56bSopenharmony_ci let mut buf = [0u8; 31]; // All tags ending in `\0` must not exceed 31 bytes in length. 262498b56bSopenharmony_ci let tag = $log_label.tag.as_bytes(); 272498b56bSopenharmony_ci let min_len = std::cmp::min(tag.len(), 30); // 30 is the max length of tag. 282498b56bSopenharmony_ci buf[0..min_len].copy_from_slice(&tag[0..min_len]); 292498b56bSopenharmony_ci 302498b56bSopenharmony_ci let mut log_str = format!($fmt, $($processed_args),*); 312498b56bSopenharmony_ci log_str.push('\0'); 322498b56bSopenharmony_ci let res = unsafe { 332498b56bSopenharmony_ci $crate::HiLogPrint($log_label.log_type as u8, $level as u8, $log_label.domain as u32, 342498b56bSopenharmony_ci buf.as_ptr() as *const c_char, 352498b56bSopenharmony_ci log_str.as_ptr() as *const c_char) 362498b56bSopenharmony_ci }; 372498b56bSopenharmony_ci res 382498b56bSopenharmony_ci ); 392498b56bSopenharmony_ci 402498b56bSopenharmony_ci (@rec $priv_flag:ident; $log_label:ident; $level:expr; $fmt:literal; ($arg:expr); $(,)? $($processed_args:expr),*) => { 412498b56bSopenharmony_ci if ($priv_flag) { 422498b56bSopenharmony_ci hilog!(@call $log_label, $level, $fmt, $($processed_args),*, "<private>"); 432498b56bSopenharmony_ci } else { 442498b56bSopenharmony_ci hilog!(@call $log_label, $level, $fmt, $($processed_args),*, $arg); 452498b56bSopenharmony_ci } 462498b56bSopenharmony_ci }; 472498b56bSopenharmony_ci 482498b56bSopenharmony_ci (@rec $priv_flag:ident; $log_label:ident; $level:expr; $fmt:literal; (@private($arg:expr)); $(,)? $($processed_args:expr),*) => { 492498b56bSopenharmony_ci if ($priv_flag) { 502498b56bSopenharmony_ci hilog!(@call $log_label, $level, $fmt, $($processed_args),*, "<private>"); 512498b56bSopenharmony_ci } else { 522498b56bSopenharmony_ci hilog!(@call $log_label, $level, $fmt, $($processed_args),*, $arg); 532498b56bSopenharmony_ci } 542498b56bSopenharmony_ci }; 552498b56bSopenharmony_ci 562498b56bSopenharmony_ci (@rec $priv_flag:ident; $log_label:ident; $level:expr; $fmt:literal; (@public($arg:expr)); $(,)? $($processed_args:expr),*) => { 572498b56bSopenharmony_ci hilog!(@call $log_label, $level, $fmt, $($processed_args),*, $arg); 582498b56bSopenharmony_ci }; 592498b56bSopenharmony_ci 602498b56bSopenharmony_ci (@rec $priv_flag:ident; $log_label:ident; $level:expr; $fmt:literal; ($arg:expr, $($unprocessed_args:tt)*); $($processed_args:tt)*) => { 612498b56bSopenharmony_ci if ($priv_flag) { 622498b56bSopenharmony_ci hilog!(@rec $priv_flag; $log_label; $level; $fmt; ($($unprocessed_args)*); $($processed_args)*, "<private>"); 632498b56bSopenharmony_ci } else { 642498b56bSopenharmony_ci hilog!(@rec $priv_flag; $log_label; $level; $fmt; ($($unprocessed_args)*); $($processed_args)*, $arg); 652498b56bSopenharmony_ci } 662498b56bSopenharmony_ci }; 672498b56bSopenharmony_ci 682498b56bSopenharmony_ci (@rec $priv_flag:ident; $log_label:ident; $level:expr; $fmt:literal; (@private($arg:expr), $($unprocessed_args:tt)*); $($processed_args:tt)*) => { 692498b56bSopenharmony_ci if ($priv_flag) { 702498b56bSopenharmony_ci hilog!(@rec $priv_flag; $log_label; $level; $fmt; ($($unprocessed_args)*); $($processed_args)*, "<private>"); 712498b56bSopenharmony_ci } else { 722498b56bSopenharmony_ci hilog!(@rec $priv_flag; $log_label; $level; $fmt; ($($unprocessed_args)*); $($processed_args)*, $arg); 732498b56bSopenharmony_ci } 742498b56bSopenharmony_ci }; 752498b56bSopenharmony_ci 762498b56bSopenharmony_ci (@rec $priv_flag:ident; $log_label:ident; $level:expr; $fmt:literal; (@public($arg:expr), $($unprocessed_args:tt)*); $($processed_args:tt)*) => { 772498b56bSopenharmony_ci hilog!(@rec $priv_flag; $log_label; $level; $fmt; ($($unprocessed_args)*); $($processed_args)*, $arg); 782498b56bSopenharmony_ci }; 792498b56bSopenharmony_ci 802498b56bSopenharmony_ci // Public API 812498b56bSopenharmony_ci ($log_label:ident, $level:expr, $fmt:literal, $($unprocessed_args:tt)*) => { 822498b56bSopenharmony_ci let priv_flag = unsafe{ $crate::IsPrivateSwitchOn() && !$crate::IsDebugOn() }; 832498b56bSopenharmony_ci hilog!(@rec priv_flag; $log_label; $level; $fmt; ($($unprocessed_args)*);); 842498b56bSopenharmony_ci }; 852498b56bSopenharmony_ci 862498b56bSopenharmony_ci ($log_label:ident, $level:expr, $fmt:literal) => { 872498b56bSopenharmony_ci hilog!(@call $log_label, $level, $fmt,); 882498b56bSopenharmony_ci }; 892498b56bSopenharmony_ci} 902498b56bSopenharmony_ci 912498b56bSopenharmony_ci/// printf log at the debug level. 922498b56bSopenharmony_ci/// 932498b56bSopenharmony_ci/// #Examples 942498b56bSopenharmony_ci/// 952498b56bSopenharmony_ci/// ``` 962498b56bSopenharmony_ci/// use hilog_rust::{debug, hilog, HiLogLabel, LogType}; 972498b56bSopenharmony_ci/// 982498b56bSopenharmony_ci/// # fn main() { 992498b56bSopenharmony_ci/// let log_label: HiLogLabel = HiLogLabel { 1002498b56bSopenharmony_ci/// log_type: LogType::LogCore, 1012498b56bSopenharmony_ci/// domain: 0xd003200, 1022498b56bSopenharmony_ci/// tag: "testTag", 1032498b56bSopenharmony_ci/// }; 1042498b56bSopenharmony_ci/// debug!(LOG_LABEL, "testLog{}", "testargs"); 1052498b56bSopenharmony_ci/// # } 1062498b56bSopenharmony_ci/// ``` 1072498b56bSopenharmony_ci#[macro_export] 1082498b56bSopenharmony_cimacro_rules! debug{ 1092498b56bSopenharmony_ci ($log_label:ident, $($arg:tt)*) => ( 1102498b56bSopenharmony_ci hilog!($log_label, $crate::LogLevel::Debug, $($arg)*) 1112498b56bSopenharmony_ci ); 1122498b56bSopenharmony_ci} 1132498b56bSopenharmony_ci 1142498b56bSopenharmony_ci/// printf log at the info level. 1152498b56bSopenharmony_ci/// 1162498b56bSopenharmony_ci/// #Examples 1172498b56bSopenharmony_ci/// 1182498b56bSopenharmony_ci/// ``` 1192498b56bSopenharmony_ci/// use hilog_rust::{hilog, info, HiLogLabel, LogType}; 1202498b56bSopenharmony_ci/// 1212498b56bSopenharmony_ci/// # fn main() { 1222498b56bSopenharmony_ci/// let log_label: HiLogLabel = HiLogLabel { 1232498b56bSopenharmony_ci/// log_type: LogType::LogCore, 1242498b56bSopenharmony_ci/// domain: 0xd003200, 1252498b56bSopenharmony_ci/// tag: "testTag", 1262498b56bSopenharmony_ci/// }; 1272498b56bSopenharmony_ci/// info!(LOG_LABEL, "testLog{}", "testargs"); 1282498b56bSopenharmony_ci/// # } 1292498b56bSopenharmony_ci/// ``` 1302498b56bSopenharmony_ci#[macro_export] 1312498b56bSopenharmony_cimacro_rules! info{ 1322498b56bSopenharmony_ci ($log_label:ident, $($arg:tt)*) => ( 1332498b56bSopenharmony_ci hilog!($log_label, $crate::LogLevel::Info, $($arg)*) 1342498b56bSopenharmony_ci ); 1352498b56bSopenharmony_ci} 1362498b56bSopenharmony_ci 1372498b56bSopenharmony_ci/// printf log at the warn level. 1382498b56bSopenharmony_ci/// 1392498b56bSopenharmony_ci/// #Examples 1402498b56bSopenharmony_ci/// 1412498b56bSopenharmony_ci/// ``` 1422498b56bSopenharmony_ci/// use hilog_rust::{hilog, warn, HiLogLabel, LogType}; 1432498b56bSopenharmony_ci/// 1442498b56bSopenharmony_ci/// # fn main() { 1452498b56bSopenharmony_ci/// let log_label: HiLogLabel = HiLogLabel { 1462498b56bSopenharmony_ci/// log_type: LogType::LogCore, 1472498b56bSopenharmony_ci/// domain: 0xd003200, 1482498b56bSopenharmony_ci/// tag: "testTag", 1492498b56bSopenharmony_ci/// }; 1502498b56bSopenharmony_ci/// warn!(LOG_LABEL, "testLog{}", "testargs"); 1512498b56bSopenharmony_ci/// # } 1522498b56bSopenharmony_ci/// ``` 1532498b56bSopenharmony_ci#[macro_export] 1542498b56bSopenharmony_cimacro_rules! warn{ 1552498b56bSopenharmony_ci ($log_label:ident, $($arg:tt)*) => ( 1562498b56bSopenharmony_ci hilog!($log_label, $crate::LogLevel::Warn, $($arg)*) 1572498b56bSopenharmony_ci ); 1582498b56bSopenharmony_ci} 1592498b56bSopenharmony_ci 1602498b56bSopenharmony_ci/// printf log at the error level. 1612498b56bSopenharmony_ci/// 1622498b56bSopenharmony_ci/// #Examples 1632498b56bSopenharmony_ci/// 1642498b56bSopenharmony_ci/// ``` 1652498b56bSopenharmony_ci/// use hilog_rust::{error, hilog, HiLogLabel, LogType}; 1662498b56bSopenharmony_ci/// 1672498b56bSopenharmony_ci/// # fn main() { 1682498b56bSopenharmony_ci/// let log_label: HiLogLabel = HiLogLabel { 1692498b56bSopenharmony_ci/// log_type: LogType::LogCore, 1702498b56bSopenharmony_ci/// domain: 0xd003200, 1712498b56bSopenharmony_ci/// tag: "testTag", 1722498b56bSopenharmony_ci/// }; 1732498b56bSopenharmony_ci/// error!(LOG_LABEL, "testLog{}", "testargs"); 1742498b56bSopenharmony_ci/// # } 1752498b56bSopenharmony_ci/// ``` 1762498b56bSopenharmony_ci#[macro_export] 1772498b56bSopenharmony_cimacro_rules! error{ 1782498b56bSopenharmony_ci ($log_label:ident, $($arg:tt)*) => ( 1792498b56bSopenharmony_ci hilog!($log_label, $crate::LogLevel::Error, $($arg)*) 1802498b56bSopenharmony_ci ); 1812498b56bSopenharmony_ci} 1822498b56bSopenharmony_ci 1832498b56bSopenharmony_ci/// printf log at the fatal level. 1842498b56bSopenharmony_ci/// 1852498b56bSopenharmony_ci/// #Examples 1862498b56bSopenharmony_ci/// 1872498b56bSopenharmony_ci/// ``` 1882498b56bSopenharmony_ci/// use hilog_rust::{fatal, hilog, HiLogLabel, LogType}; 1892498b56bSopenharmony_ci/// 1902498b56bSopenharmony_ci/// # fn main() { 1912498b56bSopenharmony_ci/// let log_label: HiLogLabel = HiLogLabel { 1922498b56bSopenharmony_ci/// log_type: LogType::LogCore, 1932498b56bSopenharmony_ci/// domain: 0xd003200, 1942498b56bSopenharmony_ci/// tag: "testTag", 1952498b56bSopenharmony_ci/// }; 1962498b56bSopenharmony_ci/// fatal!(LOG_LABEL, "testLog{}", "testargs"); 1972498b56bSopenharmony_ci/// # } 1982498b56bSopenharmony_ci/// ``` 1992498b56bSopenharmony_ci#[macro_export] 2002498b56bSopenharmony_cimacro_rules! fatal{ 2012498b56bSopenharmony_ci ($log_label:ident, $($arg:tt)*) => ( 2022498b56bSopenharmony_ci hilog!($log_label, $crate::LogLevel::Fatal, $($arg)*) 2032498b56bSopenharmony_ci ); 2042498b56bSopenharmony_ci} 205