• 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 <cstdlib>
22 
23 #ifndef LOG_TAG
24 #define LOG_TAG "bluetooth"
25 #endif
26 
27 static_assert(LOG_TAG != nullptr, "LOG_TAG should never be NULL");
28 
29 #if defined(OS_ANDROID)
30 
31 #include <log/log.h>
32 
33 #ifdef FUZZ_TARGET
34 #define LOG_VERBOSE(...)
35 #define LOG_DEBUG(...)
36 #define LOG_INFO(...)
37 #define LOG_WARN(...)
38 #else
39 
40 static_assert(LOG_TAG != nullptr, "LOG_TAG is null after header inclusion");
41 
42 #define LOG_VERBOSE(fmt, args...)                                             \
43   do {                                                                        \
44     if (bluetooth::common::InitFlags::IsDebugLoggingEnabledForTag(LOG_TAG)) { \
45       ALOGV("%s:%d %s: " fmt, __FILE__, __LINE__, __func__, ##args);          \
46     }                                                                         \
47   } while (false)
48 
49 #define LOG_DEBUG(fmt, args...)                                               \
50   do {                                                                        \
51     if (bluetooth::common::InitFlags::IsDebugLoggingEnabledForTag(LOG_TAG)) { \
52       ALOGD("%s:%d %s: " fmt, __FILE__, __LINE__, __func__, ##args);          \
53     }                                                                         \
54   } while (false)
55 
56 #define LOG_INFO(fmt, args...) ALOGI("%s:%d %s: " fmt, __FILE__, __LINE__, __func__, ##args)
57 #define LOG_WARN(fmt, args...) ALOGW("%s:%d %s: " fmt, __FILE__, __LINE__, __func__, ##args)
58 #endif /* FUZZ_TARGET */
59 #define LOG_ERROR(fmt, args...) ALOGE("%s:%d %s: " fmt, __FILE__, __LINE__, __func__, ##args)
60 
61 #else
62 
63 /* syslog didn't work well here since we would be redefining LOG_DEBUG. */
64 #include <chrono>
65 #include <cstdio>
66 #include <ctime>
67 
68 #define LOGWRAPPER(fmt, args...)                                                                                    \
69   do {                                                                                                              \
70     auto _now = std::chrono::system_clock::now();                                                                   \
71     auto _now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(_now);                                   \
72     auto _now_t = std::chrono::system_clock::to_time_t(_now);                                                       \
73     /* YYYY-MM-DD_HH:MM:SS.sss is 23 byte long, plus 1 for null terminator */                                       \
74     char _buf[24];                                                                                                  \
75     auto l = std::strftime(_buf, sizeof(_buf), "%Y-%m-%d %H:%M:%S", std::localtime(&_now_t));                       \
76     snprintf(                                                                                                       \
77         _buf + l, sizeof(_buf) - l, ".%03u", static_cast<unsigned int>(_now_ms.time_since_epoch().count() % 1000)); \
78     fprintf(stderr, "%s %s - %s:%d - %s: " fmt "\n", _buf, LOG_TAG, __FILE__, __LINE__, __func__, ##args);          \
79   } while (false)
80 
81 #ifdef FUZZ_TARGET
82 #define LOG_VERBOSE(...)
83 #define LOG_DEBUG(...)
84 #define LOG_INFO(...)
85 #define LOG_WARN(...)
86 #else
87 #define LOG_VERBOSE(...) LOGWRAPPER(__VA_ARGS__)
88 #define LOG_DEBUG(...) LOGWRAPPER(__VA_ARGS__)
89 #define LOG_INFO(...) LOGWRAPPER(__VA_ARGS__)
90 #define LOG_WARN(...) LOGWRAPPER(__VA_ARGS__)
91 #endif /* FUZZ_TARGET */
92 #define LOG_ERROR(...) LOGWRAPPER(__VA_ARGS__)
93 
94 #ifndef LOG_ALWAYS_FATAL
95 #define LOG_ALWAYS_FATAL(...) \
96   do {                        \
97     LOGWRAPPER(__VA_ARGS__);  \
98     abort();                  \
99   } while (false)
100 #endif
101 
102 #ifndef android_errorWriteLog
103 #define android_errorWriteLog(tag, subTag) LOG_ERROR("ERROR tag: 0x%x, sub_tag: %s", tag, subTag)
104 #endif
105 
106 #ifndef android_errorWriteWithInfoLog
107 #define android_errorWriteWithInfoLog(tag, subTag, uid, data, dataLen) \
108   LOG_ERROR("ERROR tag: 0x%x, sub_tag: %s", tag, subTag)
109 #endif
110 
111 #ifndef LOG_EVENT_INT
112 #define LOG_EVENT_INT(...)
113 #endif
114 
115 #endif /* defined(OS_ANDROID) */
116 
117 #define ASSERT(condition)                                    \
118   do {                                                       \
119     if (!(condition)) {                                      \
120       LOG_ALWAYS_FATAL("assertion '" #condition "' failed"); \
121     }                                                        \
122   } while (false)
123 
124 #define ASSERT_LOG(condition, fmt, args...)                                 \
125   do {                                                                      \
126     if (!(condition)) {                                                     \
127       LOG_ALWAYS_FATAL("assertion '" #condition "' failed - " fmt, ##args); \
128     }                                                                       \
129   } while (false)
130 
131 #ifndef CASE_RETURN_TEXT
132 #define CASE_RETURN_TEXT(code) \
133   case code:                   \
134     return #code
135 #endif
136