133eb0b6dSopenharmony_ci/*
233eb0b6dSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
333eb0b6dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
433eb0b6dSopenharmony_ci * you may not use this file except in compliance with the License.
533eb0b6dSopenharmony_ci * You may obtain a copy of the License at
633eb0b6dSopenharmony_ci *
733eb0b6dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
833eb0b6dSopenharmony_ci *
933eb0b6dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1033eb0b6dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1133eb0b6dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1233eb0b6dSopenharmony_ci * See the License for the specific language governing permissions and
1333eb0b6dSopenharmony_ci * limitations under the License.
1433eb0b6dSopenharmony_ci */
1533eb0b6dSopenharmony_ci
1633eb0b6dSopenharmony_ci#ifndef FOUNDATION_ACE_NAPI_UTILS_LOG_H
1733eb0b6dSopenharmony_ci#define FOUNDATION_ACE_NAPI_UTILS_LOG_H
1833eb0b6dSopenharmony_ci
1933eb0b6dSopenharmony_ci#ifdef LINUX_PLATFORM
2033eb0b6dSopenharmony_ci#include <cstdint>
2133eb0b6dSopenharmony_ci#endif /* LINUX_PLATFORM */
2233eb0b6dSopenharmony_ci#include <cstring>
2333eb0b6dSopenharmony_ci#include <string>
2433eb0b6dSopenharmony_ci
2533eb0b6dSopenharmony_ci#include "utils/macros.h"
2633eb0b6dSopenharmony_ci
2733eb0b6dSopenharmony_ci#define __FILENAME__ strrchr(__FILE__, '/') + 1
2833eb0b6dSopenharmony_ci
2933eb0b6dSopenharmony_ci#if defined(MAC_PLATFORM) || defined(WINDOWS_PLATFORM) || defined(ANDROID_PLATFORM) || defined(IOS_PLATFORM) || \
3033eb0b6dSopenharmony_ci    defined(LINUX_PLATFORM)
3133eb0b6dSopenharmony_cienum class LogLevel : uint32_t {
3233eb0b6dSopenharmony_ci    Debug = 0,
3333eb0b6dSopenharmony_ci    Info,
3433eb0b6dSopenharmony_ci    Warn,
3533eb0b6dSopenharmony_ci    Error,
3633eb0b6dSopenharmony_ci    Fatal,
3733eb0b6dSopenharmony_ci};
3833eb0b6dSopenharmony_ci
3933eb0b6dSopenharmony_ciNAPI_EXPORT void PrintLog(LogLevel level, const char* fmt, ...);
4033eb0b6dSopenharmony_ci
4133eb0b6dSopenharmony_ci#define HILOG_PRINT(Level, fmt, ...) \
4233eb0b6dSopenharmony_ci    PrintLog(LogLevel::Level, "[%-20s(%s)] " fmt, __FILENAME__, __FUNCTION__, ##__VA_ARGS__)
4333eb0b6dSopenharmony_ci
4433eb0b6dSopenharmony_ci#define HILOG_FATAL(fmt, ...)                               \
4533eb0b6dSopenharmony_ci    do {                                                    \
4633eb0b6dSopenharmony_ci        HILOG_PRINT(Error, fmt, ##__VA_ARGS__);             \
4733eb0b6dSopenharmony_ci        abort();                                            \
4833eb0b6dSopenharmony_ci    } while (0)
4933eb0b6dSopenharmony_ci#define HILOG_ERROR(fmt, ...) HILOG_PRINT(Error, fmt, ##__VA_ARGS__)
5033eb0b6dSopenharmony_ci#define HILOG_WARN(fmt, ...) HILOG_PRINT(Warn, fmt, ##__VA_ARGS__)
5133eb0b6dSopenharmony_ci#define HILOG_INFO(fmt, ...) HILOG_PRINT(Info, fmt, ##__VA_ARGS__)
5233eb0b6dSopenharmony_ci#define HILOG_DEBUG(fmt, ...) HILOG_PRINT(Debug, fmt, ##__VA_ARGS__)
5333eb0b6dSopenharmony_ci
5433eb0b6dSopenharmony_ci#else
5533eb0b6dSopenharmony_ci
5633eb0b6dSopenharmony_ci#include "hilog/log.h"
5733eb0b6dSopenharmony_ci
5833eb0b6dSopenharmony_ci#undef LOG_DOMAIN
5933eb0b6dSopenharmony_ci#undef LOG_TAG
6033eb0b6dSopenharmony_ci#undef HILOG_FATAL
6133eb0b6dSopenharmony_ci#undef HILOG_ERROR
6233eb0b6dSopenharmony_ci#undef HILOG_WARN
6333eb0b6dSopenharmony_ci#undef HILOG_INFO
6433eb0b6dSopenharmony_ci#undef HILOG_DEBUG
6533eb0b6dSopenharmony_ci
6633eb0b6dSopenharmony_ci#define LOG_DOMAIN 0xD003F01
6733eb0b6dSopenharmony_ci#define LOG_TAG "NAPI"
6833eb0b6dSopenharmony_ci
6933eb0b6dSopenharmony_cistatic constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_DOMAIN, LOG_TAG };
7033eb0b6dSopenharmony_ci
7133eb0b6dSopenharmony_ci#define HILOG_PRINT(Level, fmt, ...)     \
7233eb0b6dSopenharmony_ci    (void)OHOS::HiviewDFX::HiLog::Level( \
7333eb0b6dSopenharmony_ci        LOG_LABEL, "[(%{public}s:%{public}d)(%{public}s)] " fmt, __FILENAME__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
7433eb0b6dSopenharmony_ci
7533eb0b6dSopenharmony_ci#define HILOG_FATAL(fmt, ...)                                       \
7633eb0b6dSopenharmony_ci    do {                                                            \
7733eb0b6dSopenharmony_ci        ((void)HILOG_IMPL(LOG_CORE, LOG_FATAL, LOG_DOMAIN, LOG_TAG, \
7833eb0b6dSopenharmony_ci            "[(%{public}s:%{public}d)(%{public}s)] " fmt,           \
7933eb0b6dSopenharmony_ci            __FILENAME__, __LINE__, __FUNCTION__, ##__VA_ARGS__));  \
8033eb0b6dSopenharmony_ci        abort();                                                    \
8133eb0b6dSopenharmony_ci    } while (0)
8233eb0b6dSopenharmony_ci#define HILOG_ERROR(fmt, ...) \
8333eb0b6dSopenharmony_ci    ((void)HILOG_IMPL(LOG_CORE, LOG_ERROR, LOG_DOMAIN, LOG_TAG, \
8433eb0b6dSopenharmony_ci    "[(%{public}s:%{public}d)(%{public}s)] " fmt, __FILENAME__, __LINE__, __FUNCTION__, ##__VA_ARGS__))
8533eb0b6dSopenharmony_ci#define HILOG_WARN(fmt, ...) \
8633eb0b6dSopenharmony_ci    ((void)HILOG_IMPL(LOG_CORE, LOG_WARN, LOG_DOMAIN, LOG_TAG, \
8733eb0b6dSopenharmony_ci    "[(%{public}s:%{public}d)(%{public}s)] " fmt, __FILENAME__, __LINE__, __FUNCTION__, ##__VA_ARGS__))
8833eb0b6dSopenharmony_ci#define HILOG_INFO(fmt, ...) \
8933eb0b6dSopenharmony_ci    ((void)HILOG_IMPL(LOG_CORE, LOG_INFO, LOG_DOMAIN, LOG_TAG, \
9033eb0b6dSopenharmony_ci    "[(%{public}s:%{public}d)(%{public}s)] " fmt, __FILENAME__, __LINE__, __FUNCTION__, ##__VA_ARGS__))
9133eb0b6dSopenharmony_ci#define HILOG_DEBUG(fmt, ...) \
9233eb0b6dSopenharmony_ci    ((void)HILOG_IMPL(LOG_CORE, LOG_DEBUG, LOG_DOMAIN, LOG_TAG, \
9333eb0b6dSopenharmony_ci    "[(%{public}s:%{public}d)(%{public}s)] " fmt, __FILENAME__, __LINE__, __FUNCTION__, ##__VA_ARGS__))
9433eb0b6dSopenharmony_ci
9533eb0b6dSopenharmony_ci#endif /* MAC_PLATFORM */
9633eb0b6dSopenharmony_ci
9733eb0b6dSopenharmony_ci#define LOG_IF_SPECIAL(condition, fmt, ...) \
9833eb0b6dSopenharmony_ci    if ((condition)) {                      \
9933eb0b6dSopenharmony_ci        HILOG_FATAL(fmt, ##__VA_ARGS__);    \
10033eb0b6dSopenharmony_ci    } else {                                \
10133eb0b6dSopenharmony_ci        HILOG_ERROR(fmt, ##__VA_ARGS__);    \
10233eb0b6dSopenharmony_ci    }
10333eb0b6dSopenharmony_ci
10433eb0b6dSopenharmony_ci#endif /* FOUNDATION_ACE_NAPI_UTILS_LOG_H */
105