log.h 3.4 KB

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