1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
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
16 #include "perf_trace.h"
17 #include <cstdio>
18 #include <cstdarg>
19 #include <securec.h>
20
21 namespace OHOS {
22 namespace HiviewDFX {
XperfStartTrace(const char * fmt,...)23 void XperfStartTrace(const char *fmt, ...)
24 {
25 va_list args;
26 va_start(args, fmt);
27 char traceName[TRACE_BUF_LEN] = {0};
28 int ret = vsnprintf_s(traceName, sizeof(traceName), sizeof(traceName) - 1, fmt, args);
29 va_end(args);
30 if (ret == -1) {
31 strcpy_s(traceName, sizeof(traceName), "DefaultTraceName");
32 }
33 StartTrace(HITRACE_TAG_APP, std::string(traceName));
34 }
35
FormatTraceName(char * name,size_t size,const char * fmt,...)36 void FormatTraceName(char *name, size_t size, const char *fmt, ...)
37 {
38 if (size < 1 || name == nullptr) {
39 return;
40 }
41 va_list args;
42 va_start(args, fmt);
43 int ret = vsnprintf_s(name, size, size - 1, fmt, args);
44 va_end(args);
45 std::string traceName = "DefaultTraceName";
46 if (ret == -1 && size > traceName.length()) {
47 ret = strcpy_s(name, size, traceName.c_str());
48 if (ret != 0) {
49 return;
50 }
51 }
52 }
53
XperfAsyncTraceBegin(int32_t taskId,const char * name,bool isAnimationTrace)54 void XperfAsyncTraceBegin(int32_t taskId, const char* name, bool isAnimationTrace)
55 {
56 if (name == nullptr) {
57 return;
58 }
59 std::string nameStr(name);
60 if (isAnimationTrace) {
61 StartAsyncTrace(HITRACE_TAG_ANIMATION, nameStr, taskId);
62 } else {
63 StartAsyncTrace(HITRACE_TAG_ACE, nameStr, taskId);
64 }
65 }
66
XperfAsyncTraceEnd(int32_t taskId,const char * name,bool isAnimationTrace)67 void XperfAsyncTraceEnd(int32_t taskId, const char* name, bool isAnimationTrace)
68 {
69 if (name == nullptr) {
70 return;
71 }
72 std::string nameStr(name);
73 if (isAnimationTrace) {
74 FinishAsyncTrace(HITRACE_TAG_ANIMATION, nameStr, taskId);
75 } else {
76 FinishAsyncTrace(HITRACE_TAG_ACE, nameStr, taskId);
77 }
78 }
79 }
80 }
81