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 #ifndef HIVIEWDFX_HITRACECHAIN_CPP_H 17 #define HIVIEWDFX_HITRACECHAIN_CPP_H 18 19 #include "hitrace/hitraceid.h" 20 21 #ifdef __cplusplus 22 23 #include <string> 24 25 namespace OHOS { 26 namespace HiviewDFX { 27 class HiTraceChain final { 28 public: 29 /** 30 * @brief Start tracing a process impl. 31 * @param name name or description for current trace. 32 * @param flags trace flags to be set for current trace. 33 * @return trace id. 34 */ 35 static HiTraceId Begin(const std::string& name, int flags); 36 37 /** 38 * @brief Stop process tracing and clear trace id of current thread 39 * if the given trace id is valid, otherwise do nothing. 40 * @param id the trace id to end. 41 */ 42 static void End(const HiTraceId& id); 43 44 // Get trace id of current thread, and return a invalid trace id if no 45 // trace id belong to current thread. 46 static HiTraceId GetId(); 47 48 /** 49 * @brief Set id as trace id of current thread. Do nothing if id is invalid. 50 * @param id the trace id to set. 51 */ 52 static void SetId(const HiTraceId& id); 53 54 // Clear trace id of current thread and set it invalid. 55 static void ClearId(); 56 57 // Create a new span id according to the trace id of current thread. 58 static HiTraceId CreateSpan(); 59 60 /** 61 * @brief tracepoint hitracechain information. 62 * @param type type of tracepoint. 63 * @param id the trace id of current thread. 64 * @param fmt format of trace information. 65 */ 66 static void Tracepoint(HiTraceTracepointType type, const HiTraceId& id, const char* fmt, ...) 67 __attribute__((__format__(os_log, 3, 4))); 68 69 /** 70 * @brief tracepoint hitracechain information. 71 * @param mode mode of communication. 72 * @param type type of tracepoint. 73 * @param id the trace id of current thread. 74 * @param fmt format of trace information. 75 */ 76 static void Tracepoint(HiTraceCommunicationMode mode, HiTraceTracepointType type, const HiTraceId& id, 77 const char* fmt, ...) __attribute__((__format__(os_log, 4, 5))); 78 79 /** 80 * @brief set the target id and return the old id. 81 * @param id the trace id of target id. 82 */ 83 static HiTraceId SaveAndSet(const HiTraceId& id); 84 85 /** 86 * @brief restore the current thread id. 87 */ 88 static void Restore(const HiTraceId& id); 89 private: 90 HiTraceChain() = default; 91 ~HiTraceChain() = default; 92 }; 93 } // namespace HiviewDFX 94 } // namespace OHOS 95 96 #endif // __cplusplus 97 98 #endif // HIVIEWDFX_HITRACECHAIN_CPP_H 99