• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_ACE_TRACE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_ACE_TRACE_H
18 
19 #include <atomic>
20 #include <cstdarg>
21 #include <cstdio>
22 #include <memory>
23 #include <string>
24 
25 #include "base/utils/macros.h"
26 #include "base/utils/noncopyable.h"
27 #include "base/utils/system_properties.h"
28 
29 #define ACE_SCOPED_TRACE(fmt, ...) AceScopedTrace aceScopedTrace(fmt, ##__VA_ARGS__)
30 #define ACE_SVG_SCOPED_TRACE(fmt, ...) \
31     AceScopedTraceFlag aceScopedTraceFlag(SystemProperties::GetSvgTraceEnabled(), fmt, ##__VA_ARGS__)
32 #ifdef ACE_DEBUG
33 #define ACE_DEBUG_SCOPED_TRACE(fmt, ...) AceScopedTrace aceScopedTrace(fmt, ##__VA_ARGS__)
34 #else
35 #define ACE_DEBUG_SCOPED_TRACE(fmt, ...)
36 #endif
37 
38 #define ACE_FUNCTION_TRACE() ACE_SCOPED_TRACE(__func__)
39 
40 #define ACE_COUNT_TRACE(count, fmt, ...) AceCountTraceWidthArgs(count, fmt, ##__VA_ARGS__)
41 
42 namespace OHOS::Ace {
43 
44 bool ACE_EXPORT AceTraceEnabled();
45 bool ACE_EXPORT AceAsyncTraceEnable();
46 void ACE_EXPORT AceTraceBegin(const char* name);
47 void ACE_EXPORT AceAsyncTraceBegin(int32_t taskId, const char* name);
48 bool ACE_EXPORT AceTraceBeginWithArgs(const char* format, ...) __attribute__((__format__(printf, 1, 2)));
49 std::string ACE_EXPORT AceAsyncTraceBeginWithArgs(int32_t taskId, char* format, ...);
50 bool ACE_EXPORT AceTraceBeginWithArgv(const char* format, va_list args);
51 std::string ACE_EXPORT AceAsyncTraceBeginWithArgv(int32_t taskId, const char* format, va_list args);
52 void ACE_EXPORT AceTraceEnd();
53 void ACE_EXPORT AceAsyncTraceEnd(int32_t taskId, const char* name);
54 void ACE_EXPORT AceCountTrace(const char *key, int32_t count);
55 void ACE_EXPORT AceCountTraceWidthArgs(int32_t count, const char* format, ...);
56 
57 class ACE_FORCE_EXPORT AceScopedTrace final {
58 public:
59     explicit AceScopedTrace(const char* format, ...) __attribute__((__format__(printf, 2, 3)));
60     ~AceScopedTrace();
61 
62     ACE_DISALLOW_COPY_AND_MOVE(AceScopedTrace);
63 
64 private:
65     bool traceEnabled_ { false };
66 };
67 
68 class ACE_EXPORT AceScopedTraceFlag final {
69 public:
70     explicit AceScopedTraceFlag(bool flag, const char* format, ...) __attribute__((__format__(printf, 3, 4)));
71     ~AceScopedTraceFlag();
72 
73     ACE_DISALLOW_COPY_AND_MOVE(AceScopedTraceFlag);
74 
75 private:
76     bool flagTraceEnabled_ { false };
77 };
78 
79 class ACE_EXPORT AceAsyncScopedTrace final {
80 public:
81     AceAsyncScopedTrace(const char* format, ...);
82     ~AceAsyncScopedTrace();
83 
84     ACE_DISALLOW_COPY_AND_MOVE(AceAsyncScopedTrace);
85 
86 private:
87     bool asyncTraceEnabled_ { false };
88     std::string name_;
89     int32_t taskId_;
90     static std::atomic<std::int32_t> id_;
91 };
92 
93 } // namespace OHOS::Ace
94 
95 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_ACE_TRACE_H
96