1 /*
2 * Copyright (c) 2023 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 #ifndef PRE_LOG_PRINTF_H
16 #define PRE_LOG_PRINTF_H
17
18 #include <string>
19 #include <vector>
20
21 #include "hilog/log.h"
22
23 namespace OHOS {
24 namespace PreferencesJsKit {
LogLabel()25 static inline OHOS::HiviewDFX::HiLogLabel LogLabel()
26 {
27 return { LOG_CORE, 0xD001653, "PreferencesJsKit" };
28 }
29 }
30
31 namespace NativePreferences {
LogLabel()32 static inline OHOS::HiviewDFX::HiLogLabel LogLabel()
33 {
34 return { LOG_CORE, 0xD001653, "NativePreferences" };
35 }
36 }
37 namespace StorageJsKit {
LogLabel()38 static inline OHOS::HiviewDFX::HiLogLabel LogLabel()
39 {
40 return { LOG_CORE, 0xD001653, "StorageJsKit" };
41 }
42 }
43 } // namespace OHOS
44 #define LOG_DEBUG(fmt, ...) \
45 do { \
46 using HiLog = OHOS::HiviewDFX::HiLog; \
47 auto lable = LogLabel(); \
48 if (HiLogIsLoggable(lable.domain, lable.tag, LogLevel::LOG_DEBUG)) { \
49 HiLog::Debug(lable, "%{public}d: %{public}s " fmt " ", __LINE__, __FUNCTION__, ##__VA_ARGS__); \
50 } \
51 } while (0)
52
53 #define LOG_RECORD_FILE_NAME(message) \
54 do { \
55 const char *name = fileName.data(); \
56 auto pos = fileName.rfind('/'); \
57 pos = (pos != std::string::npos) ? pos + 1 : 0; \
58 LOG_DEBUG(message " fileName is %{private}s.", name + pos); \
59 } while (0)
60
61 #define LOG_INFO(fmt, ...) \
62 do { \
63 using HiLog = OHOS::HiviewDFX::HiLog; \
64 auto lable = LogLabel(); \
65 if (HiLogIsLoggable(lable.domain, lable.tag, LogLevel::LOG_INFO)) { \
66 HiLog::Info(lable, "%{public}d: %{public}s " fmt " ", __LINE__, __FUNCTION__, ##__VA_ARGS__); \
67 } \
68 } while (0)
69
70 #define LOG_WARN(fmt, ...) \
71 do { \
72 using HiLog = OHOS::HiviewDFX::HiLog; \
73 auto lable = LogLabel(); \
74 if (HiLogIsLoggable(lable.domain, lable.tag, LogLevel::LOG_WARN)) { \
75 HiLog::Warn(lable, "%{public}d: %{public}s " fmt " ", __LINE__, __FUNCTION__, ##__VA_ARGS__); \
76 } \
77 } while (0)
78
79 #define LOG_ERROR(fmt, ...) \
80 do { \
81 using HiLog = OHOS::HiviewDFX::HiLog; \
82 auto lable = LogLabel(); \
83 if (HiLogIsLoggable(lable.domain, lable.tag, LogLevel::LOG_ERROR)) { \
84 HiLog::Error(lable, "%{public}d: %{public}s " fmt " ", __LINE__, __FUNCTION__, ##__VA_ARGS__); \
85 } \
86 } while (0)
87
88 #define LOG_FATAL(fmt, ...) \
89 do { \
90 using HiLog = OHOS::HiviewDFX::HiLog; \
91 auto lable = LogLabel(); \
92 if (HiLogIsLoggable(lable.domain, lable.tag, LogLevel::LOG_FATAL)) { \
93 HiLog::Fatal(lable, "%{public}d: %{public}s " fmt " ", __LINE__, __FUNCTION__, ##__VA_ARGS__); \
94 } \
95 } while (0)
96
97 #endif
98