1 // Copyright (C) 2020 The Android Open Source Project 2 // 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 #ifndef IORAP_COMMON_TRACE_H_ 16 #define IORAP_COMMON_TRACE_H_ 17 18 #include <cutils/trace.h> 19 20 #include <cstdio> 21 #include <sstream> 22 23 namespace iorap { 24 25 // TODO: refactor into utils/Trace.h 26 27 class ScopedFormatTrace { 28 public: 29 template <typename ... Args> ScopedFormatTrace(uint64_t tag,const char * fmt,Args &&...args)30 ScopedFormatTrace(uint64_t tag, const char* fmt, Args&&... args) : tag_{tag} { 31 char buffer[1024]; 32 #pragma GCC diagnostic push 33 #pragma GCC diagnostic ignored "-Wformat-security" 34 snprintf(buffer, sizeof(buffer), fmt, args...); 35 #pragma GCC diagnostic pop 36 atrace_begin(tag, buffer); 37 } 38 ~ScopedFormatTrace()39 ~ScopedFormatTrace() { 40 atrace_end(tag_); 41 } 42 private: 43 uint64_t tag_; 44 }; 45 46 } // namespace iorap 47 48 #endif // IORAP_COMMON_TRACE_H_ 49