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 _ = Option::<CString>::None;    // Use this to avoid `unused` warnings.
24 
25         let mut buf = [0u8; 31]; // All tags ending in `\0` must not exceed 31 bytes in length.
26         let tag = $log_label.tag.as_bytes();
27         let min_len = std::cmp::min(tag.len(), 30); // 30 is the max length of tag.
28         buf[0..min_len].copy_from_slice(&tag[0..min_len]);
29 
30         let mut log_str = format!($fmt, $($processed_args),*);
31         log_str.push('\0');
32         let res = unsafe {
33             $crate::HiLogPrint($log_label.log_type as u8, $level as u8, $log_label.domain as u32,
34                 buf.as_ptr() as *const c_char,
35                 log_str.as_ptr() as *const c_char)
36         };
37         res
38     );
39 
40     (@rec $priv_flag:ident; $log_label:ident; $level:expr; $fmt:literal; ($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; (@private($arg:expr)); $(,)? $($processed_args:expr),*) => {
49         if ($priv_flag) {
50             hilog!(@call $log_label, $level, $fmt, $($processed_args),*, "<private>");
51         } else {
52             hilog!(@call $log_label, $level, $fmt, $($processed_args),*, $arg);
53         }
54     };
55 
56     (@rec $priv_flag:ident; $log_label:ident; $level:expr; $fmt:literal; (@public($arg:expr)); $(,)? $($processed_args:expr),*) => {
57         hilog!(@call $log_label, $level, $fmt, $($processed_args),*, $arg);
58     };
59 
60     (@rec $priv_flag:ident; $log_label:ident; $level:expr; $fmt:literal; ($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; (@private($arg:expr), $($unprocessed_args:tt)*); $($processed_args:tt)*) => {
69         if ($priv_flag) {
70             hilog!(@rec $priv_flag; $log_label; $level; $fmt; ($($unprocessed_args)*); $($processed_args)*, "<private>");
71         } else {
72             hilog!(@rec $priv_flag; $log_label; $level; $fmt; ($($unprocessed_args)*); $($processed_args)*, $arg);
73         }
74     };
75 
76     (@rec $priv_flag:ident; $log_label:ident; $level:expr; $fmt:literal; (@public($arg:expr), $($unprocessed_args:tt)*); $($processed_args:tt)*) => {
77         hilog!(@rec $priv_flag; $log_label; $level; $fmt; ($($unprocessed_args)*); $($processed_args)*, $arg);
78     };
79 
80     // Public API
81     ($log_label:ident, $level:expr, $fmt:literal, $($unprocessed_args:tt)*) => {
82         let priv_flag = unsafe{ $crate::IsPrivateSwitchOn() && !$crate::IsDebugOn() };
83         hilog!(@rec priv_flag; $log_label; $level; $fmt; ($($unprocessed_args)*););
84     };
85 
86     ($log_label:ident, $level:expr, $fmt:literal) => {
87         hilog!(@call $log_label, $level, $fmt,);
88     };
89 }
90 
91 /// printf log at the debug level.
92 ///
93 /// #Examples
94 ///
95 /// ```
96 /// use hilog_rust::{debug, hilog, HiLogLabel, LogType};
97 ///
98 /// # fn main() {
99 /// let log_label: HiLogLabel = HiLogLabel {
100 ///     log_type: LogType::LogCore,
101 ///     domain: 0xd003200,
102 ///     tag: "testTag",
103 /// };
104 /// debug!(LOG_LABEL, "testLog{}", "testargs");
105 /// # }
106 /// ```
107 #[macro_export]
108 macro_rules! debug{
109     ($log_label:ident, $($arg:tt)*) => (
110         hilog!($log_label, $crate::LogLevel::Debug, $($arg)*)
111     );
112 }
113 
114 ///  printf log at the info level.
115 ///
116 /// #Examples
117 ///
118 /// ```
119 /// use hilog_rust::{hilog, info, HiLogLabel, LogType};
120 ///
121 /// # fn main() {
122 /// let log_label: HiLogLabel = HiLogLabel {
123 ///     log_type: LogType::LogCore,
124 ///     domain: 0xd003200,
125 ///     tag: "testTag",
126 /// };
127 /// info!(LOG_LABEL, "testLog{}", "testargs");
128 /// # }
129 /// ```
130 #[macro_export]
131 macro_rules! info{
132     ($log_label:ident, $($arg:tt)*) => (
133         hilog!($log_label, $crate::LogLevel::Info, $($arg)*)
134     );
135 }
136 
137 ///  printf log at the warn level.
138 ///
139 /// #Examples
140 ///
141 /// ```
142 /// use hilog_rust::{hilog, warn, HiLogLabel, LogType};
143 ///
144 /// # fn main() {
145 /// let log_label: HiLogLabel = HiLogLabel {
146 ///     log_type: LogType::LogCore,
147 ///     domain: 0xd003200,
148 ///     tag: "testTag",
149 /// };
150 /// warn!(LOG_LABEL, "testLog{}", "testargs");
151 /// # }
152 /// ```
153 #[macro_export]
154 macro_rules! warn{
155     ($log_label:ident, $($arg:tt)*) => (
156         hilog!($log_label, $crate::LogLevel::Warn, $($arg)*)
157     );
158 }
159 
160 ///  printf log at the error level.
161 ///
162 /// #Examples
163 ///
164 /// ```
165 /// use hilog_rust::{error, hilog, HiLogLabel, LogType};
166 ///
167 /// # fn main() {
168 /// let log_label: HiLogLabel = HiLogLabel {
169 ///     log_type: LogType::LogCore,
170 ///     domain: 0xd003200,
171 ///     tag: "testTag",
172 /// };
173 /// error!(LOG_LABEL, "testLog{}", "testargs");
174 /// # }
175 /// ```
176 #[macro_export]
177 macro_rules! error{
178     ($log_label:ident, $($arg:tt)*) => (
179         hilog!($log_label, $crate::LogLevel::Error, $($arg)*)
180     );
181 }
182 
183 /// printf log at the fatal level.
184 ///
185 /// #Examples
186 ///
187 /// ```
188 /// use hilog_rust::{fatal, hilog, HiLogLabel, LogType};
189 ///
190 /// # fn main() {
191 /// let log_label: HiLogLabel = HiLogLabel {
192 ///     log_type: LogType::LogCore,
193 ///     domain: 0xd003200,
194 ///     tag: "testTag",
195 /// };
196 /// fatal!(LOG_LABEL, "testLog{}", "testargs");
197 /// # }
198 /// ```
199 #[macro_export]
200 macro_rules! fatal{
201     ($log_label:ident, $($arg:tt)*) => (
202         hilog!($log_label, $crate::LogLevel::Fatal, $($arg)*)
203     );
204 }
205