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