1 /*
2  * Copyright (C) 2021 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 LOG_H
17 #define LOG_H
18 
19 #undef LOG_DOMAIN
20 #define LOG_DOMAIN 0xD000102
21 #ifndef LOG_TAG
22 #define LOG_TAG "Bluetooth"
23 #endif
24 
25 #include "hilog/log.h"
26 
27 #ifdef HILOGF
28 #undef HILOGF
29 #endif
30 
31 #ifdef HILOGE
32 #undef HILOGE
33 #endif
34 
35 #ifdef HILOGW
36 #undef HILOGW
37 #endif
38 
39 #ifdef HILOGI
40 #undef HILOGI
41 #endif
42 
43 #ifdef HILOGD
44 #undef HILOGD
45 #endif
46 
47 #define FILENAME_SHORT (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__)
48 
49 #define HILOGD(fmt, ...)                                                \
50     HILOG_DEBUG(LOG_CORE, "[%{public}s(%{public}s:%{public}d)]" fmt,    \
51         FILENAME_SHORT, __FUNCTION__, __LINE__, ##__VA_ARGS__)
52 #define HILOGI(fmt, ...)                                                \
53     HILOG_INFO(LOG_CORE, "[%{public}s(%{public}s:%{public}d)]" fmt,     \
54         FILENAME_SHORT, __FUNCTION__, __LINE__, ##__VA_ARGS__)
55 #define HILOGW(fmt, ...)                                                \
56     HILOG_WARN(LOG_CORE, "[%{public}s(%{public}s:%{public}d)]" fmt,     \
57         FILENAME_SHORT, __FUNCTION__, __LINE__, ##__VA_ARGS__)
58 #define HILOGE(fmt, ...)                                                \
59     HILOG_ERROR(LOG_CORE, "[%{public}s(%{public}s:%{public}d)]" fmt,    \
60         FILENAME_SHORT, __FUNCTION__, __LINE__, ##__VA_ARGS__)
61 #define HILOGF(fmt, ...)                                                \
62     HILOG_FATAL(LOG_CORE, "[%{public}s(%{public}s:%{public}d)]" fmt,    \
63         FILENAME_SHORT, __FUNCTION__, __LINE__, ##__VA_ARGS__)
64 
65 #ifdef LOG_DEBUG
66 #undef LOG_DEBUG
67 #endif
68 
69 #ifdef LOG_INFO
70 #undef LOG_INFO
71 #endif
72 
73 #ifdef LOG_WARN
74 #undef LOG_WARN
75 #endif
76 
77 #ifdef LOG_ERROR
78 #undef LOG_ERROR
79 #endif
80 
81 #ifdef LOG_FATAL
82 #undef LOG_FATAL
83 #endif
84 
85 #ifdef DEBUG
86 #include <assert.h>
87 #define ASSERT(x) assert(x)
88 #else
89 #define ASSERT(x)
90 #endif
91 
92 #ifdef DEBUG
93 #include <assert.h>
94 #define ASSERT_LOG(x, fmt, args...)                                        \
95     do {                                                                      \
96         if (!(x)) {                                                     \
97         HILOGE("assertion '" #x"' failed - " fmt, ##args); \
98         }                                                                       \
99     } while (false)
100 #else
101 #define ASSERT_LOG(x, fmt, args...)
102 #endif
103 
104 #define LOG_VERBOSE(...) HILOG_DEBUG(LOG_CORE, __VA_ARGS__)
105 #define LOG_DEBUG(...) HILOG_DEBUG(LOG_CORE, __VA_ARGS__)
106 #define LOG_INFO(...) HILOG_INFO(LOG_CORE, __VA_ARGS__)
107 #define LOG_WARN(...) HILOG_WARN(LOG_CORE, __VA_ARGS__)
108 #define LOG_ERROR(...) HILOG_ERROR(LOG_CORE, __VA_ARGS__)
109 #define LOG_FATAL(...) HILOG_FATAL(LOG_CORE, __VA_ARGS__)
110 
111 #define ALOGV(...) HILOG_DEBUG(LOG_CORE, __VA_ARGS__)
112 #define ALOGD(...) HILOG_DEBUG(LOG_CORE, __VA_ARGS__)
113 #define ALOGI(...) HILOG_WARN(LOG_CORE, __VA_ARGS__)
114 #define ALOGW(...) HILOG_WARN(LOG_CORE, __VA_ARGS__)
115 #define ALOGE(...) HILOG_ERROR(LOG_CORE, __VA_ARGS__)
116 
117 #ifndef LOG_EVENT_INT
118 #define LOG_EVENT_INT(tag, subTag) LOG_ERROR("ERROR tag num: 0x%x, opcode: %ld", tag, subTag)
119 #endif
120 
121 #ifdef CHECK_AND_RETURN_LOG
122 #undef CHECK_AND_RETURN_LOG
123 #endif
124 
125 #define CHECK_AND_RETURN_LOG(cond, fmt, ...)        \
126     do {                                            \
127         if (!(cond)) {                              \
128             HILOGE(fmt, ##__VA_ARGS__);             \
129             return;                                 \
130         }                                           \
131     } while (0)
132 
133 #ifdef CHECK_AND_RETURN_LOG_RET
134 #undef CHECK_AND_RETURN_LOG_RET
135 #endif
136 
137 #define CHECK_AND_RETURN_LOG_RET(cond, ret, fmt, ...)               \
138     do {                                                            \
139         if (!(cond)) {                                              \
140             HILOGE(fmt, ##__VA_ARGS__);    \
141             return ret;                                             \
142         }                                                           \
143     } while (0)
144 
145 #endif  // LOG_H
146