nrf_log.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /**
  2. * Copyright (c) 2016 - 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. /**@file
  41. *
  42. * @defgroup nrf_log Logger module
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief The nrf_log module interface.
  47. */
  48. #ifndef NRF_LOG_H_
  49. #define NRF_LOG_H_
  50. #include "sdk_common.h"
  51. #include "nrf_section.h"
  52. #if NRF_MODULE_ENABLED(NRF_LOG)
  53. #include "nrf_strerror.h"
  54. #define NRF_LOG_ERROR_STRING_GET(code) nrf_strerror_get(code)
  55. #else
  56. #define NRF_LOG_ERROR_STRING_GET(code) ""
  57. #endif
  58. #ifdef __cplusplus
  59. extern "C" {
  60. #endif
  61. /** @brief Severity level for the module.
  62. *
  63. * The severity level can be defined in a module to override the default.
  64. */
  65. #ifndef NRF_LOG_LEVEL
  66. #define NRF_LOG_LEVEL NRF_LOG_DEFAULT_LEVEL
  67. #endif
  68. /** @brief Initial severity if filtering is enabled.
  69. */
  70. #ifndef NRF_LOG_INITIAL_LEVEL
  71. #define NRF_LOG_INITIAL_LEVEL NRF_LOG_LEVEL
  72. #endif
  73. #include "nrf_log_internal.h"
  74. /** @def NRF_LOG_ERROR
  75. * @brief Macro for logging error messages. It takes a printf-like, formatted
  76. * string with up to seven arguments.
  77. *
  78. * @details This macro is compiled only if @ref NRF_LOG_LEVEL includes error logs.
  79. */
  80. /** @def NRF_LOG_WARNING
  81. * @brief Macro for logging error messages. It takes a printf-like, formatted
  82. * string with up to seven arguments.
  83. *
  84. * @details This macro is compiled only if @ref NRF_LOG_LEVEL includes warning logs.
  85. */
  86. /** @def NRF_LOG_INFO
  87. * @brief Macro for logging error messages. It takes a printf-like, formatted
  88. * string with up to seven arguments.
  89. *
  90. * @details This macro is compiled only if @ref NRF_LOG_LEVEL includes info logs.
  91. */
  92. /** @def NRF_LOG_DEBUG
  93. * @brief Macro for logging error messages. It takes a printf-like, formatted
  94. * string with up to seven arguments.
  95. *
  96. * @details This macro is compiled only if @ref NRF_LOG_LEVEL includes debug logs.
  97. */
  98. #define NRF_LOG_ERROR(...) NRF_LOG_INTERNAL_ERROR(__VA_ARGS__)
  99. #define NRF_LOG_WARNING(...) NRF_LOG_INTERNAL_WARNING( __VA_ARGS__)
  100. #define NRF_LOG_INFO(...) NRF_LOG_INTERNAL_INFO( __VA_ARGS__)
  101. #define NRF_LOG_DEBUG(...) NRF_LOG_INTERNAL_DEBUG( __VA_ARGS__)
  102. /** @def NRF_LOG_INST_ERROR
  103. * @brief Macro for logging error messages for a given module instance. It takes a printf-like, formatted
  104. * string with up to seven arguments.
  105. *
  106. * @param p_inst Pointer to the instance with logging support.
  107. *
  108. * @details This macro is compiled only if @ref NRF_LOG_LEVEL includes error logs.
  109. */
  110. /** @def NRF_LOG_INST_WARNING
  111. * @brief Macro for logging error messages for a given module instance. It takes a printf-like, formatted
  112. * string with up to seven arguments.
  113. *
  114. * @param p_inst Pointer to the instance with logging support.
  115. *
  116. * @details This macro is compiled only if @ref NRF_LOG_LEVEL includes error logs.
  117. */
  118. /** @def NRF_LOG_INST_INFO
  119. * @brief Macro for logging error messages for a given module instance. It takes a printf-like, formatted
  120. * string with up to seven arguments.
  121. *
  122. * @param p_inst Pointer to the instance with logging support.
  123. *
  124. * @details This macro is compiled only if @ref NRF_LOG_LEVEL includes error logs.
  125. */
  126. /** @def NRF_LOG_INST_DEBUG
  127. * @brief Macro for logging error messages for given module instance. It takes a printf-like, formatted
  128. * string with up to seven arguments.
  129. *
  130. * @param p_inst Pointer to the instance with logging support.
  131. *
  132. * @details This macro is compiled only if @ref NRF_LOG_LEVEL includes error logs.
  133. */
  134. #define NRF_LOG_INST_ERROR(p_inst,...) NRF_LOG_INTERNAL_INST_ERROR(p_inst,__VA_ARGS__)
  135. #define NRF_LOG_INST_WARNING(p_inst,...) NRF_LOG_INTERNAL_INST_WARNING(p_inst,__VA_ARGS__)
  136. #define NRF_LOG_INST_INFO(p_inst,...) NRF_LOG_INTERNAL_INST_INFO(p_inst, __VA_ARGS__)
  137. #define NRF_LOG_INST_DEBUG(p_inst,...) NRF_LOG_INTERNAL_INST_DEBUG(p_inst, __VA_ARGS__)
  138. /**
  139. * @brief Macro for logging a formatted string without any prefix or timestamp.
  140. */
  141. #define NRF_LOG_RAW_INFO(...) NRF_LOG_INTERNAL_RAW_INFO( __VA_ARGS__)
  142. /** @def NRF_LOG_HEXDUMP_ERROR
  143. * @brief Macro for logging raw bytes.
  144. * @details This macro is compiled only if @ref NRF_LOG_LEVEL includes error logs.
  145. *
  146. * @param p_data Pointer to data.
  147. * @param len Data length in bytes.
  148. */
  149. /** @def NRF_LOG_HEXDUMP_WARNING
  150. * @brief Macro for logging raw bytes.
  151. * @details This macro is compiled only if @ref NRF_LOG_LEVEL includes warning logs.
  152. *
  153. * @param p_data Pointer to data.
  154. * @param len Data length in bytes.
  155. */
  156. /** @def NRF_LOG_HEXDUMP_INFO
  157. * @brief Macro for logging raw bytes.
  158. * @details This macro is compiled only if @ref NRF_LOG_LEVEL includes info logs.
  159. *
  160. * @param p_data Pointer to data.
  161. * @param len Data length in bytes.
  162. */
  163. /** @def NRF_LOG_HEXDUMP_DEBUG
  164. * @brief Macro for logging raw bytes.
  165. * @details This macro is compiled only if @ref NRF_LOG_LEVEL includes debug logs.
  166. *
  167. * @param p_data Pointer to data.
  168. * @param len Data length in bytes.
  169. */
  170. #define NRF_LOG_HEXDUMP_ERROR(p_data, len) NRF_LOG_INTERNAL_HEXDUMP_ERROR(p_data, len)
  171. #define NRF_LOG_HEXDUMP_WARNING(p_data, len) NRF_LOG_INTERNAL_HEXDUMP_WARNING(p_data, len)
  172. #define NRF_LOG_HEXDUMP_INFO(p_data, len) NRF_LOG_INTERNAL_HEXDUMP_INFO(p_data, len)
  173. #define NRF_LOG_HEXDUMP_DEBUG(p_data, len) NRF_LOG_INTERNAL_HEXDUMP_DEBUG(p_data, len)
  174. /** @def NRF_LOG_HEXDUMP_INST_ERROR
  175. * @brief Macro for logging raw bytes for a specific module instance.
  176. * @details This macro is compiled only if @ref NRF_LOG_LEVEL includes error logs.
  177. *
  178. * @param p_inst Pointer to the instance with logging support.
  179. * @param p_data Pointer to data.
  180. * @param len Data length in bytes.
  181. */
  182. /** @def NRF_LOG_HEXDUMP_INST_WARNING
  183. * @brief Macro for logging raw bytes for a specific module instance.
  184. * @details This macro is compiled only if @ref NRF_LOG_LEVEL includes error logs.
  185. *
  186. * @param p_inst Pointer to the instance with logging support.
  187. * @param p_data Pointer to data.
  188. * @param len Data length in bytes.
  189. */
  190. /** @def NRF_LOG_HEXDUMP_INST_INFO
  191. * @brief Macro for logging raw bytes for a specific module instance.
  192. * @details This macro is compiled only if @ref NRF_LOG_LEVEL includes error logs.
  193. *
  194. * @param p_inst Pointer to the instance with logging support.
  195. * @param p_data Pointer to data.
  196. * @param len Data length in bytes.
  197. */
  198. /** @def NRF_LOG_HEXDUMP_INST_DEBUG
  199. * @brief Macro for logging raw bytes for a specific module instance.
  200. * @details This macro is compiled only if @ref NRF_LOG_LEVEL includes error logs.
  201. *
  202. * @param p_inst Pointer to the instance with logging support.
  203. * @param p_data Pointer to data.
  204. * @param len Data length in bytes.
  205. */
  206. #define NRF_LOG_HEXDUMP_INST_ERROR(p_inst, p_data, len) NRF_LOG_INTERNAL_HEXDUMP_INST_ERROR(p_inst, p_data, len)
  207. #define NRF_LOG_HEXDUMP_INST_WARNING(p_inst, p_data, len) NRF_LOG_INTERNAL_HEXDUMP_INST_WARNING(p_inst, p_data, len)
  208. #define NRF_LOG_HEXDUMP_INST_INFO(p_inst, p_data, len) NRF_LOG_INTERNAL_HEXDUMP_INST_INFO(p_inst, p_data, len)
  209. #define NRF_LOG_HEXDUMP_INST_DEBUG(p_inst, p_data, len) NRF_LOG_INTERNAL_HEXDUMP_INST_DEBUG(p_inst, p_data, len)
  210. /**
  211. * @brief Macro for logging hexdump without any prefix or timestamp.
  212. */
  213. #define NRF_LOG_RAW_HEXDUMP_INFO(p_data, len) NRF_LOG_INTERNAL_RAW_HEXDUMP_INFO(p_data, len)
  214. /**
  215. * @brief Macro for copying a string to internal logger buffer if logs are deferred.
  216. *
  217. * @param _str String.
  218. */
  219. #define NRF_LOG_PUSH(_str) NRF_LOG_INTERNAL_LOG_PUSH(_str)
  220. /**
  221. * @brief Function for copying a string to the internal logger buffer if logs are deferred.
  222. *
  223. * Use this function to store a string that is volatile (for example allocated
  224. * on stack) or that may change before the deferred logs are processed. Such string is copied
  225. * into the internal logger buffer (see @ref NRF_LOG_STR_PUSH_BUFFER_SIZE).
  226. *
  227. * @note String storing is not reliable. It means that string is copied to the buffer but there is
  228. * no indication when it was used and could be freed. String may be overwritten by another
  229. * @ref nrf_log_push call before being processed. For reliable data dumping use
  230. * hexdump macros (e.g. @ref NRF_LOG_HEXDUMP_INFO).
  231. *
  232. * @note If the logs are not deferred, then this function returns the input parameter.
  233. *
  234. * @param p_str Pointer to the user string.
  235. *
  236. * @return Address to the location where the string is stored in the internal logger buffer.
  237. */
  238. char const * nrf_log_push(char * const p_str);
  239. /**
  240. * @brief Macro to be used in a formatted string to a pass float number to the log.
  241. *
  242. * Use this macro in a formatted string instead of the %f specifier together with
  243. * @ref NRF_LOG_FLOAT macro.
  244. * Example: NRF_LOG_INFO("My float number" NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(f)))
  245. */
  246. #define NRF_LOG_FLOAT_MARKER "%s%d.%02d"
  247. /**
  248. * @brief Macro for dissecting a float number into two numbers (integer and residuum).
  249. */
  250. #define NRF_LOG_FLOAT(val) (uint32_t)(((val) < 0 && (val) > -1.0) ? "-" : ""), \
  251. (int32_t)(val), \
  252. (int32_t)((((val) > 0) ? (val) - (int32_t)(val) \
  253. : (int32_t)(val) - (val))*100)
  254. /**
  255. * @brief Macro for registering an independent module.
  256. *
  257. * Registration creates set of dynamic (RAM) and constant variables associated with the module.
  258. */
  259. #define NRF_LOG_MODULE_REGISTER() NRF_LOG_INTERNAL_MODULE_REGISTER()
  260. #ifdef __cplusplus
  261. }
  262. #endif
  263. #endif // NRF_LOG_H_
  264. /** @} */