1 /*
2  * Copyright (c) 2023 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 ROSEN_MODULES_TEXGINE_EXPORT_TEXGINE_UTILS_TRACE_H
17 #define ROSEN_MODULES_TEXGINE_EXPORT_TEXGINE_UTILS_TRACE_H
18 
19 #include <string>
20 
21 namespace OHOS {
22 namespace Rosen {
23 namespace TextEngine {
24 class ScopedTrace;
25 
26 class Trace {
27 public:
28     /*
29      * @brief This method will be called when the scope trace starts.
30      * @param proc The process name for scope tracking.
31      */
32     static void Start(const std::string &proc);
33 
34     /*
35      * @brief End the scope tracking
36      */
37     static void Finish();
38 
39     /*
40      * @brief Append the count of key to trace content.
41      * @param key The proc what you what appned that counts to trace content
42      * @param val The count of the key
43      */
44     static void Count(const std::string &key, int val);
45 
46     /*
47      * @brief Disable the trace
48      */
49     static void Disable();
50 };
51 
52 class ScopedTrace {
53 public:
ScopedTrace(const std::string &proc)54     ScopedTrace(const std::string &proc)
55     {
56 #ifdef LOGGER_ENABLE_SCOPE
57         Trace::Start(proc);
58 #endif
59     }
60 
~ScopedTrace()61     ~ScopedTrace()
62     {
63         Finish();
64     }
65 
66     /*
67      * @brief End the scope tracking
68      */
Finish()69     void Finish()
70     {
71         if (isFinished == false) {
72             isFinished = true;
73 #ifdef LOGGER_ENABLE_SCOPE
74             Trace::Finish();
75 #endif
76         }
77     }
78 
79 private:
80     bool isFinished = false;
81 };
82 } // namespace TextEngine
83 } // namespace Rosen
84 } // namespace OHOS
85 
86 #endif // ROSEN_MODULES_TEXGINE_EXPORT_TEXGINE_UTILS_TRACE_H
87