Zach Johnson | 3a77003 | 2019-03-27 19:15:38 -0700 | [diff] [blame] | 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 | |
Jack He | 47ac660 | 2019-11-27 11:34:29 -0800 | [diff] [blame] | 21 | #include <cstdlib> |
Hansong Zhang | 30bf869 | 2019-05-02 15:25:54 -0700 | [diff] [blame] | 22 | |
Zach Johnson | 3a77003 | 2019-03-27 19:15:38 -0700 | [diff] [blame] | 23 | #ifndef LOG_TAG |
| 24 | #define LOG_TAG "bt" |
| 25 | #endif |
| 26 | |
| 27 | #if defined(OS_ANDROID) |
| 28 | |
| 29 | #include <log/log.h> |
| 30 | |
Jakub Pawlowski | 183a1b0 | 2020-02-15 22:41:02 +0100 | [diff] [blame] | 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 | |
Jakub Pawlowski | 02d91af | 2019-08-01 19:39:50 +0200 | [diff] [blame] | 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) |
Zach Johnson | 3a77003 | 2019-03-27 19:15:38 -0700 | [diff] [blame] | 40 | |
Jakub Pawlowski | 183a1b0 | 2020-02-15 22:41:02 +0100 | [diff] [blame] | 41 | #endif /* LOG_VERBOSE*/ |
| 42 | |
Zach Johnson | 3a77003 | 2019-03-27 19:15:38 -0700 | [diff] [blame] | 43 | #else |
| 44 | |
| 45 | /* syslog didn't work well here since we would be redefining LOG_DEBUG. */ |
Jack He | 47ac660 | 2019-11-27 11:34:29 -0800 | [diff] [blame] | 46 | #include <chrono> |
| 47 | #include <cstdio> |
| 48 | #include <ctime> |
Zach Johnson | 3a77003 | 2019-03-27 19:15:38 -0700 | [diff] [blame] | 49 | |
Jakub Pawlowski | 183a1b0 | 2020-02-15 22:41:02 +0100 | [diff] [blame] | 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 | |
Jack He | 47ac660 | 2019-11-27 11:34:29 -0800 | [diff] [blame] | 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) |
Zach Johnson | 3a77003 | 2019-03-27 19:15:38 -0700 | [diff] [blame] | 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__) |
Myles Watson | 1372eb3 | 2019-07-23 13:35:40 -0700 | [diff] [blame] | 71 | #define LOG_ALWAYS_FATAL(...) \ |
| 72 | do { \ |
| 73 | LOGWRAPPER(__VA_ARGS__); \ |
| 74 | abort(); \ |
| 75 | } while (false) |
Zach Johnson | 3a77003 | 2019-03-27 19:15:38 -0700 | [diff] [blame] | 76 | |
Jakub Pawlowski | 183a1b0 | 2020-02-15 22:41:02 +0100 | [diff] [blame] | 77 | #endif /* LOG_VERBOE */ |
| 78 | |
Zach Johnson | 3a77003 | 2019-03-27 19:15:38 -0700 | [diff] [blame] | 79 | #endif /* defined(OS_ANDROID) */ |
| 80 | |
Jakub Pawlowski | 02d91af | 2019-08-01 19:39:50 +0200 | [diff] [blame] | 81 | #define ASSERT(condition) \ |
| 82 | do { \ |
| 83 | if (!(condition)) { \ |
| 84 | LOG_ALWAYS_FATAL("assertion '" #condition "' failed"); \ |
| 85 | } \ |
Myles Watson | 49496a3 | 2019-05-14 14:11:55 -0700 | [diff] [blame] | 86 | } while (false) |
Zach Johnson | 3a77003 | 2019-03-27 19:15:38 -0700 | [diff] [blame] | 87 | |
Jakub Pawlowski | 02d91af | 2019-08-01 19:39:50 +0200 | [diff] [blame] | 88 | #define ASSERT_LOG(condition, fmt, args...) \ |
| 89 | do { \ |
| 90 | if (!(condition)) { \ |
| 91 | LOG_ALWAYS_FATAL("assertion '" #condition "' failed - " fmt, ##args); \ |
| 92 | } \ |
Myles Watson | 49496a3 | 2019-05-14 14:11:55 -0700 | [diff] [blame] | 93 | } while (false) |