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"
21 namespace OHOS::ObjectStore {
LogLabel()22 static inline OHOS::HiviewDFX::HiLogLabel LogLabel()
23 {
24 return { LOG_CORE, 0xD001652, "ObjectStore-x" };
25 }
26
27 #define LOG_DEBUG(fmt, ...) \
28 do { \
29 using HiLog = OHOS::HiviewDFX::HiLog; \
30 auto lable = LogLabel(); \
31 if (HiLogIsLoggable(lable.domain, lable.tag, LogLevel::LOG_DEBUG)) { \
32 HiLog::Debug(lable, "%{public}d: %{public}s " fmt " ", __LINE__, __FUNCTION__, ##__VA_ARGS__); \
33 } \
34 } while (0)
35
36 #define LOG_INFO(fmt, ...) \
37 do { \
38 using HiLog = OHOS::HiviewDFX::HiLog; \
39 auto lable = LogLabel(); \
40 if (HiLogIsLoggable(lable.domain, lable.tag, LogLevel::LOG_INFO)) { \
41 HiLog::Info(lable, "%{public}d: %{public}s " fmt " ", __LINE__, __FUNCTION__, ##__VA_ARGS__); \
42 } \
43 } while (0)
44
45 #define LOG_WARN(fmt, ...) \
46 do { \
47 using HiLog = OHOS::HiviewDFX::HiLog; \
48 auto lable = LogLabel(); \
49 if (HiLogIsLoggable(lable.domain, lable.tag, LogLevel::LOG_WARN)) { \
50 HiLog::Warn(lable, "%{public}d: %{public}s " fmt " ", __LINE__, __FUNCTION__, ##__VA_ARGS__); \
51 } \
52 } while (0)
53
54 #define LOG_ERROR(fmt, ...) \
55 do { \
56 using HiLog = OHOS::HiviewDFX::HiLog; \
57 auto lable = LogLabel(); \
58 if (HiLogIsLoggable(lable.domain, lable.tag, LogLevel::LOG_ERROR)) { \
59 HiLog::Error(lable, "%{public}d: %{public}s " fmt " ", __LINE__, __FUNCTION__, ##__VA_ARGS__); \
60 } \
61 } while (0)
62
63 #define LOG_FATAL(fmt, ...) \
64 do { \
65 using HiLog = OHOS::HiviewDFX::HiLog; \
66 auto lable = LogLabel(); \
67 if (HiLogIsLoggable(lable.domain, lable.tag, LogLevel::LOG_FATAL)) { \
68 HiLog::Fatal(lable, "%{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__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
79 #define LOG_ERROR(fmt, ...) \
80 printf("[E][ObjectStore]%s:%d %s: " fmt "\n", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
81 #define LOG_INFO(fmt, ...) \
82 printf("[I][ObjectStore]%s:%d %s: " fmt "\n", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
83 #define LOG_WARN(fmt, ...) \
84 printf("[W][ObjectStore]%s:%d %s: " fmt "\n", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
85 #endif // #ifdef HILOG_ENABLE
86 #endif // OBJECT_STORE_LOGGER_H
87