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 (TraceUtils::atraceFormatBegin(fmt, ##__VA_ARGS__), TraceUtils::TraceEnder()) 27 28 #define ATRACE_FORMAT_BEGIN(fmt, ...) TraceUtils::atraceFormatBegin(fmt, ##__VA_ARGS__) 29 30 #define ATRACE_FORMAT_INSTANT(fmt, ...) TraceUtils::intantFormat(fmt, ##__VA_ARGS__) 31 32 namespace android { 33 34 class TraceUtils { 35 public: 36 class TraceEnder { 37 public: ~TraceEnder()38 ~TraceEnder() { ATRACE_END(); } 39 }; 40 atraceFormatBegin(const char * fmt,...)41 static void atraceFormatBegin(const char* fmt, ...) { 42 if (CC_LIKELY(!ATRACE_ENABLED())) return; 43 44 const int BUFFER_SIZE = 256; 45 va_list ap; 46 char buf[BUFFER_SIZE]; 47 48 va_start(ap, fmt); 49 vsnprintf(buf, BUFFER_SIZE, fmt, ap); 50 va_end(ap); 51 52 ATRACE_BEGIN(buf); 53 } 54 intantFormat(const char * fmt,...)55 static void intantFormat(const char* fmt, ...) { 56 if (CC_LIKELY(!ATRACE_ENABLED())) return; 57 58 const int BUFFER_SIZE = 256; 59 va_list ap; 60 char buf[BUFFER_SIZE]; 61 62 va_start(ap, fmt); 63 vsnprintf(buf, BUFFER_SIZE, fmt, ap); 64 va_end(ap); 65 66 ATRACE_INSTANT(buf); 67 } 68 69 }; // class TraceUtils 70 71 } /* namespace android */ 72