1/*
2 * Copyright (C) 2023 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//! Rust macros defined in crate hisysevent.
17
18/// As macro __FUNC__ defined in C/C++.
19///
20/// # Example
21///
22/// ```
23/// extern crate hisysevent;
24///
25/// let func_name = hisysevent::function!();
26///
27/// ```
28///
29#[macro_export]
30macro_rules! function {
31    () => {{
32        fn hook() {}
33        fn type_name_of<T>(_: T) -> &'static str {
34            std::any::type_name::<T>()
35        }
36        let name = type_name_of(hook);
37        let off_set: usize = 6; // ::hook
38        &name[..name.len() - off_set]
39    }}
40}
41
42/// Build HiSysEventParamType with different type.
43#[macro_export]
44macro_rules! build_param_type {
45    ($param_type:expr, $param_len:expr) => {
46        match $param_type as &str {
47            "bool" => {
48                if $param_len > 0 {
49                    hisysevent::HiSysEventParamType::BoolArray
50                } else {
51                    hisysevent::HiSysEventParamType::Bool
52                }
53            }
54            "i8" => {
55                if $param_len > 0 {
56                    hisysevent::HiSysEventParamType::Int8Array
57                } else {
58                    hisysevent::HiSysEventParamType::Int8
59                }
60            }
61            "u8" => {
62                if $param_len > 0 {
63                    hisysevent::HiSysEventParamType::Uint8Array
64                } else {
65                    hisysevent::HiSysEventParamType::Uint8
66                }
67            }
68            "i16" => {
69                if $param_len > 0 {
70                    hisysevent::HiSysEventParamType::Int16Array
71                } else {
72                    hisysevent::HiSysEventParamType::Int16
73                }
74            }
75            "u16" => {
76                if $param_len > 0 {
77                    hisysevent::HiSysEventParamType::Uint16Array
78                } else {
79                    hisysevent::HiSysEventParamType::Uint16
80                }
81            }
82            "i32" => {
83                if $param_len > 0 {
84                    hisysevent::HiSysEventParamType::Int32Array
85                } else {
86                    hisysevent::HiSysEventParamType::Int32
87                }
88            }
89            "u32" => {
90                if $param_len > 0 {
91                    hisysevent::HiSysEventParamType::Uint32Array
92                } else {
93                    hisysevent::HiSysEventParamType::Uint32
94                }
95            }
96            "i64" => {
97                if $param_len > 0 {
98                    hisysevent::HiSysEventParamType::Int64Array
99                } else {
100                    hisysevent::HiSysEventParamType::Int64
101                }
102            }
103            "u64" => {
104                if $param_len > 0 {
105                    hisysevent::HiSysEventParamType::Uint64Array
106                } else {
107                    hisysevent::HiSysEventParamType::Uint64
108                }
109            }
110            "f32" => {
111                if $param_len > 0 {
112                    hisysevent::HiSysEventParamType::FloatArray
113                } else {
114                    hisysevent::HiSysEventParamType::Float
115                }
116            }
117            "f64" => {
118                if $param_len > 0 {
119                    hisysevent::HiSysEventParamType::DoubleArray
120                } else {
121                    hisysevent::HiSysEventParamType::Double
122                }
123            }
124            "str" => {
125                if $param_len > 0 {
126                    hisysevent::HiSysEventParamType::ParamTypeStringArray
127                } else {
128                    hisysevent::HiSysEventParamType::ParamTypeString
129                }
130            }
131            _ => {
132                if $param_len > 0 {
133                    hisysevent::HiSysEventParamType::BoolArray
134                } else {
135                    hisysevent::HiSysEventParamType::Bool
136                }
137            }
138        }
139    }
140}
141
142/// Build array consist of HiSysEventParamValue with different type except string.
143#[macro_export]
144macro_rules! build_array_params {
145    ($param_name_:expr, $param_value_:expr) => {
146        {
147            let (param_type, param_len) = hisysevent::parse_type_len($param_value_);
148            hisysevent::HiSysEventParam {
149                param_name: $param_name_,
150                param_type: $crate::build_param_type!(param_type, param_len),
151                param_value: hisysevent::HiSysEventParamValue {
152                    void_ptr_: $param_value_.as_ptr() as *const std::ffi::c_int as *const (),
153                },
154                array_size: param_len,
155            }
156        }
157    }
158}
159
160/// Build array consist of HiSysEventParamValue with string type.
161#[macro_export]
162macro_rules! build_string_array_params {
163    ($param_name_:expr, $param_value_:expr) => {
164        hisysevent::build_string_arrays($param_name_, $param_value_)
165    }
166}
167
168/// Build HiSysEventParamValue with different type except string and bool.
169#[macro_export]
170macro_rules! build_param_value {
171    ($param_type:expr, $val:expr) => {
172        match $param_type as &str {
173            "i8" => hisysevent::HiSysEventParamValue {
174                i8_: $val as i8,
175            },
176            "u8" => hisysevent::HiSysEventParamValue {
177                u8_: $val as u8,
178            },
179            "i16" => hisysevent::HiSysEventParamValue {
180                i16_: $val as i16,
181            },
182            "u16" => hisysevent::HiSysEventParamValue {
183                u16_: $val as u16,
184            },
185            "i32" => hisysevent::HiSysEventParamValue {
186                i32_: $val as i32,
187            },
188            "u32" => hisysevent::HiSysEventParamValue {
189                u32_: $val as u32,
190            },
191            "i64" => hisysevent::HiSysEventParamValue {
192                i64_: $val as i64,
193            },
194            "u64" => hisysevent::HiSysEventParamValue {
195                u64_: $val as u64,
196            },
197            "f32" => hisysevent::HiSysEventParamValue {
198                f32_: $val as f32,
199            },
200            "f64" => hisysevent::HiSysEventParamValue {
201                f64_: $val as f64,
202            },
203            _ => hisysevent::HiSysEventParamValue {
204                b_: false,
205            },
206        }
207    }
208}
209
210/// Build HiSysEventParamValue with any number type.
211#[macro_export]
212macro_rules! build_number_param {
213    ($param_name_:expr, $param_value:expr) => {
214        {
215            let (param_type, param_len) = hisysevent::parse_type_len($param_value);
216            hisysevent::HiSysEventParam {
217                param_name: $param_name_,
218                param_type: $crate::build_param_type!(param_type, param_len),
219                param_value: $crate::build_param_value!(param_type, $param_value),
220                array_size: param_len,
221            }
222        }
223    }
224}
225
226/// Build HiSysEventParamValue with bool type.
227#[macro_export]
228macro_rules! build_bool_param {
229    ($param_name_:expr, $param_value:expr) => {
230        {
231            let (param_type, param_len) = hisysevent::parse_type_len($param_value);
232            hisysevent::HiSysEventParam {
233                param_name: $param_name_,
234                param_type: hisysevent::HiSysEventParamType::Bool,
235                param_value: hisysevent::HiSysEventParamValue {
236                    b_: $param_value,
237                },
238                array_size: param_len,
239            }
240        }
241    }
242}
243
244/// Build HiSysEventParamValue with string type.
245#[macro_export]
246macro_rules! build_str_param {
247    ($param_name_:expr, $param_value_:expr) => {
248        {
249            let (param_type, param_len) = hisysevent::parse_type_len($param_value_);
250            hisysevent::HiSysEventParam {
251                param_name: $param_name_,
252                param_type: hisysevent::HiSysEventParamType::ParamTypeString,
253                param_value: {
254                    let str_wrapper = std::ffi::CString::new($param_value_).unwrap();
255                    hisysevent::HiSysEventParamValue {
256                        char_ptr_: str_wrapper.into_raw() as *const std::ffi::c_char,
257                    }
258                },
259                array_size: param_len,
260            }
261        }
262    }
263}
264