nrf_log_instance.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * Copyright (c) 2018 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRF_LOG_INSTANCE_H
  41. #define NRF_LOG_INSTANCE_H
  42. #include "sdk_config.h"
  43. #include "nrf_section.h"
  44. #include "nrf_log_types.h"
  45. #include <stdint.h>
  46. /*
  47. * For GCC, sections are sorted in the group by the linker. For IAR and KEIL, it is assumed that linker will sort
  48. * dynamic and const section in the same order (but in different locations). Proper message formatting
  49. * is based on that assumption.
  50. */
  51. #if defined(__GNUC__)
  52. #define NRF_LOG_DYNAMIC_SECTION_NAME(_module_name) CONCAT_2(log_dynamic_data_,_module_name)
  53. #define NRF_LOG_FILTER_SECTION_NAME(_module_name) CONCAT_2(log_filter_data_,_module_name)
  54. #define NRF_LOG_CONST_SECTION_NAME(_module_name) CONCAT_2(log_const_data_,_module_name)
  55. #else
  56. #define NRF_LOG_DYNAMIC_SECTION_NAME(_module_name) log_dynamic_data
  57. #define NRF_LOG_FILTER_SECTION_NAME(_module_name) log_filter_data
  58. #define NRF_LOG_CONST_SECTION_NAME(_module_name) log_const_data
  59. #endif
  60. #define NRF_LOG_ITEM_DATA(_name) CONCAT_3(m_nrf_log_,_name,_logs_data)
  61. #define NRF_LOG_ITEM_DATA_DYNAMIC(_name) CONCAT_2(NRF_LOG_ITEM_DATA(_name),_dynamic)
  62. #define NRF_LOG_ITEM_DATA_FILTER(_name) CONCAT_2(NRF_LOG_ITEM_DATA(_name),_filter)
  63. #define NRF_LOG_ITEM_DATA_CONST(_name) CONCAT_2(NRF_LOG_ITEM_DATA(_name),_const)
  64. #ifdef UNIT_TEST
  65. #define _CONST
  66. #else
  67. #define _CONST const
  68. #endif
  69. /*lint -save -esym(526,log_const_data*) -esym(526,log_dynamic_data*)*/
  70. NRF_SECTION_DEF(log_dynamic_data, nrf_log_module_dynamic_data_t);
  71. NRF_SECTION_DEF(log_filter_data, nrf_log_module_filter_data_t);
  72. NRF_SECTION_DEF(log_const_data, nrf_log_module_const_data_t);
  73. /*lint -restore*/
  74. #define NRF_LOG_INTERNAL_CONST_ITEM_REGISTER( \
  75. _name, _str_name, _info_color, _debug_color, _initial_lvl, _compiled_lvl) \
  76. NRF_SECTION_ITEM_REGISTER(NRF_LOG_CONST_SECTION_NAME(_name), \
  77. _CONST nrf_log_module_const_data_t NRF_LOG_ITEM_DATA_CONST(_name)) = { \
  78. .p_module_name = _str_name, \
  79. .info_color_id = (_info_color), \
  80. .debug_color_id = (_debug_color), \
  81. .compiled_lvl = (nrf_log_severity_t)(_compiled_lvl), \
  82. .initial_lvl = (nrf_log_severity_t)(_initial_lvl), \
  83. }
  84. #if NRF_LOG_FILTERS_ENABLED
  85. #define NRF_LOG_INTERNAL_ITEM_REGISTER( \
  86. _name, _str_name, _info_color, _debug_color, _initial_lvl, _compiled_lvl) \
  87. NRF_LOG_INTERNAL_CONST_ITEM_REGISTER(_name, \
  88. _str_name, \
  89. _info_color, \
  90. _debug_color, \
  91. _initial_lvl, \
  92. _compiled_lvl); \
  93. NRF_SECTION_ITEM_REGISTER(NRF_LOG_DYNAMIC_SECTION_NAME(_name), \
  94. nrf_log_module_dynamic_data_t NRF_LOG_ITEM_DATA_DYNAMIC(_name)); \
  95. NRF_SECTION_ITEM_REGISTER(NRF_LOG_FILTER_SECTION_NAME(_name), \
  96. nrf_log_module_filter_data_t NRF_LOG_ITEM_DATA_FILTER(_name))
  97. #else
  98. #define NRF_LOG_INTERNAL_ITEM_REGISTER( \
  99. _name, _str_name, _info_color, _debug_color, _initial_lvl, _compiled_lvl) \
  100. NRF_LOG_INTERNAL_CONST_ITEM_REGISTER(_name, \
  101. _str_name, \
  102. _info_color, \
  103. _debug_color, \
  104. _initial_lvl, \
  105. _compiled_lvl)
  106. #endif
  107. /**@file
  108. *
  109. * @defgroup nrf_log_instance Macros for logging on instance level
  110. * @{
  111. * @ingroup nrf_log
  112. *
  113. * @brief Macros for logging on instance level
  114. */
  115. /** @def NRF_LOG_INSTANCE_PTR_DECLARE
  116. * @brief Macro for declaring a logger instance pointer in the module stucture.
  117. */
  118. /** @def NRF_LOG_INSTANCE_REGISTER
  119. * @brief Macro for creating an independent module instance.
  120. *
  121. * Module instance provides filtering of logs on instance level instead of module level.
  122. */
  123. /** @def NRF_LOG_INSTANCE_PTR_INIT
  124. * @brief Macro for initializing a pointer to the logger instance.
  125. */
  126. /** @} */
  127. #if NRF_LOG_ENABLED && NRF_LOG_FILTERS_ENABLED
  128. #define NRF_LOG_INSTANCE_PTR_DECLARE(_p_name) nrf_log_module_dynamic_data_t * _p_name;
  129. #define NRF_LOG_INSTANCE_REGISTER( \
  130. _module_name, _inst_name, _info_color, _debug_color, _initial_lvl, _compiled_lvl) \
  131. NRF_LOG_INTERNAL_ITEM_REGISTER(CONCAT_3(_module_name,_,_inst_name), \
  132. STRINGIFY(_module_name._inst_name), \
  133. _info_color, \
  134. _debug_color, \
  135. _initial_lvl, \
  136. _compiled_lvl)
  137. #define NRF_LOG_INSTANCE_PTR_INIT(_p_name, _module_name, _inst_name) \
  138. ._p_name = &NRF_LOG_ITEM_DATA_DYNAMIC(CONCAT_3(_module_name,_,_inst_name)),
  139. #else
  140. #define NRF_LOG_INSTANCE_PTR_DECLARE(_p_name)
  141. #define NRF_LOG_INSTANCE_REGISTER(_module_name, _inst_name, info_color, debug_color, _initial_lvl, compiled_lvl)
  142. #define NRF_LOG_INSTANCE_PTR_INIT(_p_name, _module_name, _inst_name)
  143. #endif
  144. #endif //NRF_LOG_INSTANCE_H