1 /*
2  * Copyright (c) 2021 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 "base/log/ace_trace.h"
17 
18 #include "hitrace_meter.h"
19 
20 #include "base/log/log.h"
21 #include "base/utils/macros.h"
22 #include "base/utils/system_properties.h"
23 #include "base/utils/utils.h"
24 
25 namespace OHOS::Ace {
26 namespace {
27 static constexpr uint64_t ACE_TRACE_COMMERCIAL = HITRACE_TAG_ACE | HITRACE_TAG_COMMERCIAL;
28 static constexpr uint64_t ANIMATION_TRACE_COMMERCIAL = HITRACE_TAG_ANIMATION | HITRACE_TAG_COMMERCIAL;
29 }
30 
AceTraceBegin(const char * name)31 void AceTraceBegin(const char* name)
32 {
33     CHECK_NULL_VOID(name);
34     std::string nameStr(name);
35     StartTrace(HITRACE_TAG_ACE, nameStr);
36 }
37 
AceTraceEnd()38 void AceTraceEnd()
39 {
40     FinishTrace(HITRACE_TAG_ACE);
41 }
42 
AceTraceBeginCommercial(const char * name)43 void AceTraceBeginCommercial(const char* name)
44 {
45     StartTrace(ACE_TRACE_COMMERCIAL, name);
46 }
47 
AceTraceEndCommercial()48 void AceTraceEndCommercial()
49 {
50     FinishTrace(ACE_TRACE_COMMERCIAL);
51 }
52 
AceAsyncTraceBegin(int32_t taskId,const char * name,bool isAnimationTrace)53 void AceAsyncTraceBegin(int32_t taskId, const char* name, bool isAnimationTrace)
54 {
55     CHECK_NULL_VOID(name);
56     std::string nameStr(name);
57     if (isAnimationTrace) {
58         StartAsyncTrace(HITRACE_TAG_ANIMATION, nameStr, taskId);
59     } else {
60         StartAsyncTrace(HITRACE_TAG_ACE, nameStr, taskId);
61     }
62 }
63 
AceAsyncTraceEnd(int32_t taskId,const char * name,bool isAnimationTrace)64 void AceAsyncTraceEnd(int32_t taskId, const char* name, bool isAnimationTrace)
65 {
66     CHECK_NULL_VOID(name);
67     std::string nameStr(name);
68     if (isAnimationTrace) {
69         FinishAsyncTrace(HITRACE_TAG_ANIMATION, nameStr, taskId);
70     } else {
71         FinishAsyncTrace(HITRACE_TAG_ACE, nameStr, taskId);
72     }
73 }
74 
AceAsyncTraceBeginCommercial(int32_t taskId,const char * name,bool isAnimationTrace)75 void AceAsyncTraceBeginCommercial(int32_t taskId, const char* name, bool isAnimationTrace)
76 {
77     CHECK_NULL_VOID(name);
78     std::string nameStr(name);
79     if (isAnimationTrace) {
80         StartAsyncTrace(ANIMATION_TRACE_COMMERCIAL, nameStr, taskId);
81     } else {
82         StartAsyncTrace(ACE_TRACE_COMMERCIAL, nameStr, taskId);
83     }
84 }
85 
AceAsyncTraceEndCommercial(int32_t taskId,const char * name,bool isAnimationTrace)86 void AceAsyncTraceEndCommercial(int32_t taskId, const char* name, bool isAnimationTrace)
87 {
88     CHECK_NULL_VOID(name);
89     std::string nameStr(name);
90     if (isAnimationTrace) {
91         FinishAsyncTrace(ANIMATION_TRACE_COMMERCIAL, nameStr, taskId);
92     } else {
93         FinishAsyncTrace(ACE_TRACE_COMMERCIAL, nameStr, taskId);
94     }
95 }
96 
AceCountTrace(const char * key,int32_t count)97 void AceCountTrace(const char *key, int32_t count)
98 {
99     CHECK_NULL_VOID(key);
100     std::string keyStr(key);
101     CountTrace(HITRACE_TAG_ACE, keyStr, count);
102 }
103 } // namespace OHOS::Ace
104