1 /*
2  * Copyright (c) 2024 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 #ifdef DFX_ENABLE_TRACE
16 #include "dfx_trace.h"
17 #include <cstdio>
18 #include <cstdarg>
19 #include <securec.h>
DfxStartTrace(const char * fmt,...)20 void DfxStartTrace(const char *fmt, ...)
21 {
22     va_list args;
23     va_start(args, fmt);
24     char traceName[TRACE_BUF_LEN] = {0};
25     int ret = vsnprintf_s(traceName, sizeof(traceName), sizeof(traceName) - 1, fmt, args);
26     va_end(args);
27     if (ret == -1) {
28         strcpy_s(traceName, TRACE_BUF_LEN, "DefaultTraceName");
29     }
30     StartTrace(HITRACE_TAG_APP, traceName);
31 }
32 
FormatTraceName(char * name,size_t size,const char * fmt,...)33 void FormatTraceName(char *name, size_t size, const char *fmt, ...)
34 {
35     if (size < 1 || name == nullptr) {
36         return;
37     }
38     va_list args;
39     va_start(args, fmt);
40     int ret = vsnprintf_s(name, size, size - 1, fmt, args);
41     va_end(args);
42     std::string traceName = "DefaultTraceName";
43     if (ret == -1 && size > traceName.length()) {
44         ret = strcpy_s(name, size, traceName.c_str());
45         if (ret != 0) {
46             return;
47         }
48     }
49 }
50 #endif