1/*
2 * Copyright (c) 2022 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#ifndef OBJECT_STORE_LOGGER_H
17#define OBJECT_STORE_LOGGER_H
18#include <memory>
19#ifdef HILOG_ENABLE
20#include "hilog/log.h"
21namespace OHOS::ObjectStore {
22static inline OHOS::HiviewDFX::HiLogLabel LogLabel()
23{
24    return { LOG_CORE, 0xD001652, "ObjectStore-x" };
25}
26
27#define LOG_DEBUG(fmt, ...)                                                                 \
28    do {                                                                                    \
29        auto lable = LogLabel();                                                            \
30        if (HiLogIsLoggable(lable.domain, lable.tag, LogLevel::LOG_DEBUG)) {                \
31            ((void)HILOG_IMPL(lable.type, LogLevel::LOG_DEBUG, lable.domain, lable.tag,     \
32                "%{public}d: %{public}s " fmt " ", __LINE__, __FUNCTION__, ##__VA_ARGS__)); \
33        }                                                                                   \
34    } while (0)
35
36#define LOG_INFO(fmt, ...)                                                                  \
37    do {                                                                                    \
38        auto lable = LogLabel();                                                            \
39        if (HiLogIsLoggable(lable.domain, lable.tag, LogLevel::LOG_INFO)) {                 \
40            ((void)HILOG_IMPL(lable.type, LogLevel::LOG_INFO, lable.domain, lable.tag,      \
41                "%{public}d: %{public}s " fmt " ", __LINE__, __FUNCTION__, ##__VA_ARGS__)); \
42        }                                                                                   \
43    } while (0)
44
45#define LOG_WARN(fmt, ...)                                                                  \
46    do {                                                                                    \
47        auto lable = LogLabel();                                                            \
48        if (HiLogIsLoggable(lable.domain, lable.tag, LogLevel::LOG_WARN)) {                 \
49            ((void)HILOG_IMPL(lable.type, LogLevel::LOG_WARN, lable.domain, lable.tag,      \
50                "%{public}d: %{public}s " fmt " ", __LINE__, __FUNCTION__, ##__VA_ARGS__)); \
51        }                                                                                   \
52    } while (0)
53
54#define LOG_ERROR(fmt, ...)                                                                 \
55    do {                                                                                    \
56        auto lable = LogLabel();                                                            \
57        if (HiLogIsLoggable(lable.domain, lable.tag, LogLevel::LOG_ERROR)) {                \
58            ((void)HILOG_IMPL(lable.type, LogLevel::LOG_ERROR, lable.domain, lable.tag,     \
59                "%{public}d: %{public}s " fmt " ", __LINE__, __FUNCTION__, ##__VA_ARGS__)); \
60        }                                                                                   \
61    } while (0)
62
63#define LOG_FATAL(fmt, ...)                                                                 \
64    do {                                                                                    \
65        auto lable = LogLabel();                                                            \
66        if (HiLogIsLoggable(lable.domain, lable.tag, LogLevel::LOG_FATAL)) {                \
67            ((void)HILOG_IMPL(lable.type, LogLevel::LOG_FATAL, lable.domain, lable.tag,     \
68                "%{public}d: %{public}s " fmt " ", __LINE__, __FUNCTION__, ##__VA_ARGS__)); \
69        }                                                                                   \
70    } while (0)
71
72} // namespace OHOS::ObjectStore
73#else
74#include <stdio.h>
75#include <stdlib.h>
76
77#define LOG_DEBUG(fmt, ...) \
78    printf("[D][ObjectStore]%s:%d %s: " fmt "\n", __FILE_NAME__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
79#define LOG_ERROR(fmt, ...) \
80    printf("[E][ObjectStore]%s:%d %s: " fmt "\n", __FILE_NAME__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
81#define LOG_INFO(fmt, ...) \
82    printf("[I][ObjectStore]%s:%d %s: " fmt "\n", __FILE_NAME__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
83#define LOG_WARN(fmt, ...) \
84    printf("[W][ObjectStore]%s:%d %s: " fmt "\n", __FILE_NAME__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
85#endif // #ifdef HILOG_ENABLE
86#endif // OBJECT_STORE_LOGGER_H
87