• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 //! Rust macros defined in crate hisysevent.
15 
16 /// As macro __FUNC__ defined in C/C++.
17 ///
18 /// # Example
19 ///
20 /// ```
21 /// extern crate hisysevent;
22 ///
23 /// let func_name = hisysevent::function!();
24 /// ```
25 #[macro_export]
26 macro_rules! function {
27     () => {{
28         fn hook() {}
29         fn type_name_of<T>(_: T) -> &'static str {
30             std::any::type_name::<T>()
31         }
32         let name = type_name_of(hook);
33         let off_set: usize = 6; // ::hook
34         &name[..name.len() - off_set]
35     }};
36 }
37 
38 /// Build HiSysEventParamType with different type.
39 #[macro_export]
40 macro_rules! build_param_type {
41     ($param_type:expr, $param_len:expr) => {
42         match $param_type as &str {
43             "bool" => {
44                 if $param_len > 0 {
45                     hisysevent::HiSysEventParamType::BoolArray
46                 } else {
47                     hisysevent::HiSysEventParamType::Bool
48                 }
49             }
50             "i8" => {
51                 if $param_len > 0 {
52                     hisysevent::HiSysEventParamType::Int8Array
53                 } else {
54                     hisysevent::HiSysEventParamType::Int8
55                 }
56             }
57             "u8" => {
58                 if $param_len > 0 {
59                     hisysevent::HiSysEventParamType::Uint8Array
60                 } else {
61                     hisysevent::HiSysEventParamType::Uint8
62                 }
63             }
64             "i16" => {
65                 if $param_len > 0 {
66                     hisysevent::HiSysEventParamType::Int16Array
67                 } else {
68                     hisysevent::HiSysEventParamType::Int16
69                 }
70             }
71             "u16" => {
72                 if $param_len > 0 {
73                     hisysevent::HiSysEventParamType::Uint16Array
74                 } else {
75                     hisysevent::HiSysEventParamType::Uint16
76                 }
77             }
78             "i32" => {
79                 if $param_len > 0 {
80                     hisysevent::HiSysEventParamType::Int32Array
81                 } else {
82                     hisysevent::HiSysEventParamType::Int32
83                 }
84             }
85             "u32" => {
86                 if $param_len > 0 {
87                     hisysevent::HiSysEventParamType::Uint32Array
88                 } else {
89                     hisysevent::HiSysEventParamType::Uint32
90                 }
91             }
92             "i64" => {
93                 if $param_len > 0 {
94                     hisysevent::HiSysEventParamType::Int64Array
95                 } else {
96                     hisysevent::HiSysEventParamType::Int64
97                 }
98             }
99             "u64" => {
100                 if $param_len > 0 {
101                     hisysevent::HiSysEventParamType::Uint64Array
102                 } else {
103                     hisysevent::HiSysEventParamType::Uint64
104                 }
105             }
106             "f32" => {
107                 if $param_len > 0 {
108                     hisysevent::HiSysEventParamType::FloatArray
109                 } else {
110                     hisysevent::HiSysEventParamType::Float
111                 }
112             }
113             "f64" => {
114                 if $param_len > 0 {
115                     hisysevent::HiSysEventParamType::DoubleArray
116                 } else {
117                     hisysevent::HiSysEventParamType::Double
118                 }
119             }
120             "str" => {
121                 if $param_len > 0 {
122                     hisysevent::HiSysEventParamType::ParamTypeStringArray
123                 } else {
124                     hisysevent::HiSysEventParamType::ParamTypeString
125                 }
126             }
127             _ => {
128                 if $param_len > 0 {
129                     hisysevent::HiSysEventParamType::BoolArray
130                 } else {
131                     hisysevent::HiSysEventParamType::Bool
132                 }
133             }
134         }
135     };
136 }
137 
138 /// Build array consist of HiSysEventParamValue with different type except
139 /// string.
140 #[macro_export]
141 macro_rules! build_array_params {
142     ($param_name_:expr, $param_value_:expr) => {{
143         let (param_type, param_len) = hisysevent::parse_type_len($param_value_);
144         hisysevent::HiSysEventParam {
145             param_name: $param_name_,
146             param_type: $crate::build_param_type!(param_type, param_len),
147             param_value: hisysevent::HiSysEventParamValue {
148                 void_ptr_: $param_value_.as_ptr() as *const std::ffi::c_int as *const (),
149             },
150             array_size: param_len,
151         }
152     }};
153 }
154 
155 /// Build array consist of HiSysEventParamValue with string type.
156 #[macro_export]
157 macro_rules! build_string_array_params {
158     ($param_name_:expr, $param_value_:expr) => {
159         hisysevent::build_string_arrays($param_name_, $param_value_)
160     };
161 }
162 
163 /// Build HiSysEventParamValue with different type except string and bool.
164 #[macro_export]
165 macro_rules! build_param_value {
166     ($param_type:expr, $val:expr) => {
167         match $param_type as &str {
168             "i8" => hisysevent::HiSysEventParamValue { i8_: $val as i8 },
169             "u8" => hisysevent::HiSysEventParamValue { u8_: $val as u8 },
170             "i16" => hisysevent::HiSysEventParamValue { i16_: $val as i16 },
171             "u16" => hisysevent::HiSysEventParamValue { u16_: $val as u16 },
172             "i32" => hisysevent::HiSysEventParamValue { i32_: $val as i32 },
173             "u32" => hisysevent::HiSysEventParamValue { u32_: $val as u32 },
174             "i64" => hisysevent::HiSysEventParamValue { i64_: $val as i64 },
175             "u64" => hisysevent::HiSysEventParamValue { u64_: $val as u64 },
176             "f32" => hisysevent::HiSysEventParamValue { f32_: $val as f32 },
177             "f64" => hisysevent::HiSysEventParamValue { f64_: $val as f64 },
178             _ => hisysevent::HiSysEventParamValue { b_: false },
179         }
180     };
181 }
182 
183 /// Build HiSysEventParamValue with any number type.
184 #[macro_export]
185 macro_rules! build_number_param {
186     ($param_name_:expr, $param_value:expr) => {{
187         let (param_type, param_len) = hisysevent::parse_type_len($param_value);
188         hisysevent::HiSysEventParam {
189             param_name: $param_name_,
190             param_type: $crate::build_param_type!(param_type, param_len),
191             param_value: $crate::build_param_value!(param_type, $param_value),
192             array_size: param_len,
193         }
194     }};
195 }
196 
197 /// Build HiSysEventParamValue with bool type.
198 #[macro_export]
199 macro_rules! build_bool_param {
200     ($param_name_:expr, $param_value:expr) => {{
201         let (param_type, param_len) = hisysevent::parse_type_len($param_value);
202         hisysevent::HiSysEventParam {
203             param_name: $param_name_,
204             param_type: hisysevent::HiSysEventParamType::Bool,
205             param_value: hisysevent::HiSysEventParamValue { b_: $param_value },
206             array_size: param_len,
207         }
208     }};
209 }
210 
211 /// Build HiSysEventParamValue with string type.
212 #[macro_export]
213 macro_rules! build_str_param {
214     ($param_name_:expr, $param_value_:expr) => {{
215         let (param_type, param_len) = hisysevent::parse_type_len($param_value_);
216         hisysevent::HiSysEventParam {
217             param_name: $param_name_,
218             param_type: hisysevent::HiSysEventParamType::ParamTypeString,
219             param_value: {
220                 let str_wrapper = std::ffi::CString::new($param_value_).unwrap();
221                 hisysevent::HiSysEventParamValue {
222                     char_ptr_: str_wrapper.into_raw() as *const std::ffi::c_char,
223                 }
224             },
225             array_size: param_len,
226         }
227     }};
228 }
229