blob: 29ff7517e0fb3f95f2bcfe1176ec36bbfc580652 [file] [log] [blame]
Zach Johnson3a770032019-03-27 19:15:38 -07001/******************************************************************************
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 He47ac6602019-11-27 11:34:29 -080021#include <cstdlib>
Hansong Zhang30bf8692019-05-02 15:25:54 -070022
Zach Johnson3a770032019-03-27 19:15:38 -070023#ifndef LOG_TAG
24#define LOG_TAG "bt"
25#endif
26
27#if defined(OS_ANDROID)
28
29#include <log/log.h>
30
Jakub Pawlowski183a1b02020-02-15 22:41:02 +010031/* 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 Pawlowski02d91af2019-08-01 19:39:50 +020035#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 Johnson3a770032019-03-27 19:15:38 -070040
Jakub Pawlowski183a1b02020-02-15 22:41:02 +010041#endif /* LOG_VERBOSE*/
42
Zach Johnson3a770032019-03-27 19:15:38 -070043#else
44
45/* syslog didn't work well here since we would be redefining LOG_DEBUG. */
Jack He47ac6602019-11-27 11:34:29 -080046#include <chrono>
47#include <cstdio>
48#include <ctime>
Zach Johnson3a770032019-03-27 19:15:38 -070049
Jakub Pawlowski183a1b02020-02-15 22:41:02 +010050/* 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 He47ac6602019-11-27 11:34:29 -080054#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 Johnson3a770032019-03-27 19:15:38 -070065
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 Watson1372eb32019-07-23 13:35:40 -070071#define LOG_ALWAYS_FATAL(...) \
72 do { \
73 LOGWRAPPER(__VA_ARGS__); \
74 abort(); \
75 } while (false)
Zach Johnson3a770032019-03-27 19:15:38 -070076
Jakub Pawlowski183a1b02020-02-15 22:41:02 +010077#endif /* LOG_VERBOE */
78
Zach Johnson3a770032019-03-27 19:15:38 -070079#endif /* defined(OS_ANDROID) */
80
Jakub Pawlowski02d91af2019-08-01 19:39:50 +020081#define ASSERT(condition) \
82 do { \
83 if (!(condition)) { \
84 LOG_ALWAYS_FATAL("assertion '" #condition "' failed"); \
85 } \
Myles Watson49496a32019-05-14 14:11:55 -070086 } while (false)
Zach Johnson3a770032019-03-27 19:15:38 -070087
Jakub Pawlowski02d91af2019-08-01 19:39:50 +020088#define ASSERT_LOG(condition, fmt, args...) \
89 do { \
90 if (!(condition)) { \
91 LOG_ALWAYS_FATAL("assertion '" #condition "' failed - " fmt, ##args); \
92 } \
Myles Watson49496a32019-05-14 14:11:55 -070093 } while (false)