1 /* 2 * Copyright (C) 2018 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 #ifndef IDMAP2_INCLUDE_IDMAP2_SYSTRACE_H_ 18 #define IDMAP2_INCLUDE_IDMAP2_SYSTRACE_H_ 19 20 #define ATRACE_TAG ATRACE_TAG_RRO 21 22 #include <sstream> 23 #include <vector> 24 25 #include "cutils/trace.h" 26 27 namespace android::idmap2::utils { 28 #ifdef __ANDROID__ 29 30 class ScopedTraceNoStart { 31 public: ~ScopedTraceNoStart()32 ~ScopedTraceNoStart() { 33 ATRACE_END(); 34 } 35 }; 36 37 class ScopedTraceMessageHelper { 38 public: ~ScopedTraceMessageHelper()39 ~ScopedTraceMessageHelper() { 40 ATRACE_BEGIN(buffer_.str().c_str()); 41 } 42 stream()43 std::ostream& stream() { 44 return buffer_; 45 } 46 47 private: 48 std::ostringstream buffer_; 49 }; 50 51 #define SYSTRACE \ 52 android::idmap2::utils::ScopedTraceNoStart _trace##__LINE__; \ 53 (ATRACE_ENABLED()) && android::idmap2::utils::ScopedTraceMessageHelper().stream() 54 55 #else 56 57 class DummyStream { 58 public: 59 std::ostream& stream() { 60 return buffer_; 61 } 62 63 private: 64 std::ostringstream buffer_; 65 }; 66 67 #define SYSTRACE android::idmap2::utils::DummyStream().stream() 68 69 #endif 70 } // namespace android::idmap2::utils 71 72 template <typename T> 73 std::ostream& operator<<(std::ostream& stream, const std::vector<T>& vector) { 74 bool first = true; 75 stream << "["; 76 for (const auto& item : vector) { 77 if (!first) { 78 stream << ", "; 79 } 80 stream << item; 81 first = false; 82 } 83 stream << "]"; 84 return stream; 85 } 86 87 #endif // IDMAP2_INCLUDE_IDMAP2_SYSTRACE_H_ 88