1 /* 2 * Copyright (c) 2021-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_HITRACE_ID_H 17 #define HIVIEWDFX_HITRACE_ID_H 18 19 #include "hitrace/hitracechainc.h" 20 21 #ifdef __cplusplus 22 23 namespace OHOS { 24 namespace HiviewDFX { 25 class HiTraceId final { 26 public: 27 HiTraceId(); 28 HiTraceId(const HiTraceIdStruct& id); 29 HiTraceId(const uint8_t* pIdArray, int len); 30 ~HiTraceId() = default; 31 32 // Judge whether the trace id is valid or not. 33 bool IsValid() const; 34 35 /** 36 * @brief Judge whether the trace id has enabled a trace flag or not. 37 * @param flag the trace flag to check. 38 * @return true means this trace flag has been enabled. 39 */ 40 bool IsFlagEnabled(HiTraceFlag flag) const; 41 42 /** 43 * @brief Enable the designative trace flag for the trace id. 44 * @param flag the trace flag to check. 45 */ 46 void EnableFlag(HiTraceFlag flag); 47 48 // Get trace flag of the trace id. 49 int GetFlags() const; 50 51 /** 52 * @brief Set trace flag for the trace id. 53 * @param flag the trace flag to set. 54 */ 55 void SetFlags(int flags); 56 57 // Get chain id of the trace id. 58 uint64_t GetChainId() const; 59 60 /** 61 * @brief Set chain id for the trace id. 62 * @param chainId the chain id to set. 63 */ 64 void SetChainId(uint64_t chainId); 65 66 // Get span id of the trace id. 67 uint64_t GetSpanId() const; 68 69 /** 70 * @brief Set span id for the trace id. 71 * @param spanId the span id to set. 72 */ 73 void SetSpanId(uint64_t spanId); 74 75 // Get parent span id of the trace id. 76 uint64_t GetParentSpanId() const; 77 78 /** 79 * @brief Set parent span id for the trace id. 80 * @param parentSpanId the parent span id to set. 81 */ 82 void SetParentSpanId(uint64_t parentSpanId); 83 84 /** 85 * @brief Serialize the trace id into bytes 86 * @param pIdArray bytes array which load the serialized trace id. 87 * @param len len of the array. 88 */ 89 int ToBytes(uint8_t* pIdArray, int len) const; 90 91 private: 92 HiTraceIdStruct id_; 93 friend class HiTraceChain; 94 }; 95 } // namespace HiviewDFX 96 } // namespace OHOS 97 98 #endif // __cplusplus 99 100 #endif // HIVIEWDFX_HITRACE_ID_H 101