1 /*
2  * Copyright (c) 2022-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 #define LOG_TAG "MiscPasteboardDfx"
17 
18 #include "pasteboard_trace.h"
19 
20 #include <atomic>
21 #include <cstdint>
22 
23 #include "hitrace/trace.h"
24 #include "hitrace_meter.h"
25 #include "pasteboard_hilog.h"
26 
27 namespace OHOS {
28 namespace MiscServices {
29 static constexpr uint64_t BYTRACE_LABEL = HITRACE_TAG_MISC;
30 using OHOS::HiviewDFX::HiTraceChain;
31 using namespace MiscServices;
32 
33 std::atomic_bool PasteboardTrace::isSetBytraceEnabled_ = false;
34 
PasteboardTrace(const std::string & value)35 PasteboardTrace::PasteboardTrace(const std::string &value)
36 {
37     SetBytraceEnable();
38     StartTrace(BYTRACE_LABEL, value);
39 }
40 
~PasteboardTrace()41 PasteboardTrace::~PasteboardTrace()
42 {
43     FinishTrace(BYTRACE_LABEL);
44 }
45 
SetBytraceEnable() const46 bool PasteboardTrace::SetBytraceEnable() const
47 {
48     if (isSetBytraceEnabled_.exchange(true)) {
49         return true;
50     }
51     UpdateTraceLabel();
52     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "success, current tag is true");
53     return true;
54 }
55 } // namespace MiscServices
56 } // namespace OHOS
57