1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci#ifdef DFX_ENABLE_TRACE
16800b99b8Sopenharmony_ci#include "dfx_trace.h"
17800b99b8Sopenharmony_ci#include <cstdio>
18800b99b8Sopenharmony_ci#include <cstdarg>
19800b99b8Sopenharmony_ci#include <securec.h>
20800b99b8Sopenharmony_civoid DfxStartTrace(const char *fmt, ...)
21800b99b8Sopenharmony_ci{
22800b99b8Sopenharmony_ci    va_list args;
23800b99b8Sopenharmony_ci    va_start(args, fmt);
24800b99b8Sopenharmony_ci    char traceName[TRACE_BUF_LEN] = {0};
25800b99b8Sopenharmony_ci    int ret = vsnprintf_s(traceName, sizeof(traceName), sizeof(traceName) - 1, fmt, args);
26800b99b8Sopenharmony_ci    va_end(args);
27800b99b8Sopenharmony_ci    if (ret == -1) {
28800b99b8Sopenharmony_ci        strcpy_s(traceName, TRACE_BUF_LEN, "DefaultTraceName");
29800b99b8Sopenharmony_ci    }
30800b99b8Sopenharmony_ci    StartTrace(HITRACE_TAG_APP, traceName);
31800b99b8Sopenharmony_ci}
32800b99b8Sopenharmony_ci
33800b99b8Sopenharmony_civoid FormatTraceName(char *name, size_t size, const char *fmt, ...)
34800b99b8Sopenharmony_ci{
35800b99b8Sopenharmony_ci    if (size < 1 || name == nullptr) {
36800b99b8Sopenharmony_ci        return;
37800b99b8Sopenharmony_ci    }
38800b99b8Sopenharmony_ci    va_list args;
39800b99b8Sopenharmony_ci    va_start(args, fmt);
40800b99b8Sopenharmony_ci    int ret = vsnprintf_s(name, size, size - 1, fmt, args);
41800b99b8Sopenharmony_ci    va_end(args);
42800b99b8Sopenharmony_ci    std::string traceName = "DefaultTraceName";
43800b99b8Sopenharmony_ci    if (ret == -1 && size > traceName.length()) {
44800b99b8Sopenharmony_ci        ret = strcpy_s(name, size, traceName.c_str());
45800b99b8Sopenharmony_ci        if (ret != 0) {
46800b99b8Sopenharmony_ci            return;
47800b99b8Sopenharmony_ci        }
48800b99b8Sopenharmony_ci    }
49800b99b8Sopenharmony_ci}
50800b99b8Sopenharmony_ci#endif