nrf_log_backend_interface.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /**
  2. * Copyright (c) 2017 - 2020, 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_BACKEND_INTERFACE_H
  41. #define NRF_LOG_BACKEND_INTERFACE_H
  42. /**@file
  43. * @addtogroup nrf_log Logger module
  44. * @ingroup app_common
  45. *
  46. * @defgroup nrf_log_backend_interface Logger backend interface
  47. * @{
  48. * @ingroup nrf_log
  49. * @brief The nrf_log backend interface.
  50. */
  51. #if NRF_MODULE_ENABLED(NRF_LOG)
  52. #include "nrf_memobj.h"
  53. #endif
  54. #include "nrf_section.h"
  55. #include "nordic_common.h"
  56. #include <stdint.h>
  57. #ifdef __cplusplus
  58. extern "C" {
  59. #endif
  60. /**
  61. * @brief nrf_log entry.
  62. */
  63. #if NRF_LOG_ENABLED
  64. typedef nrf_memobj_t nrf_log_entry_t;
  65. #else
  66. typedef void nrf_log_entry_t;
  67. #endif
  68. /* Forward declaration of the nrf_log_backend_t type. */
  69. typedef struct nrf_log_backend_s nrf_log_backend_t;
  70. /**
  71. * @brief Logger backend API.
  72. */
  73. typedef struct
  74. {
  75. /**
  76. * @brief @ref nrf_log_backend_put
  77. */
  78. void (*put)(nrf_log_backend_t const * p_backend, nrf_log_entry_t * p_entry);
  79. /**
  80. * @brief @ref nrf_log_backend_panic_set
  81. */
  82. void (*panic_set)(nrf_log_backend_t const * p_backend);
  83. /**
  84. * @brief @ref nrf_log_backend_flush
  85. */
  86. void (*flush)(nrf_log_backend_t const * p_backend);
  87. } nrf_log_backend_api_t;
  88. /**
  89. * @brief Logger backend control block.
  90. */
  91. typedef struct
  92. {
  93. nrf_log_backend_t const * p_next; //!< Pointer to the next backend in the logger.
  94. uint8_t id; //!< ID of the backend.
  95. bool enabled; //!< Enable flag.
  96. } nrf_log_backend_cb_t;
  97. /**
  98. * @brief Logger backend structure.
  99. */
  100. struct nrf_log_backend_s
  101. {
  102. nrf_log_backend_api_t const * p_api; //!< Pointer to interface.
  103. void * p_ctx; //!< User context.
  104. char * p_name; //!< Name of the backend.
  105. nrf_log_backend_cb_t * p_cb; //!< Pointer to the backend control block.
  106. };
  107. #define NRF_LOG_BACKEND_SECTION_NAME log_backends
  108. #define NRF_LOG_BACKEND_SUBSECTION_NAME(_name) NRF_LOG_BACKEND_SECTION_NAME
  109. /** @brief Invalid ID value indicating that logger backend is not attached to the logger frontend.*/
  110. #define NRF_LOG_BACKEND_INVALID_ID 0xFF
  111. /** @brief Memory section where backends are located. */
  112. NRF_SECTION_DEF(NRF_LOG_BACKEND_SECTION_NAME, nrf_log_backend_t);
  113. /**
  114. * @brief Macro for creating a logger backend instance.
  115. *
  116. * @param _name Name of the backend instance.
  117. * @param _api Logger backend API.
  118. * @param _p_ctx Pointer to the user context.
  119. */
  120. #define NRF_LOG_BACKEND_DEF(_name, _api, _p_ctx) \
  121. static nrf_log_backend_cb_t CONCAT_2(log_backend_cb_, _name) = { \
  122. .enabled = false, \
  123. .id = NRF_LOG_BACKEND_INVALID_ID, \
  124. .p_next = NULL \
  125. }; \
  126. NRF_SECTION_ITEM_REGISTER(NRF_LOG_BACKEND_SUBSECTION_NAME(_name), \
  127. static const nrf_log_backend_t _name) = { \
  128. .p_api = &_api, \
  129. .p_ctx = _p_ctx, \
  130. .p_cb = &CONCAT_2(log_backend_cb_, _name), \
  131. .p_name = (char *)STRINGIFY(_name) \
  132. }
  133. /**
  134. * @brief Function for putting message with log entry to the backend.
  135. *
  136. * @param[in] p_backend Pointer to the backend instance.
  137. * @param[in] p_msg Pointer to message with log entry.
  138. */
  139. __STATIC_INLINE void nrf_log_backend_put(nrf_log_backend_t const * const p_backend,
  140. nrf_log_entry_t * p_msg);
  141. /**
  142. * @brief Function for reconfiguring backend to panic mode.
  143. *
  144. * @param[in] p_backend Pointer to the backend instance.
  145. */
  146. __STATIC_INLINE void nrf_log_backend_panic_set(nrf_log_backend_t const * const p_backend);
  147. /**
  148. * @brief Function for flushing backend.
  149. *
  150. * On flushing request backend should release log message(s).
  151. *
  152. * @param[in] p_backend Pointer to the backend instance.
  153. */
  154. __STATIC_INLINE void nrf_log_backend_flush(nrf_log_backend_t const * const p_backend);
  155. /**
  156. * @brief Function for setting backend id.
  157. *
  158. * @note It is used internally by the logger.
  159. *
  160. * @param[in] p_backend Pointer to the backend instance.
  161. * @param[in] id Id.
  162. */
  163. __STATIC_INLINE void nrf_log_backend_id_set(nrf_log_backend_t const * const p_backend, uint8_t id);
  164. /**
  165. * @brief Function for getting backend id.
  166. *
  167. * @note It is used internally by the logger.
  168. *
  169. * @param[in] p_backend Pointer to the backend instance.
  170. * @return Id.
  171. */
  172. __STATIC_INLINE uint8_t nrf_log_backend_id_get(nrf_log_backend_t const * const p_backend);
  173. /**
  174. * @brief Function for enabling backend.
  175. *
  176. * @param[in] p_backend Pointer to the backend instance.
  177. */
  178. __STATIC_INLINE void nrf_log_backend_enable(nrf_log_backend_t const * const p_backend);
  179. /**
  180. * @brief Function for disabling backend.
  181. *
  182. * @param[in] p_backend Pointer to the backend instance.
  183. */
  184. __STATIC_INLINE void nrf_log_backend_disable(nrf_log_backend_t const * const p_backend);
  185. /**
  186. * @brief Function for checking state of the backend.
  187. *
  188. * @param[in] p_backend Pointer to the backend instance.
  189. *
  190. * @return True if backend is enabled, false otherwise.
  191. */
  192. __STATIC_INLINE bool nrf_log_backend_is_enabled(nrf_log_backend_t const * const p_backend);
  193. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  194. __STATIC_INLINE void nrf_log_backend_put(nrf_log_backend_t const * const p_backend,
  195. nrf_log_entry_t * p_msg)
  196. {
  197. p_backend->p_api->put(p_backend, p_msg);
  198. }
  199. __STATIC_INLINE void nrf_log_backend_panic_set(nrf_log_backend_t const * const p_backend)
  200. {
  201. p_backend->p_api->panic_set(p_backend);
  202. }
  203. __STATIC_INLINE void nrf_log_backend_flush(nrf_log_backend_t const * const p_backend)
  204. {
  205. p_backend->p_api->flush(p_backend);
  206. }
  207. __STATIC_INLINE void nrf_log_backend_id_set(nrf_log_backend_t const * const p_backend, uint8_t id)
  208. {
  209. p_backend->p_cb->id = id;
  210. }
  211. __STATIC_INLINE uint8_t nrf_log_backend_id_get(nrf_log_backend_t const * const p_backend)
  212. {
  213. return p_backend->p_cb->id;
  214. }
  215. __STATIC_INLINE void nrf_log_backend_enable(nrf_log_backend_t const * const p_backend)
  216. {
  217. p_backend->p_cb->enabled = true;
  218. }
  219. __STATIC_INLINE void nrf_log_backend_disable(nrf_log_backend_t const * const p_backend)
  220. {
  221. p_backend->p_cb->enabled = false;
  222. }
  223. __STATIC_INLINE bool nrf_log_backend_is_enabled(nrf_log_backend_t const * const p_backend)
  224. {
  225. return p_backend->p_cb->enabled;
  226. }
  227. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  228. #ifdef __cplusplus
  229. }
  230. #endif
  231. #endif //NRF_LOG_BACKEND_INTERFACE_H
  232. /** @} */