• 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 namespace OHOS::Rosen {
23     #define LIKELY(exp) (__builtin_expect((exp) != 0, true))
24     #define UNLIKELY(exp) (__builtin_expect((exp) != 0, false))
25 }
26 #endif
27 
28 namespace OHOS::Rosen {
29 #ifdef ENABLE_OHOS_ENHANCE
30 enum class TextTraceLevel {
31     TEXT_TRACE_LEVEL_DEFAULT,
32     TEXT_TRACE_LEVEL_LOW,
33     TEXT_TRACE_LEVEL_MIDDLE,
34     TEXT_TRACE_LEVEL_HIGH
35 };
36 
37 #define TEXT_TRACE(name) OHOS::Rosen::TextOptionalTrace optionalTrace(name)
38 #define TEXT_TRACE_FUNC() OHOS::Rosen::TextOptionalTrace optionalTrace(__PRETTY_FUNCTION__)
39 #define TEXT_TRACE_LEVEL(level, name) OHOS::Rosen::TextOptionalTrace::TraceWithLevel(level, name, __PRETTY_FUNCTION__)
40 
41 class TextOptionalTrace {
42 public:
TextOptionalTrace(std::string traceStr)43     TextOptionalTrace(std::string traceStr)
44     {
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     }
53 
~TextOptionalTrace()54     ~TextOptionalTrace()
55     {
56         static bool debugTraceEnable = (OHOS::system::GetIntParameter("persist.sys.graphic.openDebugTrace", 0) != 0);
57         if (UNLIKELY(debugTraceEnable)) {
58             FinishTrace(HITRACE_TAG_GRAPHIC_AGP | HITRACE_TAG_COMMERCIAL);
59         }
60     }
61 
62     // Simplify __PRETTY_FUNCTION__ to only return class name and function name
63     // case: std::unique_str<XXX::XXX::Xxx> XXX::XXX::ClassName::FunctionName()
64     // retrun: ClassName::FunctionName
CutPrettyFunction(std::string & str)65     static void CutPrettyFunction(std::string& str)
66     {
67         // find last '('
68         size_t endIndex = str.rfind('(');
69         if (endIndex == std::string::npos) {
70             return;
71         }
72 
73         // find the third ':' before '('
74         auto rIter = std::make_reverse_iterator(str.begin() + endIndex);
75         size_t count = 0;
76         size_t startIndex = 0;
77         for (; rIter != str.rend(); ++rIter) {
78             if (*rIter == ':') {
79                 count += 1;
80                 if (count == 3) { // 3 means to stop iterating when reaching the third ':'
81                     startIndex = str.rend() - rIter;
82                     break;
83                 }
84             }
85         }
86         str = str.substr(startIndex, endIndex - startIndex);
87     }
88 
TraceWithLevel(TextTraceLevel level,const std::string & traceStr,std::string caller)89     static void TraceWithLevel(TextTraceLevel level, const std::string& traceStr, std::string caller)
90     {
91         static int32_t systemLevel =
92             std::atoi(OHOS::system::GetParameter("persist.sys.graphic.openDebugTrace", "0").c_str());
93         if ((systemLevel != 0) && (systemLevel <= static_cast<int32_t>(level))) {
94             CutPrettyFunction(caller);
95             HITRACE_METER_FMT(HITRACE_TAG_GRAPHIC_AGP, "Text#%s %s", traceStr.c_str(), caller.c_str());
96         }
97     }
98 };
99 
100 #else
101 #define TEXT_TRACE(name)
102 #define TEXT_TRACE_FUNC()
103 #define TEXT_TRACE_LEVEL(level, name)
104 #endif
105 } // namespace OHOS::Rosen
106 #endif