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 #define ACE_LAYOUT_SCOPED_TRACE(fmt, ...) \ 33 AceScopedTraceFlag aceScopedTraceFlag(SystemProperties::GetLayoutTraceEnabled(), fmt, ##__VA_ARGS__) 34 // Enable trace for component creation and attribute settings 35 #define ACE_BUILD_SCOPED_TRACE(fmt, ...) \ 36 AceScopedTraceFlag aceScopedTraceFlag(SystemProperties::GetBuildTraceEnabled(), fmt, ##__VA_ARGS__) 37 #ifdef ACE_DEBUG 38 #define ACE_DEBUG_SCOPED_TRACE(fmt, ...) AceScopedTrace aceScopedTrace(fmt, ##__VA_ARGS__) 39 #else 40 #define ACE_DEBUG_SCOPED_TRACE(fmt, ...) 41 #endif 42 43 #define ACE_FUNCTION_TRACE() ACE_SCOPED_TRACE(__func__) 44 45 #define ACE_COUNT_TRACE(count, fmt, ...) AceCountTraceWidthArgs(count, fmt, ##__VA_ARGS__) 46 47 namespace OHOS::Ace { 48 49 bool ACE_EXPORT AceTraceEnabled(); 50 bool ACE_EXPORT AceAsyncTraceEnable(); 51 void ACE_EXPORT AceTraceBegin(const char* name); 52 void ACE_EXPORT AceAsyncTraceBegin(int32_t taskId, const char* name, bool isAnimationTrace = false); 53 bool ACE_EXPORT AceTraceBeginWithArgs(const char* format, ...) __attribute__((__format__(printf, 1, 2))); 54 std::string ACE_EXPORT AceAsyncTraceBeginWithArgs(int32_t taskId, char* format, ...); 55 bool ACE_EXPORT AceTraceBeginWithArgv(const char* format, va_list args); 56 std::string ACE_EXPORT AceAsyncTraceBeginWithArgv(int32_t taskId, const char* format, va_list args); 57 void ACE_EXPORT AceTraceEnd(); 58 void ACE_EXPORT AceAsyncTraceEnd(int32_t taskId, const char* name, bool isAnimationTrace = false); 59 void ACE_EXPORT AceCountTrace(const char *key, int32_t count); 60 void ACE_EXPORT AceCountTraceWidthArgs(int32_t count, const char* format, ...); 61 62 class ACE_FORCE_EXPORT AceScopedTrace final { 63 public: 64 explicit AceScopedTrace(const char* format, ...) __attribute__((__format__(printf, 2, 3))); 65 ~AceScopedTrace(); 66 67 ACE_DISALLOW_COPY_AND_MOVE(AceScopedTrace); 68 69 private: 70 bool traceEnabled_ { false }; 71 }; 72 73 class ACE_EXPORT AceScopedTraceFlag final { 74 public: 75 explicit AceScopedTraceFlag(bool flag, const char* format, ...) __attribute__((__format__(printf, 3, 4))); 76 ~AceScopedTraceFlag(); 77 78 ACE_DISALLOW_COPY_AND_MOVE(AceScopedTraceFlag); 79 80 private: 81 bool flagTraceEnabled_ { false }; 82 }; 83 84 class ACE_EXPORT AceAsyncScopedTrace final { 85 public: 86 AceAsyncScopedTrace(const char* format, ...); 87 ~AceAsyncScopedTrace(); 88 89 ACE_DISALLOW_COPY_AND_MOVE(AceAsyncScopedTrace); 90 91 private: 92 bool asyncTraceEnabled_ { false }; 93 std::string name_; 94 int32_t taskId_; 95 static std::atomic<std::int32_t> id_; 96 }; 97 98 } // namespace OHOS::Ace 99 100 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_ACE_TRACE_H 101