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 "bt"
25 #endif
26 
27 #if defined(OS_ANDROID)
28 
29 #include <log/log.h>
30 
31 #ifdef FUZZ_TARGET
32 #define LOG_VERBOSE(...)
33 #define LOG_DEBUG(...)
34 #define LOG_INFO(...)
35 #define LOG_WARN(...)
36 #else
37 #define LOG_VERBOSE(fmt, args...) ALOGV("%s:%d %s: " fmt, __FILE__, __LINE__, __func__, ##args)
38 #define LOG_DEBUG(fmt, args...) ALOGD("%s:%d %s: " fmt, __FILE__, __LINE__, __func__, ##args)
39 #define LOG_INFO(fmt, args...) ALOGI("%s:%d %s: " fmt, __FILE__, __LINE__, __func__, ##args)
40 #define LOG_WARN(fmt, args...) ALOGW("%s:%d %s: " fmt, __FILE__, __LINE__, __func__, ##args)
41 #endif /* FUZZ_TARGET */
42 #define LOG_ERROR(fmt, args...) ALOGE("%s:%d %s: " fmt, __FILE__, __LINE__, __func__, ##args)
43 
44 #else
45 
46 /* syslog didn't work well here since we would be redefining LOG_DEBUG. */
47 #include <chrono>
48 #include <cstdio>
49 #include <ctime>
50 
51 #define LOGWRAPPER(fmt, args...)                                                                                      \
52   do {                                                                                                                \
53     auto now = std::chrono::system_clock::now();                                                                      \
54     auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);                                       \
55     auto now_t = std::chrono::system_clock::to_time_t(now);                                                           \
56     /* YYYY-MM-DD_HH:MM:SS.sss is 23 byte long, plus 1 for null terminator */                                         \
57     char buf[24];                                                                                                     \
58     auto l = std::strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", std::localtime(&now_t));                            \
59     snprintf(buf + l, sizeof(buf) - l, ".%03u", static_cast<unsigned int>(now_ms.time_since_epoch().count() % 1000)); \
60     fprintf(stderr, "%s %s - %s:%d - %s: " fmt "\n", buf, LOG_TAG, __FILE__, __LINE__, __func__, ##args);             \
61   } while (false)
62 
63 #ifdef FUZZ_TARGET
64 #define LOG_VERBOSE(...)
65 #define LOG_DEBUG(...)
66 #define LOG_INFO(...)
67 #define LOG_WARN(...)
68 #else
69 #define LOG_VERBOSE(...) LOGWRAPPER(__VA_ARGS__)
70 #define LOG_DEBUG(...) LOGWRAPPER(__VA_ARGS__)
71 #define LOG_INFO(...) LOGWRAPPER(__VA_ARGS__)
72 #define LOG_WARN(...) LOGWRAPPER(__VA_ARGS__)
73 #endif /* FUZZ_TARGET */
74 #define LOG_ERROR(...) LOGWRAPPER(__VA_ARGS__)
75 #define LOG_ALWAYS_FATAL(...) \
76   do {                        \
77     LOGWRAPPER(__VA_ARGS__);  \
78     abort();                  \
79   } while (false)
80 #define android_errorWriteLog(tag, subTag) LOG_ERROR("ERROR tag: 0x%x, sub_tag: %s", tag, subTag)
81 #define LOG_EVENT_INT(...)
82 
83 #endif /* defined(OS_ANDROID) */
84 
85 #define ASSERT(condition)                                    \
86   do {                                                       \
87     if (!(condition)) {                                      \
88       LOG_ALWAYS_FATAL("assertion '" #condition "' failed"); \
89     }                                                        \
90   } while (false)
91 
92 #define ASSERT_LOG(condition, fmt, args...)                                 \
93   do {                                                                      \
94     if (!(condition)) {                                                     \
95       LOG_ALWAYS_FATAL("assertion '" #condition "' failed - " fmt, ##args); \
96     }                                                                       \
97   } while (false)
98