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 hitracechain.
17
18/// tracepoint hitracechain information
19///
20/// # Example
21///
22/// ```
23/// extern crate hitracechain;
24///
25/// let trace_id = hitracechain::get_id();
26/// hitracechain::tracepoint!(hitracechain::HiTraceCommunicationMode::Default,
27///     hitracechain::HiTraceTracepointType::Cs, &trace_id,
28///     "chain id is {}, span id is {}, parent span id is {}",
29///     trace_id.get_chain_id(), trace_id.get_span_id()
30///     trace)id_get_parent_span_id());
31/// hitracechain::end(trace_id);
32/// ```
33/// # Safty
34///
35/// Call C ffi border function, all risks are under control.
36///
37#[macro_export]
38macro_rules! tracepoint {
39    ($communication_mode:expr, $tracepoint_type:expr, $p_id:expr, $($arg:tt)*) => {
40        let args = format!($($arg)*);
41        unsafe {
42            hitracechain::HiTraceChainTracepointExWrapper(
43                $communication_mode as std::ffi::c_int,
44                $tracepoint_type as std::ffi::c_int,
45                $p_id as *const hitracechain::HiTraceId,
46                std::ffi::CString::new(args).expect("").as_ptr() as *const std::ffi::c_char);
47        }
48    }
49}
50
51/// start a new trace
52///
53/// # Example
54///
55/// ```
56/// extern crate hitracechain;
57///
58/// let trace_id = hitracechain::begin!("process name", hitracechain::HiTraceFlag::Default);
59/// hitracechain::end(trace_id);
60/// ```
61///
62#[macro_export]
63macro_rules! begin {
64    ($process_name:literal, $trace_flag:expr) => {
65        hitracechain::begin($process_name, $trace_flag as i32);
66    }
67}