1 /* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 //! macros crate for Rust. 17 18 /// hilog macros 19 20 #[macro_export] 21 macro_rules! hilog { 22 (@call $log_label:ident, $level:expr, $fmt:literal, $(,)? $($processed_args:expr),* ) => ( 23 let log_str = format!($fmt, $($processed_args),*); 24 let res = unsafe { 25 $crate::HiLogPrint($log_label.log_type as u8, $level as u8, $log_label.domain as u32, 26 CString::new($log_label.tag).expect("default tag").as_ptr() as *const c_char, 27 CString::new(log_str).expect("default log").as_ptr() as *const c_char) 28 }; 29 res 30 ); 31 32 (@rec $priv_flag:ident; $log_label:ident; $level:expr; $fmt:literal; ($arg:expr); $(,)? $($processed_args:expr),*) => { 33 if ($priv_flag) { 34 hilog!(@call $log_label, $level, $fmt, $($processed_args),*, "<private>"); 35 } else { 36 hilog!(@call $log_label, $level, $fmt, $($processed_args),*, $arg); 37 } 38 }; 39 40 (@rec $priv_flag:ident; $log_label:ident; $level:expr; $fmt:literal; (@private($arg:expr)); $(,)? $($processed_args:expr),*) => { 41 if ($priv_flag) { 42 hilog!(@call $log_label, $level, $fmt, $($processed_args),*, "<private>"); 43 } else { 44 hilog!(@call $log_label, $level, $fmt, $($processed_args),*, $arg); 45 } 46 }; 47 48 (@rec $priv_flag:ident; $log_label:ident; $level:expr; $fmt:literal; (@public($arg:expr)); $(,)? $($processed_args:expr),*) => { 49 hilog!(@call $log_label, $level, $fmt, $($processed_args),*, $arg); 50 }; 51 52 (@rec $priv_flag:ident; $log_label:ident; $level:expr; $fmt:literal; ($arg:expr, $($unprocessed_args:tt)*); $($processed_args:tt)*) => { 53 if ($priv_flag) { 54 hilog!(@rec $priv_flag; $log_label; $level; $fmt; ($($unprocessed_args)*); $($processed_args)*, "<private>"); 55 } else { 56 hilog!(@rec $priv_flag; $log_label; $level; $fmt; ($($unprocessed_args)*); $($processed_args)*, $arg); 57 } 58 }; 59 60 (@rec $priv_flag:ident; $log_label:ident; $level:expr; $fmt:literal; (@private($arg:expr), $($unprocessed_args:tt)*); $($processed_args:tt)*) => { 61 if ($priv_flag) { 62 hilog!(@rec $priv_flag; $log_label; $level; $fmt; ($($unprocessed_args)*); $($processed_args)*, "<private>"); 63 } else { 64 hilog!(@rec $priv_flag; $log_label; $level; $fmt; ($($unprocessed_args)*); $($processed_args)*, $arg); 65 } 66 }; 67 68 (@rec $priv_flag:ident; $log_label:ident; $level:expr; $fmt:literal; (@public($arg:expr), $($unprocessed_args:tt)*); $($processed_args:tt)*) => { 69 hilog!(@rec $priv_flag; $log_label; $level; $fmt; ($($unprocessed_args)*); $($processed_args)*, $arg); 70 }; 71 72 // Public API 73 ($log_label:ident, $level:expr, $fmt:literal, $($unprocessed_args:tt)*) => { 74 let priv_flag = unsafe{ $crate::IsPrivateSwitchOn() && !$crate::IsDebugOn() }; 75 hilog!(@rec priv_flag; $log_label; $level; $fmt; ($($unprocessed_args)*);); 76 }; 77 78 ($log_label:ident, $level:expr, $fmt:literal) => { 79 hilog!(@call $log_label, $level, $fmt,); 80 }; 81 } 82 83 /// printf log at the debug level. 84 /// 85 /// #Examples 86 /// 87 /// ``` 88 /// use hilog_rust::{debug, hilog, HiLogLabel, LogType}; 89 /// 90 /// # fn main() { 91 /// let log_label: HiLogLabel = HiLogLabel { 92 /// log_type: LogType::LogCore, 93 /// domain: 0xd003200, 94 /// tag: "testTag" 95 /// }; 96 /// debug!(LOG_LABEL,"testLog{}", "testargs"); 97 /// # } 98 /// ``` 99 #[macro_export] 100 macro_rules! debug{ 101 ($log_label:ident, $($arg:tt)*) => ( 102 hilog!($log_label, $crate::LogLevel::Debug, $($arg)*) 103 ); 104 } 105 106 /// printf log at the info level. 107 /// 108 /// #Examples 109 /// 110 /// ``` 111 /// use hilog_rust::{info, hilog, HiLogLabel, LogType}; 112 /// 113 /// # fn main() { 114 /// let log_label: HiLogLabel = HiLogLabel { 115 /// log_type: LogType::LogCore, 116 /// domain: 0xd003200, 117 /// tag: "testTag" 118 /// }; 119 /// info!(LOG_LABEL,"testLog{}", "testargs"); 120 /// # } 121 /// ``` 122 #[macro_export] 123 macro_rules! info{ 124 ($log_label:ident, $($arg:tt)*) => ( 125 hilog!($log_label, $crate::LogLevel::Info, $($arg)*) 126 ); 127 } 128 129 /// printf log at the warn level. 130 /// 131 /// #Examples 132 /// 133 /// ``` 134 /// use hilog_rust::{warn, hilog, HiLogLabel, LogType}; 135 /// 136 /// # fn main() { 137 /// let log_label: HiLogLabel = HiLogLabel { 138 /// log_type: LogType::LogCore, 139 /// domain: 0xd003200, 140 /// tag: "testTag" 141 /// }; 142 /// warn!(LOG_LABEL,"testLog{}", "testargs"); 143 /// # } 144 /// ``` 145 #[macro_export] 146 macro_rules! warn{ 147 ($log_label:ident, $($arg:tt)*) => ( 148 hilog!($log_label, $crate::LogLevel::Warn, $($arg)*) 149 ); 150 } 151 152 /// printf log at the error level. 153 /// 154 /// #Examples 155 /// 156 /// ``` 157 /// use hilog_rust::{error, hilog, HiLogLabel, LogType}; 158 /// 159 /// # fn main() { 160 /// let log_label: HiLogLabel = HiLogLabel { 161 /// log_type: LogType::LogCore, 162 /// domain: 0xd003200, 163 /// tag: "testTag" 164 /// }; 165 /// error!(LOG_LABEL,"testLog{}", "testargs"); 166 /// # } 167 /// ``` 168 #[macro_export] 169 macro_rules! error{ 170 ($log_label:ident, $($arg:tt)*) => ( 171 hilog!($log_label, $crate::LogLevel::Error, $($arg)*) 172 ); 173 } 174 175 /// printf log at the fatal level. 176 /// 177 /// #Examples 178 /// 179 /// ``` 180 /// use hilog_rust::{fatal, hilog, HiLogLabel, LogType}; 181 /// 182 /// # fn main() { 183 /// let log_label: HiLogLabel = HiLogLabel { 184 /// log_type: LogType::LogCore, 185 /// domain: 0xd003200, 186 /// tag: "testTag" 187 /// }; 188 /// fatal!(LOG_LABEL,"testLog{}", "testargs"); 189 /// # } 190 /// ``` 191 #[macro_export] 192 macro_rules! fatal{ 193 ($log_label:ident, $($arg:tt)*) => ( 194 hilog!($log_label, $crate::LogLevel::Fatal, $($arg)*) 195 ); 196 }