1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <android-base/logging.h> 20 #include <string_view> 21 #include <utils/Trace.h> 22 23 // #define LOG_FUNC 24 #define TRACE_FUNC 25 26 #ifdef TRACE_FUNC 27 #define DEBUG_FUNC() constexpr static FullMethodName __kFullNameObj__ = \ 28 FullMethodName{ __PRETTY_FUNCTION__ }; \ 29 constexpr static const char *__kFullName__ = __kFullNameObj__.get(); \ 30 ATRACE_NAME(__kFullName__) 31 32 #define DEBUG_DISPLAY_FUNC(display) \ 33 constexpr static FullMethodName __kFullNameObj__{__PRETTY_FUNCTION__}; \ 34 constexpr static const char *__kFullName__ = __kFullNameObj__.get(); \ 35 if (CC_UNLIKELY(ATRACE_ENABLED())) { \ 36 ::android::String8 _traceName_; \ 37 _traceName_.appendFormat("%s(display=%" PRId64 ",..)", __kFullName__, display); \ 38 ATRACE_BEGIN(_traceName_.c_str()); \ 39 } \ 40 ScopedTraceEnder _traceEnder_ 41 #else 42 43 #ifdef LOG_FUNC 44 #define DEBUG_DISPLAY_FUNC(display) DebugFunction _dbgFnObj_(__func__, display) 45 #else 46 #define DEBUG_DISPLAY_FUNC(display) 47 #endif 48 49 #define DEBUG_FUNC() DEBUG_DISPLAY_FUNC(std::nullopt) 50 51 #endif 52 53 #define RET_IF_ERR(expr) \ 54 do { \ 55 auto err = (expr); \ 56 if (err) [[unlikely]] return err; \ 57 } while (0) 58 59 60 #define TO_BINDER_STATUS(x) x == 0 \ 61 ? ndk::ScopedAStatus::ok() \ 62 : ndk::ScopedAStatus::fromServiceSpecificError(x) 63 64 namespace aidl::android::hardware::graphics::composer3::impl { 65 66 class ScopedTraceEnder { 67 public: ~ScopedTraceEnder()68 ~ScopedTraceEnder() { ATRACE_END(); } 69 }; 70 71 class DebugFunction { 72 public: DebugFunction(const char * name,std::optional<int64_t> display)73 DebugFunction(const char *name, std::optional<int64_t> display) 74 : mName(name), mDisplay(display) { 75 if (mDisplay) { 76 LOG(INFO) << mName << "(display=" << *mDisplay << ",..) Enter"; 77 } else { 78 LOG(INFO) << mName << " Enter"; 79 } 80 } 81 ~DebugFunction()82 ~DebugFunction() { 83 if (mDisplay) { 84 LOG(INFO) << mName << "(display=" << *mDisplay << ",..) Exit"; 85 } else { 86 LOG(INFO) << mName << " Exit"; 87 } 88 } 89 90 private: 91 const char* mName; 92 std::optional<int64_t> mDisplay; 93 }; 94 95 class FullMethodName { 96 public: FullMethodName(const std::string_view prettyName)97 constexpr FullMethodName(const std::string_view prettyName) : mBuf() { 98 // remove every thing before 'impl::' 99 auto start = prettyName.find("impl::"); 100 if (start == prettyName.npos) { 101 start = 0; 102 } 103 // remove everything after '(' 104 auto end = prettyName.rfind('('); 105 if (end == prettyName.npos) { 106 end = prettyName.length(); 107 } 108 109 auto len = std::min(end - start, mBuf.size()); 110 // to a null-terminated string 111 // prettyName.copy(mBuf.data(), len, start) is available in c++20 112 for (int i = 0; i < len; ++i) { 113 mBuf[i] = prettyName[start + i]; 114 } 115 } 116 get()117 constexpr const char *get() const { 118 return mBuf.data(); 119 } 120 121 private: 122 std::array<char, 256> mBuf; 123 }; 124 125 } // namespace aidl::android::hardware::graphics::composer3::impl 126