1cc290419Sopenharmony_ci/*
2cc290419Sopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd.
3cc290419Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4cc290419Sopenharmony_ci * you may not use this file except in compliance with the License.
5cc290419Sopenharmony_ci * You may obtain a copy of the License at
6cc290419Sopenharmony_ci *
7cc290419Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8cc290419Sopenharmony_ci *
9cc290419Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10cc290419Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11cc290419Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cc290419Sopenharmony_ci * See the License for the specific language governing permissions and
13cc290419Sopenharmony_ci * limitations under the License.
14cc290419Sopenharmony_ci */
15cc290419Sopenharmony_ci#include "log.h"
16cc290419Sopenharmony_ci#ifdef HDC_HILOG
17cc290419Sopenharmony_ci#include "hilog/log.h"
18cc290419Sopenharmony_ci#endif
19cc290419Sopenharmony_ci
20cc290419Sopenharmony_cinamespace Hdc {
21cc290419Sopenharmony_civoid PrintLogEx(const char *functionName, int line, uint8_t logLevel, const char *msg, ...)
22cc290419Sopenharmony_ci{
23cc290419Sopenharmony_ci    char buf[BUF_SIZE_DEFAULT4] = { 0 }; // only 4k to avoid stack overflow in 32bit or L0
24cc290419Sopenharmony_ci    va_list vaArgs;
25cc290419Sopenharmony_ci    va_start(vaArgs, msg);
26cc290419Sopenharmony_ci    const int retSize = vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, msg, vaArgs);
27cc290419Sopenharmony_ci    va_end(vaArgs);
28cc290419Sopenharmony_ci    if (retSize < 0) {
29cc290419Sopenharmony_ci        return;
30cc290419Sopenharmony_ci    }
31cc290419Sopenharmony_ci
32cc290419Sopenharmony_ci#ifdef  HDC_HILOG
33cc290419Sopenharmony_ci    string tmpPath = functionName;
34cc290419Sopenharmony_ci    string filePath = GetFileNameAny(tmpPath);
35cc290419Sopenharmony_ci    static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = {LOG_CORE, 0xD002D13, "HDC_LOG"};
36cc290419Sopenharmony_ci    switch (static_cast<int>(logLevel)) {
37cc290419Sopenharmony_ci        case static_cast<int>(LOG_DEBUG):
38cc290419Sopenharmony_ci            // Info level log can be printed default in hilog, debug can't
39cc290419Sopenharmony_ci            OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "[%{public}s:%{public}d] %{public}s",
40cc290419Sopenharmony_ci                                         filePath.c_str(), line, buf);
41cc290419Sopenharmony_ci            break;
42cc290419Sopenharmony_ci        case static_cast<int>(LOG_INFO):
43cc290419Sopenharmony_ci            OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "[%{public}s:%{public}d] %{public}s",
44cc290419Sopenharmony_ci                                         filePath.c_str(), line, buf);
45cc290419Sopenharmony_ci            break;
46cc290419Sopenharmony_ci        case static_cast<int>(LOG_WARN):
47cc290419Sopenharmony_ci            OHOS::HiviewDFX::HiLog::Warn(LOG_LABEL, "[%{public}s:%{public}d] %{public}s",
48cc290419Sopenharmony_ci                                         filePath.c_str(), line, buf);
49cc290419Sopenharmony_ci            break;
50cc290419Sopenharmony_ci        case static_cast<int>(LOG_FATAL):
51cc290419Sopenharmony_ci            OHOS::HiviewDFX::HiLog::Fatal(LOG_LABEL, "[%{public}s:%{public}d] %{public}s",
52cc290419Sopenharmony_ci                                          filePath.c_str(), line, buf);
53cc290419Sopenharmony_ci            break;
54cc290419Sopenharmony_ci        default:
55cc290419Sopenharmony_ci            break;
56cc290419Sopenharmony_ci    }
57cc290419Sopenharmony_ci#endif
58cc290419Sopenharmony_ci}
59cc290419Sopenharmony_ci}