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 #endif
23 
24 namespace OHOS::Rosen {
25 #ifdef OHOS_TEXT_ENABLE
26 enum class TextTraceLevel {
27     TEXT_TRACE_LEVEL_DEFAULT,
28     TEXT_TRACE_LEVEL_LOW,
29     TEXT_TRACE_LEVEL_MIDDLE,
30     TEXT_TRACE_LEVEL_HIGH
31 };
32 
33 #define TEXT_TRACE(name) TextOptionalTrace optionalTrace(name)
34 #define TEXT_TRACE_FUNC() TextOptionalTrace optionalTrace(__FUNCTION__)
35 #define TEXT_TRACE_LEVEL(level, name) TextOptionalTrace::TraceWithLevel(level, name, __FUNCTION__)
36 
37 class TextOptionalTrace {
38 public:
TextOptionalTrace(const std::string & traceStr)39     TextOptionalTrace(const std::string& traceStr)
40     {
41         static bool debugTraceEnable =
42             (OHOS::system::GetIntParameter("persist.sys.graphic.openDebugTrace", 0) != 0);
43         if (debugTraceEnable) {
44             std::string name{"Text#"};
45             name.append(traceStr);
46             StartTrace(HITRACE_TAG_GRAPHIC_AGP | HITRACE_TAG_COMMERCIAL, name);
47         }
48     }
49 
~TextOptionalTrace()50     ~TextOptionalTrace()
51     {
52         static bool debugTraceEnable =
53             (OHOS::system::GetIntParameter("persist.sys.graphic.openDebugTrace", 0) != 0);
54         if (debugTraceEnable) {
55             FinishTrace(HITRACE_TAG_GRAPHIC_AGP | HITRACE_TAG_COMMERCIAL);
56         }
57     }
58 
TraceWithLevel(TextTraceLevel level,const std::string & traceStr,const std::string & caller)59     static void TraceWithLevel(TextTraceLevel level, const std::string &traceStr, const std::string &caller)
60     {
61         static int32_t systemLevel =
62             std::atoi(OHOS::system::GetParameter("persist.sys.graphic.openDebugTrace", "0").c_str());
63         if ((systemLevel != 0) && (systemLevel <= static_cast<int32_t>(level))) {
64             HITRACE_METER_FMT(HITRACE_TAG_GRAPHIC_AGP, "Text#%s %s", traceStr.c_str(), caller.c_str());
65         }
66     }
67 };
68 
69 #else
70 #define TEXT_TRACE(name)
71 #define TEXT_TRACE_FUNC()
72 #define TEXT_TRACE_LEVEL(level, name)
73 #endif
74 }
75 #endif