• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 TEXT_TRACE_H
17 #define TEXT_TRACE_H
18 
19 #ifdef ENABLE_OHOS_ENHANCE
20 #include "hitrace_meter.h"
21 #include "parameters.h"
22 #include "text_common.h"
23 #elif defined(CROSS_PLATFORM)
24 #include "hitrace_meter.h"
25 #endif
26 
27 namespace OHOS::Rosen {
28 #if defined(ENABLE_OHOS_ENHANCE) || defined(CROSS_PLATFORM)
29 enum class TextTraceLevel {
30     TEXT_TRACE_LEVEL_DEFAULT,
31     TEXT_TRACE_LEVEL_LOW,
32     TEXT_TRACE_LEVEL_MIDDLE,
33     TEXT_TRACE_LEVEL_HIGH
34 };
35 
36 #define TEXT_TRACE(name) OHOS::Rosen::TextOptionalTrace optionalTrace(name)
37 #define TEXT_TRACE_FUNC() OHOS::Rosen::TextOptionalTrace optionalTrace(__PRETTY_FUNCTION__)
38 #define TEXT_TRACE_LEVEL(level, name) OHOS::Rosen::TextOptionalTrace::TraceWithLevel(level, name, __PRETTY_FUNCTION__)
39 
40 class TextOptionalTrace {
41 public:
TextOptionalTrace(std::string traceStr)42     TextOptionalTrace(std::string traceStr)
43     {
44 #ifdef ENABLE_OHOS_ENHANCE
45         static bool debugTraceEnable = (OHOS::system::GetIntParameter("persist.sys.graphic.openDebugTrace", 0) != 0);
46         if (UNLIKELY(debugTraceEnable)) {
47             std::string name { "Text#" };
48             CutPrettyFunction(traceStr);
49             name.append(traceStr);
50             StartTrace(HITRACE_TAG_GRAPHIC_AGP | HITRACE_TAG_COMMERCIAL, name);
51         }
52 #elif defined(CROSS_PLATFORM)
53         std::string name { "Text#" };
54         CutPrettyFunction(traceStr);
55         name.append(traceStr);
56         StartTrace(HITRACE_TAG_GRAPHIC_AGP | HITRACE_TAG_COMMERCIAL, name);
57 #endif
58     }
59 
~TextOptionalTrace()60     ~TextOptionalTrace()
61     {
62 #ifdef ENABLE_OHOS_ENHANCE
63         static bool debugTraceEnable = (OHOS::system::GetIntParameter("persist.sys.graphic.openDebugTrace", 0) != 0);
64         if (UNLIKELY(debugTraceEnable)) {
65             FinishTrace(HITRACE_TAG_GRAPHIC_AGP | HITRACE_TAG_COMMERCIAL);
66         }
67 #elif defined(CROSS_PLATFORM)
68         FinishTrace(HITRACE_TAG_GRAPHIC_AGP | HITRACE_TAG_COMMERCIAL);
69 #endif
70     }
71 
72     // Simplify __PRETTY_FUNCTION__ to only return class name and function name
73     // case: std::unique_str<XXX::XXX::Xxx> XXX::XXX::ClassName::FunctionName()
74     // retrun: ClassName::FunctionName
CutPrettyFunction(std::string & str)75     static void CutPrettyFunction(std::string& str)
76     {
77         // find last '('
78         size_t endIndex = str.rfind('(');
79         if (endIndex == std::string::npos) {
80             return;
81         }
82 
83         // find the third ':' before '('
84         auto rIter = std::make_reverse_iterator(str.begin() + endIndex);
85         size_t count = 0;
86         size_t startIndex = 0;
87         for (; rIter != str.rend(); ++rIter) {
88             if (*rIter == ':') {
89                 count += 1;
90                 if (count == 3) { // 3 means to stop iterating when reaching the third ':'
91                     startIndex = str.rend() - rIter;
92                     break;
93                 }
94             }
95         }
96         str = str.substr(startIndex, endIndex - startIndex);
97     }
98 
TraceWithLevel(TextTraceLevel level,const std::string & traceStr,std::string caller)99     static void TraceWithLevel(TextTraceLevel level, const std::string& traceStr, std::string caller)
100     {
101 #ifdef ENABLE_OHOS_ENHANCE
102         static int32_t systemLevel =
103             std::atoi(OHOS::system::GetParameter("persist.sys.graphic.openDebugTrace", "0").c_str());
104         if ((systemLevel != 0) && (systemLevel <= static_cast<int32_t>(level))) {
105             CutPrettyFunction(caller);
106             HITRACE_METER_FMT(HITRACE_TAG_GRAPHIC_AGP, "Text#%s %s", traceStr.c_str(), caller.c_str());
107         }
108 #elif defined(CROSS_PLATFORM)
109         CutPrettyFunction(caller);
110         HITRACE_METER_FMT(HITRACE_TAG_GRAPHIC_AGP, "Text#%s %s", traceStr.c_str(), caller.c_str());
111 #endif
112     }
113 };
114 
115 #else
116 #define TEXT_TRACE(name)
117 #define TEXT_TRACE_FUNC()
118 #define TEXT_TRACE_LEVEL(level, name)
119 #endif
120 } // namespace OHOS::Rosen
121 #endif