• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 2019 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #pragma once
20 
21 #include <inttypes.h>
22 
23 #include <cstdlib>
24 
25 #ifndef LOG_TAG
26 #define LOG_TAG "bluetooth"
27 #endif
28 
29 static_assert(LOG_TAG != nullptr, "LOG_TAG should never be NULL");
30 
31 #include "os/log_tags.h"
32 #include "os/logging/log_adapter.h"
33 
34 #if defined(__ANDROID__)
35 
36 #include <log/log.h>
37 #include <log/log_event_list.h>
38 
39 #ifdef FUZZ_TARGET
40 #define LOG_VERBOSE(...)
41 #define LOG_DEBUG(...)
42 #define LOG_INFO(...)
43 #define LOG_WARN(...)
44 #else
45 
46 static_assert(LOG_TAG != nullptr, "LOG_TAG is null after header inclusion");
47 
48 #if __has_include("src/init_flags.rs.h")
49 
50 #include "common/init_flags.h"
51 
52 #define LOG_VERBOSE(fmt, args...)                                                      \
53   do {                                                                                 \
54     if (bluetooth::common::InitFlags::GetLogLevelForTag(LOG_TAG) >= LOG_TAG_VERBOSE) { \
55       ALOGV("%s:%d %s: " fmt, __FILE__, __LINE__, __func__, ##args);                   \
56     }                                                                                  \
57   } while (false)
58 
59 #define LOG_DEBUG(fmt, args...)                                                      \
60   do {                                                                               \
61     if (bluetooth::common::InitFlags::GetLogLevelForTag(LOG_TAG) >= LOG_TAG_DEBUG) { \
62       ALOGD("%s:%d %s: " fmt, __FILE__, __LINE__, __func__, ##args);                 \
63     }                                                                                \
64   } while (false)
65 #endif /* __has_include("src/init_flags.rs.h") */
66 
67 #define LOG_INFO(fmt, args...) ALOGI("%s:%d %s: " fmt, __FILE__, __LINE__, __func__, ##args)
68 #define LOG_WARN(fmt, args...) ALOGW("%s:%d %s: " fmt, __FILE__, __LINE__, __func__, ##args)
69 #endif /* FUZZ_TARGET */
70 #define LOG_ERROR(fmt, args...) ALOGE("%s:%d %s: " fmt, __FILE__, __LINE__, __func__, ##args)
71 
72 #elif defined (ANDROID_EMULATOR)
73 // Log using android emulator logging mechanism
74 #include "android/utils/debug.h"
75 
76 #define LOGWRAPPER(fmt, args...) VERBOSE_INFO(bluetooth, "bluetooth: %s:%d - %s: " fmt, \
77                                               __FILE__, __LINE__, __func__, ##args)
78 
79 #define LOG_VEBOSE(...) LOGWRAPPER(__VA_ARGS__)
80 #define LOG_DEBUG(...)  LOGWRAPPER(__VA_ARGS__)
81 #define LOG_INFO(...)   LOGWRAPPER(__VA_ARGS__)
82 #define LOG_WARN(...)   LOGWRAPPER(__VA_ARGS__)
83 #define LOG_ERROR(...)  LOGWRAPPER(__VA_ARGS__)
84 #define LOG_ALWAYS_FATAL(fmt, args...)                                              \
85   do {                                                                              \
86     fprintf(stderr, "%s:%d - %s: " fmt "\n", __FILE__, __LINE__, __func__, ##args); \
87     abort();                                                                        \
88   } while (false)
89 #elif defined(TARGET_FLOSS)
90 #include "gd/common/init_flags.h"
91 #include "gd/os/syslog.h"
92 
93 // Prefix the log with tag, file, line and function
94 #define LOGWRAPPER(tag, fmt, args...) \
95   write_syslog(tag, "%s:%s:%d - %s: " fmt, LOG_TAG, __FILE__, __LINE__, __func__, ##args)
96 
97 #ifdef FUZZ_TARGET
98 #define LOG_VERBOSE(...)
99 #define LOG_DEBUG(...)
100 #define LOG_INFO(...)
101 #define LOG_WARN(...)
102 #else
103 #define LOG_VERBOSE(...)                                                               \
104   do {                                                                                 \
105     if (bluetooth::common::InitFlags::GetLogLevelForTag(LOG_TAG) >= LOG_TAG_VERBOSE) { \
106       LOGWRAPPER(LOG_TAG_VERBOSE, __VA_ARGS__);                                        \
107     }                                                                                  \
108   } while (false)
109 #define LOG_DEBUG(...)                                                               \
110   do {                                                                               \
111     if (bluetooth::common::InitFlags::GetLogLevelForTag(LOG_TAG) >= LOG_TAG_DEBUG) { \
112       LOGWRAPPER(LOG_TAG_DEBUG, __VA_ARGS__);                                        \
113     }                                                                                \
114   } while (false)
115 #define LOG_INFO(...) LOGWRAPPER(LOG_TAG_INFO, __VA_ARGS__)
116 #define LOG_WARN(...) LOGWRAPPER(LOG_TAG_WARN, __VA_ARGS__)
117 #endif /*FUZZ_TARGET*/
118 #define LOG_ERROR(...) LOGWRAPPER(LOG_TAG_ERROR, __VA_ARGS__)
119 
120 #define LOG_ALWAYS_FATAL(...)               \
121   do {                                      \
122     LOGWRAPPER(LOG_TAG_FATAL, __VA_ARGS__); \
123     abort();                                \
124   } while (false)
125 
126 #else
127 /* syslog didn't work well here since we would be redefining LOG_DEBUG. */
128 #include <sys/syscall.h>
129 #include <sys/types.h>
130 #include <unistd.h>
131 
132 #include <chrono>
133 #include <cstdio>
134 #include <ctime>
135 
136 #define LOGWRAPPER(fmt, args...)                                                                                    \
137   do {                                                                                                              \
138     auto _now = std::chrono::system_clock::now();                                                                   \
139     auto _now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(_now);                                   \
140     auto _now_t = std::chrono::system_clock::to_time_t(_now);                                                       \
141     /* YYYY-MM-DD_HH:MM:SS.sss is 23 byte long, plus 1 for null terminator */                                       \
142     char _buf[24];                                                                                                  \
143     auto l = std::strftime(_buf, sizeof(_buf), "%Y-%m-%d %H:%M:%S", std::localtime(&_now_t));                       \
144     snprintf(                                                                                                       \
145         _buf + l, sizeof(_buf) - l, ".%03u", static_cast<unsigned int>(_now_ms.time_since_epoch().count() % 1000)); \
146     /* pid max is 2^22 = 4194304 in 64-bit system, and 32768 by default, hence 7 digits are needed most */          \
147     fprintf(                                                                                                        \
148         stderr,                                                                                                     \
149         "%s %7d %7ld %s - %s:%d - %s: " fmt "\n",                                                                   \
150         _buf,                                                                                                       \
151         static_cast<int>(getpid()),                                                                                 \
152         syscall(SYS_gettid),                                                                                        \
153         LOG_TAG,                                                                                                    \
154         __FILE__,                                                                                                   \
155         __LINE__,                                                                                                   \
156         __func__,                                                                                                   \
157         ##args);                                                                                                    \
158   } while (false)
159 
160 #ifdef FUZZ_TARGET
161 #define LOG_VERBOSE(...)
162 #define LOG_DEBUG(...)
163 #define LOG_INFO(...)
164 #define LOG_WARN(...)
165 #else
166 #define LOG_VERBOSE(fmt, args...)                                                      \
167   do {                                                                                 \
168     if (bluetooth::common::InitFlags::GetLogLevelForTag(LOG_TAG) >= LOG_TAG_VERBOSE) { \
169       LOGWRAPPER(fmt, ##args);                                                         \
170     }                                                                                  \
171   } while (false)
172 #define LOG_DEBUG(fmt, args...)                                                      \
173   do {                                                                               \
174     if (bluetooth::common::InitFlags::GetLogLevelForTag(LOG_TAG) >= LOG_TAG_DEBUG) { \
175       LOGWRAPPER(fmt, ##args);                                                       \
176     }                                                                                \
177   } while (false)
178 #define LOG_INFO(...) LOGWRAPPER(__VA_ARGS__)
179 #define LOG_WARN(...) LOGWRAPPER(__VA_ARGS__)
180 #endif /* FUZZ_TARGET */
181 #define LOG_ERROR(...) LOGWRAPPER(__VA_ARGS__)
182 
183 #ifndef LOG_ALWAYS_FATAL
184 #define LOG_ALWAYS_FATAL(...) \
185   do {                        \
186     LOGWRAPPER(__VA_ARGS__);  \
187     abort();                  \
188   } while (false)
189 #endif
190 
191 #endif /* defined(__ANDROID__) */
192 
193 #define ASSERT(condition)                                    \
194   do {                                                       \
195     if (!(condition)) {                                      \
196       LOG_ALWAYS_FATAL("assertion '" #condition "' failed"); \
197     }                                                        \
198   } while (false)
199 
200 #define ASSERT_LOG(condition, fmt, args...)                                 \
201   do {                                                                      \
202     if (!(condition)) {                                                     \
203       LOG_ALWAYS_FATAL("assertion '" #condition "' failed - " fmt, ##args); \
204     }                                                                       \
205   } while (false)
206 
207 #ifndef CASE_RETURN_TEXT
208 #define CASE_RETURN_TEXT(code) \
209   case code:                   \
210     return #code
211 #endif
212