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 #include "frameworks/base/log/trace_id.h"
17 
18 #include "hitrace/trace.h"
19 
20 namespace OHOS::Ace {
21 
22 class TraceIdImpl : public TraceId {
23 public:
TraceIdImpl()24     TraceIdImpl() : traceId_(std::make_unique<OHOS::HiviewDFX::HiTraceId>(OHOS::HiviewDFX::HiTraceChain::GetId())) {}
25 
26     ~TraceIdImpl() = default;
27 
SetTraceId()28     void SetTraceId() override
29     {
30         if (traceId_ && traceId_->IsValid()) {
31             OHOS::HiviewDFX::HiTraceChain::SetId(*(traceId_.get()));
32         }
33     }
34 
ClearTraceId()35     void ClearTraceId() override
36     {
37         OHOS::HiviewDFX::HiTraceChain::ClearId();
38     }
39 
40 private:
41     std::unique_ptr<OHOS::HiviewDFX::HiTraceId> traceId_;
42 };
43 
CreateTraceId()44 std::shared_ptr<TraceId> TraceId::CreateTraceId()
45 {
46     return std::make_shared<TraceIdImpl>();
47 }
48 
49 } // namespace OHOS::Ace