1 /** @file
2  *  @brief Bluetooth subsystem logging helpers.
3  */
4 
5 /*
6  * Copyright (c) 2017 Nordic Semiconductor ASA
7  * Copyright (c) 2015-2016 Intel Corporation
8  *
9  * SPDX-License-Identifier: Apache-2.0
10  */
11 #ifndef __BT_LOG_H
12 #define __BT_LOG_H
13 
14 #include <linker/sections.h>
15 #include <offsets.h>
16 #include <zephyr.h>
17 #include <logging/log.h>
18 #include <sys/__assert.h>
19 
20 #include <bluetooth/bluetooth.h>
21 #include <bluetooth/uuid.h>
22 #include <bluetooth/hci.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #if !defined(BT_DBG_ENABLED)
29 #define BT_DBG_ENABLED 1
30 #endif
31 
32 #if BT_DBG_ENABLED
33 #define LOG_LEVEL LOG_LEVEL_DBG
34 #else
35 #define LOG_LEVEL CONFIG_BT_LOG_LEVEL
36 #endif
37 
38 LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL);
39 
40 #define BT_DBG(fmt, ...) LOG_DBG(fmt, ##__VA_ARGS__)
41 #define BT_ERR(fmt, ...) LOG_ERR(fmt, ##__VA_ARGS__)
42 #define BT_WARN(fmt, ...) LOG_WRN(fmt, ##__VA_ARGS__)
43 #define BT_INFO(fmt, ...) LOG_INF(fmt, ##__VA_ARGS__)
44 
45 #if defined(CONFIG_BT_ASSERT_VERBOSE)
46 #define BT_ASSERT_PRINT(test) __ASSERT_LOC(test)
47 #define BT_ASSERT_PRINT_MSG(fmt, ...) __ASSERT_MSG_INFO(fmt, ##__VA_ARGS__)
48 #else
49 #define BT_ASSERT_PRINT(test)
50 #define BT_ASSERT_PRINT_MSG(fmt, ...)
51 #endif /* CONFIG_BT_ASSERT_VERBOSE */
52 
53 #if defined(CONFIG_BT_ASSERT_PANIC)
54 #define BT_ASSERT_DIE k_panic
55 #else
56 #define BT_ASSERT_DIE k_oops
57 #endif /* CONFIG_BT_ASSERT_PANIC */
58 
59 #if defined(CONFIG_BT_ASSERT)
60 #define BT_ASSERT(cond)                          \
61 	do {                                     \
62 		if (!(cond)) {                   \
63 			BT_ASSERT_PRINT(cond);   \
64 			BT_ASSERT_DIE();         \
65 		}                                \
66 	} while (0)
67 
68 #define BT_ASSERT_MSG(cond, fmt, ...)                              \
69 	do {                                                       \
70 		if (!(cond)) {                                     \
71 			BT_ASSERT_PRINT(cond);                     \
72 			BT_ASSERT_PRINT_MSG(fmt, ##__VA_ARGS__);   \
73 			BT_ASSERT_DIE();                           \
74 		}                                                  \
75 	} while (0)
76 #else
77 #define BT_ASSERT(cond) __ASSERT_NO_MSG(cond)
78 #define BT_ASSERT_MSG(cond, msg, ...) __ASSERT(cond, msg, ##__VA_ARGS__)
79 #endif/* CONFIG_BT_ASSERT*/
80 
81 #define BT_HEXDUMP_DBG(_data, _length, _str) \
82 		LOG_HEXDUMP_DBG((const uint8_t *)_data, _length, _str)
83 
84 /* NOTE: These helper functions always encodes into the same buffer storage.
85  * It is the responsibility of the user of this function to copy the information
86  * in this string if needed.
87  *
88  * NOTE: These functions are not thread-safe!
89  */
90 const char *bt_hex_real(const void *buf, size_t len);
91 const char *bt_addr_str_real(const bt_addr_t *addr);
92 const char *bt_addr_le_str_real(const bt_addr_le_t *addr);
93 const char *bt_uuid_str_real(const struct bt_uuid *uuid);
94 
95 /* NOTE: log_strdup does not guarantee a duplication of the string.
96  * It is therefore still the responsibility of the user to handle the
97  * restrictions in the underlying function call.
98  */
99 #define bt_hex(buf, len) log_strdup(bt_hex_real(buf, len))
100 #define bt_addr_str(addr) log_strdup(bt_addr_str_real(addr))
101 #define bt_addr_le_str(addr) log_strdup(bt_addr_le_str_real(addr))
102 #define bt_uuid_str(uuid) log_strdup(bt_uuid_str_real(uuid))
103 
104 #ifdef __cplusplus
105 }
106 #endif
107 
108 #endif /* __BT_LOG_H */
109