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