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 
38 namespace PreferencesNdk {
LogLabel()39 static inline OHOS::HiviewDFX::HiLogLabel LogLabel()
40 {
41     return { LOG_CORE, 0xD001653, "PreferencesNdk" };
42 }
43 }
44 
45 namespace StorageJsKit {
LogLabel()46 static inline OHOS::HiviewDFX::HiLogLabel LogLabel()
47 {
48     return { LOG_CORE, 0xD001653, "StorageJsKit" };
49 }
50 }
51 namespace Sendable::JSPreferences {
LogLabel()52 static inline OHOS::HiviewDFX::HiLogLabel LogLabel()
53 {
54     return { LOG_CORE, 0xD001653, "JSPreferences" };
55 }
56 }
57 
58 } // namespace OHOS
59 #define LOG_DEBUG(fmt, ...)                                                             \
60     do {                                                                                \
61         auto lable = LogLabel();                                                        \
62         if (HiLogIsLoggable(lable.domain, lable.tag, LogLevel::LOG_DEBUG)) {            \
63             ((void)HILOG_IMPL(lable.type, LogLevel::LOG_DEBUG, lable.domain, lable.tag, \
64                               "%{public}s: " fmt, __FUNCTION__, ##__VA_ARGS__));        \
65         }                                                                               \
66     } while (0)
67 
68 #define LOG_RECORD_FILE_NAME(message)                                                   \
69     do {                                                                                \
70         const char *name = fileName.data();                                             \
71         auto pos = fileName.rfind('/');                                                 \
72         pos = (pos != std::string::npos) ? pos + 1 : 0;                                 \
73         LOG_DEBUG(message " fileName is %{private}s.", name + pos);                     \
74     } while (0)
75 
76 #define LOG_INFO(fmt, ...)                                                              \
77     do {                                                                                \
78         auto lable = LogLabel();                                                        \
79         if (HiLogIsLoggable(lable.domain, lable.tag, LogLevel::LOG_INFO)) {             \
80             ((void)HILOG_IMPL(lable.type, LogLevel::LOG_INFO, lable.domain, lable.tag,  \
81                               "%{public}s: " fmt, __FUNCTION__, ##__VA_ARGS__));        \
82         }                                                                               \
83     } while (0)
84 
85 #define LOG_WARN(fmt, ...)                                                              \
86     do {                                                                                \
87         auto lable = LogLabel();                                                        \
88         if (HiLogIsLoggable(lable.domain, lable.tag, LogLevel::LOG_WARN)) {             \
89             ((void)HILOG_IMPL(lable.type, LogLevel::LOG_WARN, lable.domain, lable.tag,  \
90                               "%{public}s: " fmt, __FUNCTION__, ##__VA_ARGS__));        \
91         }                                                                               \
92     } while (0)
93 
94 #define LOG_ERROR(fmt, ...)                                                             \
95     do {                                                                                \
96         auto lable = LogLabel();                                                        \
97         if (HiLogIsLoggable(lable.domain, lable.tag, LogLevel::LOG_ERROR)) {            \
98             ((void)HILOG_IMPL(lable.type, LogLevel::LOG_ERROR, lable.domain, lable.tag, \
99                               "%{public}s: " fmt, __FUNCTION__, ##__VA_ARGS__));        \
100         }                                                                               \
101     } while (0)
102 
103 #define LOG_FATAL(fmt, ...)                                                             \
104     do {                                                                                \
105         auto lable = LogLabel();                                                        \
106         if (HiLogIsLoggable(lable.domain, lable.tag, LogLevel::LOG_FATAL)) {            \
107             ((void)HILOG_IMPL(lable.type, LogLevel::LOG_FATAL, lable.domain, lable.tag, \
108                               "%{public}s: " fmt, __FUNCTION__, ##__VA_ARGS__));        \
109         }                                                                               \
110     } while (0)
111 
112 #endif
113