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 namespace OHOS::Ace { 41 42 bool ACE_EXPORT AceTraceEnabled(); 43 bool ACE_EXPORT AceAsyncTraceEnable(); 44 void ACE_EXPORT AceTraceBegin(const char* name); 45 void ACE_EXPORT AceAsyncTraceBegin(int32_t taskId, const char* name); 46 bool ACE_EXPORT AceTraceBeginWithArgs(const char* format, ...) __attribute__((__format__(printf, 1, 2))); 47 std::string ACE_EXPORT AceAsyncTraceBeginWithArgs(int32_t taskId, char* format, ...); 48 bool ACE_EXPORT AceTraceBeginWithArgv(const char* format, va_list args); 49 std::string ACE_EXPORT AceAsyncTraceBeginWithArgv(int32_t taskId, const char* format, va_list args); 50 void ACE_EXPORT AceTraceEnd(); 51 void ACE_EXPORT AceAsyncTraceEnd(int32_t taskId, const char* name); 52 53 class ACE_FORCE_EXPORT AceScopedTrace final { 54 public: 55 explicit AceScopedTrace(const char* format, ...) __attribute__((__format__(printf, 2, 3))); 56 ~AceScopedTrace(); 57 58 ACE_DISALLOW_COPY_AND_MOVE(AceScopedTrace); 59 60 private: 61 bool traceEnabled_ { false }; 62 }; 63 64 class ACE_EXPORT AceScopedTraceFlag final { 65 public: 66 explicit AceScopedTraceFlag(bool flag, const char* format, ...) __attribute__((__format__(printf, 3, 4))); 67 ~AceScopedTraceFlag(); 68 69 ACE_DISALLOW_COPY_AND_MOVE(AceScopedTraceFlag); 70 71 private: 72 bool flagTraceEnabled_ { false }; 73 }; 74 75 class ACE_EXPORT AceAsyncScopedTrace final { 76 public: 77 AceAsyncScopedTrace(const char* format, ...); 78 ~AceAsyncScopedTrace(); 79 80 ACE_DISALLOW_COPY_AND_MOVE(AceAsyncScopedTrace); 81 82 private: 83 bool asyncTraceEnabled_ { false }; 84 std::string name_; 85 int32_t taskId_; 86 static std::atomic<std::int32_t> id_; 87 }; 88 89 } // namespace OHOS::Ace 90 91 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_ACE_TRACE_H 92