1 /* 2 * Copyright 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 <stdarg.h> 20 21 #include <cutils/trace.h> 22 #include <utils/Trace.h> 23 24 #define ATRACE_FORMAT(fmt, ...) \ 25 TraceUtils::TraceEnder traceEnder = \ 26 (CC_UNLIKELY(ATRACE_ENABLED()) && \ 27 (TraceUtils::atraceFormatBegin(fmt, ##__VA_ARGS__), true), \ 28 TraceUtils::TraceEnder()) 29 30 #define ATRACE_FORMAT_INSTANT(fmt, ...) \ 31 (CC_UNLIKELY(ATRACE_ENABLED()) && (TraceUtils::instantFormat(fmt, ##__VA_ARGS__), true)) 32 33 #define ALOGE_AND_TRACE(fmt, ...) \ 34 do { \ 35 ALOGE(fmt, ##__VA_ARGS__); \ 36 ATRACE_FORMAT_INSTANT(fmt, ##__VA_ARGS__); \ 37 } while (false) 38 39 namespace android { 40 41 class TraceUtils { 42 public: 43 class TraceEnder { 44 public: ~TraceEnder()45 ~TraceEnder() { ATRACE_END(); } 46 }; 47 atraceFormatBegin(const char * fmt,...)48 static void atraceFormatBegin(const char* fmt, ...) { 49 const int BUFFER_SIZE = 256; 50 va_list ap; 51 char buf[BUFFER_SIZE]; 52 53 va_start(ap, fmt); 54 vsnprintf(buf, BUFFER_SIZE, fmt, ap); 55 va_end(ap); 56 57 ATRACE_BEGIN(buf); 58 } 59 instantFormat(const char * fmt,...)60 static void instantFormat(const char* fmt, ...) { 61 const int BUFFER_SIZE = 256; 62 va_list ap; 63 char buf[BUFFER_SIZE]; 64 65 va_start(ap, fmt); 66 vsnprintf(buf, BUFFER_SIZE, fmt, ap); 67 va_end(ap); 68 69 ATRACE_INSTANT(buf); 70 } 71 }; 72 73 } // namespace android 74