nrf_log.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. #ifndef NRF_LOG_H_
  2. #define NRF_LOG_H_
  3. #ifndef DOXYGEN
  4. #include <stdint.h>
  5. #include <stdarg.h>
  6. #include <app_util.h>
  7. #ifndef NRF_LOG_USES_RTT
  8. #define NRF_LOG_USES_RTT 0
  9. #endif
  10. #ifndef NRF_LOG_USES_UART
  11. #define NRF_LOG_USES_UART 0
  12. #endif
  13. #ifndef NRF_LOG_USES_RAW_UART
  14. #define NRF_LOG_USES_RAW_UART 0
  15. #endif
  16. #ifndef NRF_LOG_USES_COLORS
  17. #define NRF_LOG_USES_COLORS 1
  18. #endif
  19. #if NRF_LOG_USES_COLORS == 1
  20. #define NRF_LOG_COLOR_DEFAULT "\x1B[0m"
  21. #define NRF_LOG_COLOR_BLACK "\x1B[1;30m"
  22. #define NRF_LOG_COLOR_RED "\x1B[1;31m"
  23. #define NRF_LOG_COLOR_GREEN "\x1B[1;32m"
  24. #define NRF_LOG_COLOR_YELLOW "\x1B[1;33m"
  25. #define NRF_LOG_COLOR_BLUE "\x1B[1;34m"
  26. #define NRF_LOG_COLOR_MAGENTA "\x1B[1;35m"
  27. #define NRF_LOG_COLOR_CYAN "\x1B[1;36m"
  28. #define NRF_LOG_COLOR_WHITE "\x1B[1;37m"
  29. #else
  30. #define NRF_LOG_COLOR_DEFAULT
  31. #define NRF_LOG_COLOR_BLACK
  32. #define NRF_LOG_COLOR_RED
  33. #define NRF_LOG_COLOR_GREEN
  34. #define NRF_LOG_COLOR_YELLOW
  35. #define NRF_LOG_COLOR_BLUE
  36. #define NRF_LOG_COLOR_MAGENTA
  37. #define NRF_LOG_COLOR_CYAN
  38. #define NRF_LOG_COLOR_WHITE
  39. #endif
  40. #if defined(NRF_LOG_USES_RTT) && NRF_LOG_USES_RTT == 1
  41. #define LOG_TERMINAL_NORMAL (0)
  42. #define LOG_TERMINAL_ERROR (1)
  43. #define LOG_TERMINAL_INPUT (0)
  44. /**@brief Function for initializing the SEGGER RTT logger.
  45. *
  46. * @details See <a href="https://www.segger.com/jlink-rtt.html" target="_blank">segger.com</a>
  47. * for information about SEGGER Real Time Transfer (RTT).
  48. *
  49. * This function is available only when NRF_LOG_USES_RTT is defined as 1.
  50. *
  51. * @note Do not call this function directly. Use the macro @ref NRF_LOG_INIT instead.
  52. *
  53. * @retval NRF_SUCCESS If initialization was successful.
  54. * @retval NRF_ERROR Otherwise.
  55. */
  56. uint32_t log_rtt_init(void);
  57. /**@brief Function for writing a printf string using RTT.
  58. *
  59. * @details The printf implementation in SEGGER's RTT is more efficient than
  60. * the standard implementation. However, printf requires more processor time
  61. * than other logging functions. Therefore, applications that require logging
  62. * but need it to interfere as little as possible with the execution, should
  63. * avoid using printf.
  64. *
  65. * This function is available only when NRF_LOG_USES_RTT is defined as 1.
  66. *
  67. * @note Do not call this function directly. Use one of the following macros instead:
  68. * - @ref NRF_LOG_PRINTF
  69. * - @ref NRF_LOG_PRINTF_DEBUG
  70. * - @ref NRF_LOG_PRINTF_ERROR
  71. *
  72. * @param terminal_index Segger RTT terminal index to use as output.
  73. * @param format_msg Printf format string.
  74. */
  75. void log_rtt_printf(int terminal_index, char * format_msg, ...);
  76. /**@brief Function for writing a string using RTT.
  77. *
  78. * @details The string to write must be null-terminated, but the null termination will not be stored
  79. * in the ring buffer.
  80. * The impact of running this function should be very low compared to writing to UART.
  81. *
  82. * This function is available only when NRF_LOG_USES_RTT is defined as 1.
  83. *
  84. * @note Do not call this function directly. Use one of the following macros instead:
  85. * - @ref NRF_LOG
  86. * - @ref NRF_LOG_DEBUG
  87. * - @ref NRF_LOG_ERROR
  88. *
  89. * @param terminal_index Segger RTT terminal index to use as output.
  90. * @param num_args Number of arguments.
  91. */
  92. void log_rtt_write_string(int terminal_index, int num_args, ...);
  93. /**@brief Function for writing an integer as HEX using RTT.
  94. *
  95. * The output data is formatted as, for example, 0x89ABCDEF.
  96. *
  97. * This function is available only when NRF_LOG_USES_RTT is defined as 1.
  98. *
  99. * @note Do not call this function directly. Use one of the following macros instead:
  100. * - @ref NRF_LOG_HEX
  101. * - @ref NRF_LOG_HEX_DEBUG
  102. * - @ref NRF_LOG_HEX_ERROR
  103. *
  104. * @param terminal_index Segger RTT terminal index to use as output.
  105. * @param value Integer value to be printed as HEX.
  106. */
  107. void log_rtt_write_hex(int terminal_index, uint32_t value);
  108. /**@brief Function for writing a single character as HEX using RTT.
  109. *
  110. * The output string is formatted as, for example, AA.
  111. *
  112. * This function is available only when NRF_LOG_USES_RTT is defined as 1.
  113. *
  114. * @note Do not call this function directly. Use one of the following macros instead:
  115. * - @ref NRF_LOG_HEX_CHAR
  116. * - @ref NRF_LOG_HEX_CHAR_DEBUG
  117. * - @ref NRF_LOG_HEX_CHAR_ERROR
  118. *
  119. * @param terminal_index Segger RTT terminal index to use as output.
  120. * @param value Character to print as HEX.
  121. */
  122. void log_rtt_write_hex_char(int terminal_index, uint8_t value);
  123. /**@brief Function for checking if data is available in the input buffer.
  124. *
  125. * This function is available only when NRF_LOG_USES_RTT is defined as 1.
  126. *
  127. * @note Do not call this function directly. Use @ref NRF_LOG_HAS_INPUT instead.
  128. *
  129. * @retval 1 If characters are available to read.
  130. * @retval 0 If no characters are available.
  131. */
  132. int log_rtt_has_input(void);
  133. /**@brief Function for reading one character from the input buffer.
  134. *
  135. * @param[out] p_char Pointer where to store the character.
  136. *
  137. * This function is available only when NRF_LOG_USES_RTT is defined as 1.
  138. *
  139. * @note Do not call this function directly. Use @ref NRF_LOG_READ_INPUT instead.
  140. *
  141. * @retval NRF_SUCCESS If the character was read out.
  142. * @retval NRF_ERROR_INVALID_DATA If no character could be read.
  143. */
  144. uint32_t log_rtt_read_input(char* p_char);
  145. #define NRF_LOG_INIT() log_rtt_init() /*!< Initialize the module. */
  146. #define NRF_LOG_PRINTF(...) log_rtt_printf(LOG_TERMINAL_NORMAL, ##__VA_ARGS__) /*!< Print a log message using printf. */
  147. #define NRF_LOG_PRINTF_DEBUG(...) log_rtt_printf(LOG_TERMINAL_NORMAL, ##__VA_ARGS__) /*!< If DEBUG is set, print a log message using printf. */
  148. #define NRF_LOG_PRINTF_ERROR(...) log_rtt_printf(LOG_TERMINAL_ERROR, ##__VA_ARGS__) /*!< Print a log message using printf to the error stream. */
  149. #define NRF_LOG(...) log_rtt_write_string(LOG_TERMINAL_NORMAL, NUM_VA_ARGS(__VA_ARGS__), ##__VA_ARGS__) /*!< Print a log message. The input string must be null-terminated. */
  150. #define NRF_LOG_DEBUG(...) log_rtt_write_string(LOG_TERMINAL_NORMAL, NUM_VA_ARGS(__VA_ARGS__), ##__VA_ARGS__) /*!< If DEBUG is set, print a log message. The input string must be null-terminated. */
  151. #define NRF_LOG_ERROR(...) log_rtt_write_string(LOG_TERMINAL_ERROR, NUM_VA_ARGS(__VA_ARGS__), ##__VA_ARGS__) /*!< Print a log message to the error stream. The input string must be null-terminated. */
  152. #define NRF_LOG_HEX(val) log_rtt_write_hex(LOG_TERMINAL_NORMAL, val) /*!< Log an integer as HEX value (example output: 0x89ABCDEF). */
  153. #define NRF_LOG_HEX_DEBUG(val) log_rtt_write_hex(LOG_TERMINAL_NORMAL, val) /*!< If DEBUG is set, log an integer as HEX value (example output: 0x89ABCDEF). */
  154. #define NRF_LOG_HEX_ERROR(val) log_rtt_write_hex(LOG_TERMINAL_ERROR, val) /*!< Log an integer as HEX value to the error stream (example output: 0x89ABCDEF). */
  155. #define NRF_LOG_HEX_CHAR(val) log_rtt_write_hex_char(LOG_TERMINAL_NORMAL, val) /*!< Log a character as HEX value (example output: AA). */
  156. #define NRF_LOG_HEX_CHAR_DEBUG(val) log_rtt_write_hex_char(LOG_TERMINAL_NORMAL, val) /*!< If DEBUG is set, log a character as HEX value (example output: AA). */
  157. #define NRF_LOG_HEX_CHAR_ERROR(val) log_rtt_write_hex_char(LOG_TERMINAL_ERROR, val) /*!< Log a character as HEX value to the error stream (example output: AA). */
  158. #define NRF_LOG_HAS_INPUT() log_rtt_has_input() /*!< Check if the input buffer has unconsumed characters. */
  159. #define NRF_LOG_READ_INPUT(p_char) log_rtt_read_input(p_char) /*!< Consume a character from the input buffer. */
  160. #if !defined(DEBUG) && !defined(DOXYGEN)
  161. #undef NRF_LOG_DEBUG
  162. #define NRF_LOG_DEBUG(...)
  163. #undef NRF_LOG_STR_DEBUG
  164. #define NRF_LOG_STR_DEBUG(...)
  165. #undef NRF_LOG_HEX_DEBUG
  166. #define NRF_LOG_HEX_DEBUG(...)
  167. #undef NRF_LOG_HEX_CHAR_DEBUG
  168. #define NRF_LOG_HEX_CHAR_DEBUG(...)
  169. #endif // !defined(DEBUG) && !defined(DOXYGEN)
  170. #elif defined(NRF_LOG_USES_UART) && NRF_LOG_USES_UART == 1
  171. /**@brief Function for initializing the UART logger.
  172. *
  173. * This function is available only when NRF_LOG_USES_UART is defined as 1.
  174. *
  175. * @note Do not call this function directly. Use the macro @ref NRF_LOG_INIT instead.
  176. *
  177. * @retval NRF_SUCCESS If initialization was successful.
  178. * @retval NRF_ERROR Otherwise.
  179. */
  180. uint32_t log_uart_init(void);
  181. /**@brief Function for logging a printf string to UART.
  182. *
  183. * @details Printf requires more processor time
  184. * than other logging functions. Therefore, applications that require logging
  185. * but need it to interfere as little as possible with the execution, should
  186. * avoid using printf.
  187. *
  188. * This function is available only when NRF_LOG_USES_UART is defined as 1.
  189. *
  190. * @note This function is non-blocking. If too much data is sent to the UART,
  191. * some characters might be skipped.
  192. *
  193. * @note Do not call this function directly. Use one of the following macros instead:
  194. * - @ref NRF_LOG_PRINTF
  195. * - @ref NRF_LOG_PRINTF_DEBUG
  196. * - @ref NRF_LOG_PRINTF_ERROR
  197. *
  198. * @param format_msg Printf format string.
  199. */
  200. void log_uart_printf(const char * format_msg, ...);
  201. /**@brief Function for logging a single character to UART.
  202. *
  203. * This function is available only when NRF_LOG_USES_UART is defined as 1.
  204. *
  205. * @param c Character.
  206. */
  207. void log_uart_write_char(const char c);
  208. /**@brief Function for logging null-terminated strings to UART.
  209. *
  210. * @details This function is more efficient than using printf.
  211. * The null termination will not be logged.
  212. *
  213. * This function is available only when NRF_LOG_USES_UART is defined as 1.
  214. *
  215. * @note Do not call this function directly. Use one of the following macros instead:
  216. * - @ref NRF_LOG
  217. * - @ref NRF_LOG_DEBUG
  218. * - @ref NRF_LOG_ERROR
  219. *
  220. * @param num_args Number of arguments.
  221. */
  222. void log_uart_write_string_many(int num_args, ...);
  223. /**@brief Function for logging a null-terminated string to UART.
  224. *
  225. * @details This function is more efficient than using printf.
  226. * The null termination will not be logged.
  227. *
  228. * This function is available only when NRF_LOG_USES_UART is defined as 1.
  229. *
  230. * @note Do not call this function directly. Use one of the following macros instead:
  231. * - @ref NRF_LOG
  232. * - @ref NRF_LOG_DEBUG
  233. * - @ref NRF_LOG_ERROR
  234. *
  235. * @param msg Null-terminated string.
  236. */
  237. void log_uart_write_string(const char* msg);
  238. /**@brief Function for logging an integer value as HEX to UART.
  239. *
  240. * @details The output data is formatted as, for example, 0x89ABCDEF.
  241. * This function is more efficient than printf.
  242. *
  243. * This function is available only when NRF_LOG_USES_UART is defined as 1.
  244. *
  245. * @note This function is non-blocking. If too much data is sent to the UART,
  246. * some characters might be skipped.
  247. *
  248. * @note Do not call this function directly. Use one of the following macros instead:
  249. * - @ref NRF_LOG_HEX
  250. * - @ref NRF_LOG_HEX_DEBUG
  251. * - @ref NRF_LOG_HEX_ERROR
  252. *
  253. * @param value Integer value to be printed as HEX.
  254. */
  255. void log_uart_write_hex(uint32_t value);
  256. /**@brief Function for logging a single character as HEX to UART.
  257. *
  258. * @details The output string is formatted as, for example, AA.
  259. *
  260. * This function is available only when NRF_LOG_USES_UART is defined as 1.
  261. *
  262. * @note This function is non-blocking. If too much data is sent to the UART,
  263. * some characters might be skipped.
  264. *
  265. * @note Do not call this function directly. Use one of the following macros instead:
  266. * - @ref NRF_LOG_HEX_CHAR
  267. * - @ref NRF_LOG_HEX_CHAR_DEBUG
  268. * - @ref NRF_LOG_HEX_CHAR_ERROR
  269. *
  270. * @param c Character.
  271. */
  272. void log_uart_write_hex_char(uint8_t c);
  273. /**@brief Function for checking if data is available in the input buffer.
  274. *
  275. * This function is available only when NRF_LOG_USES_UART is defined as 1.
  276. *
  277. * @note Do not call this function directly. Use @ref NRF_LOG_HAS_INPUT instead.
  278. *
  279. * @retval 1 If characters are available to read.
  280. * @retval 0 If no characters are available.
  281. */
  282. int log_uart_has_input(void);
  283. /**@brief Function for reading one character from the input buffer.
  284. *
  285. * @param[out] p_char Pointer where to store the character.
  286. *
  287. * This function is available only when NRF_LOG_USES_UART is defined as 1.
  288. *
  289. * @note Do not call this function directly. Use NRF_LOG_READ_INPUT instead.
  290. *
  291. * @retval NRF_SUCCESS If the character was read out.
  292. * @retval NRF_ERROR_INVALID_DATA If no character could be read.
  293. */
  294. uint32_t log_uart_read_input(char* p_char);
  295. void log_uart_write_hexs(const char* msg, uint8_t* data, uint32_t length);
  296. #define NRF_LOG_INIT() log_uart_init() /*!< Initialize the module. */
  297. #define NRF_LOG_PRINTF(...) log_uart_printf(__VA_ARGS__) /*!< Print a log message using printf. */
  298. #define NRF_LOG_PRINTF_DEBUG(...) log_uart_printf(__VA_ARGS__) /*!< If DEBUG is set, print a log message using printf. */
  299. #define NRF_LOG_PRINTF_ERROR(...) log_uart_printf(__VA_ARGS__) /*!< Print a log message using printf to the error stream. */
  300. #define NRF_LOG(...) log_uart_write_string_many(NUM_VA_ARGS(__VA_ARGS__), ##__VA_ARGS__) /*!< Print a log message. The input string must be null-terminated. */
  301. #define NRF_LOG_DEBUG(...) log_uart_write_string_many(NUM_VA_ARGS(__VA_ARGS__), ##__VA_ARGS__) /*!< If DEBUG is set, print a log message. The input string must be null-terminated. */
  302. #define NRF_LOG_ERROR(...) log_uart_write_string_many(NUM_VA_ARGS(__VA_ARGS__), ##__VA_ARGS__) /*!< Print a log message to the error stream. The input string must be null-terminated. */
  303. #define NRF_LOG_HEX(val) log_uart_write_hex(val) /*!< Log an integer as HEX value (example output: 0x89ABCDEF). */
  304. #define NRF_LOG_HEX_DEBUG(val) log_uart_write_hex(val) /*!< If DEBUG is set, log an integer as HEX value (example output: 0x89ABCDEF). */
  305. #define NRF_LOG_HEX_ERROR(val) log_uart_write_hex(val) /*!< Log an integer as HEX value to the error stream (example output: 0x89ABCDEF). */
  306. #define NRF_LOG_HEX_CHAR(val) log_uart_write_hex_char(val) /*!< Log a character as HEX value (example output: AA). */
  307. #define NRF_LOG_HEX_CHAR_DEBUG(val) log_uart_write_hex_char(val) /*!< If DEBUG is set, log a character as HEX value (example output: AA). */
  308. #define NRF_LOG_HEX_CHAR_ERROR(val) log_uart_write_hex_char(val) /*!< Log a character as HEX value to the error stream (example output: AA). */
  309. #define NRF_LOG_HAS_INPUT() log_uart_has_input() /*!< Check if the input buffer has unconsumed characters. */
  310. #define NRF_LOG_READ_INPUT(p_char) log_uart_read_input(p_char) /*!< Consume a character from the input buffer. */
  311. #define NRF_LOG_HEXS(str, var, len) log_uart_write_hexs(str, var, len)
  312. #if !defined(DEBUG) && !defined(DOXYGEN)
  313. #undef NRF_LOG_DEBUG
  314. #define NRF_LOG_DEBUG(...)
  315. #undef NRF_LOG_PRINTF_DEBUG
  316. #define NRF_LOG_PRINTF_DEBUG(...)
  317. #undef NRF_LOG_STR_DEBUG
  318. #define NRF_LOG_STR_DEBUG(...)
  319. #undef NRF_LOG_HEX_DEBUG
  320. #define NRF_LOG_HEX_DEBUG(...)
  321. #undef NRF_LOG_HEX_CHAR_DEBUG
  322. #define NRF_LOG_HEX_CHAR_DEBUG(...)
  323. #endif // !defined(DEBUG) && !defined(DOXYGEN)
  324. #elif defined(NRF_LOG_USES_RAW_UART) && NRF_LOG_USES_RAW_UART == 1
  325. /**@brief Function for initializing the raw UART logger.
  326. *
  327. * This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
  328. *
  329. * @note Do not call this function directly. Use the macro @ref NRF_LOG_INIT instead.
  330. *
  331. * @retval NRF_SUCCESS If initialization was successful.
  332. * @retval NRF_ERROR Otherwise.
  333. */
  334. uint32_t log_raw_uart_init(void);
  335. /**@brief Function for logging a printf string to raw UART.
  336. *
  337. * @details Printf requires more processor time
  338. * than other logging functions. Therefore, applications that require logging
  339. * but need it to interfere as little as possible with the execution, should
  340. * avoid using printf.
  341. *
  342. * This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
  343. *
  344. * @note This function is non-blocking. If too much data is sent to the UART,
  345. * some characters might be skipped.
  346. *
  347. * @note Do not call this function directly. Use one of the following macros instead:
  348. * - @ref NRF_LOG_PRINTF
  349. * - @ref NRF_LOG_PRINTF_DEBUG
  350. * - @ref NRF_LOG_PRINTF_ERROR
  351. *
  352. * @param format_msg Printf format string.
  353. */
  354. void log_raw_uart_printf(const char * format_msg, ...);
  355. /**@brief Function for logging a single character to raw UART.
  356. *
  357. * This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
  358. *
  359. * @param c Character.
  360. */
  361. void log_raw_uart_write_char(const char c);
  362. /**@brief Function for logging null-terminated strings to raw UART.
  363. *
  364. * @details This function is more efficient than using printf.
  365. * The null termination will not be logged.
  366. *
  367. * This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
  368. *
  369. * @note Do not call this function directly. Use one of the following macros instead:
  370. * - @ref NRF_LOG
  371. * - @ref NRF_LOG_DEBUG
  372. * - @ref NRF_LOG_ERROR
  373. *
  374. * @param num_args Number of arguments.
  375. */
  376. void log_raw_uart_write_string_many(int num_args, ...);
  377. /**@brief Function for logging a null-terminated string to raw UART.
  378. *
  379. * @details This function is more efficient than using printf.
  380. * The null termination will not be logged.
  381. *
  382. * This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
  383. *
  384. * @note Do not call this function directly. Use one of the following macros instead:
  385. * - @ref NRF_LOG
  386. * - @ref NRF_LOG_DEBUG
  387. * - @ref NRF_LOG_ERROR
  388. *
  389. * @param str Null-terminated string.
  390. */
  391. void log_raw_uart_write_string(const char * str);
  392. /**@brief Function for logging an integer value as HEX to raw UART.
  393. *
  394. * @details The output data is formatted as, for example, 0x89ABCDEF.
  395. * This function is more efficient than printf.
  396. *
  397. * This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
  398. *
  399. * @note This function is non-blocking. If too much data is sent to the UART,
  400. * some characters might be skipped.
  401. *
  402. * @note Do not call this function directly. Use one of the following macros instead:
  403. * - @ref NRF_LOG_HEX
  404. * - @ref NRF_LOG_HEX_DEBUG
  405. * - @ref NRF_LOG_HEX_ERROR
  406. *
  407. * @param value Integer value to be printed as HEX.
  408. */
  409. void log_raw_uart_write_hex(uint32_t value);
  410. /**@brief Function for logging a single character as HEX to raw UART.
  411. *
  412. * @details The output string is formatted as, for example, AA.
  413. *
  414. * This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
  415. *
  416. * @note This function is non-blocking. If too much data is sent to the UART,
  417. * some characters might be skipped.
  418. *
  419. * @note Do not call this function directly. Use one of the following macros instead:
  420. * - @ref NRF_LOG_HEX_CHAR
  421. * - @ref NRF_LOG_HEX_CHAR_DEBUG
  422. * - @ref NRF_LOG_HEX_CHAR_ERROR
  423. *
  424. * @param c Character.
  425. */
  426. void log_raw_uart_write_hex_char(uint8_t c);
  427. /**@brief Function for checking if data is available in the input buffer.
  428. *
  429. * This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
  430. *
  431. * @note Do not call this function directly. Use @ref NRF_LOG_HAS_INPUT instead.
  432. *
  433. * @retval 1 If characters are available to read.
  434. * @retval 0 If no characters are available.
  435. */
  436. int log_raw_uart_has_input(void);
  437. /**@brief Function for reading one character from the input buffer.
  438. *
  439. * @param[out] p_char Pointer where to store the character.
  440. *
  441. * This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
  442. *
  443. * @note Do not call this function directly. Use NRF_LOG_READ_INPUT instead.
  444. *
  445. * @retval NRF_SUCCESS If the character was read out.
  446. * @retval NRF_ERROR_INVALID_DATA If no character could be read.
  447. */
  448. uint32_t log_raw_uart_read_input(char* p_char);
  449. #define NRF_LOG_INIT() log_raw_uart_init() /*!< nitialize the module. */
  450. #define NRF_LOG_PRINTF(...) log_raw_uart_printf(__VA_ARGS__) /*!< Print a log message using printf. */
  451. #define NRF_LOG_PRINTF_DEBUG(...) log_raw_uart_printf(__VA_ARGS__) /*!< If DEBUG is set, print a log message using printf. */
  452. #define NRF_LOG_PRINTF_ERROR(...) log_raw_uart_printf(__VA_ARGS__) /*!< Print a log message using printf to the error stream. */
  453. #define NRF_LOG(...) log_raw_uart_write_string_many(NUM_VA_ARGS(__VA_ARGS__), ##__VA_ARGS__) /*!< Print a log message. The input string must be null-terminated. */
  454. #define NRF_LOG_DEBUG(...) log_raw_uart_write_string_many(NUM_VA_ARGS(__VA_ARGS__), ##__VA_ARGS__) /*!< If DEBUG is set, print a log message. The input string must be null-terminated. */
  455. #define NRF_LOG_ERROR(...) log_raw_uart_write_string_many(NUM_VA_ARGS(__VA_ARGS__), ##__VA_ARGS__) /*!< Print a log message to the error stream. The input string must be null-terminated. */
  456. #define NRF_LOG_HEX(val) log_raw_uart_write_hex(val) /*!< Log an integer as HEX value (example output: 0x89ABCDEF). */
  457. #define NRF_LOG_HEX_DEBUG(val) log_raw_uart_write_hex(val) /*!< If DEBUG is set, log an integer as HEX value (example output: 0x89ABCDEF). */
  458. #define NRF_LOG_HEX_ERROR(val) log_raw_uart_write_hex(val) /*!< Log an integer as HEX value to the error stream (example output: 0x89ABCDEF). */
  459. #define NRF_LOG_HEX_CHAR(val) log_raw_uart_write_hex_char(val) /*!< Log a character as HEX value (example output: AA). */
  460. #define NRF_LOG_HEX_CHAR_DEBUG(val) log_raw_uart_write_hex_char(val) /*!< If DEBUG is set, log a character as HEX value (example output: AA). */
  461. #define NRF_LOG_HEX_CHAR_ERROR(val) log_raw_uart_write_hex_char(val) /*!< Log a character as HEX value to the error stream (example output: AA). */
  462. #define NRF_LOG_HAS_INPUT() log_raw_uart_has_input() /*!< Check if the input buffer has unconsumed characters. */
  463. #define NRF_LOG_READ_INPUT(p_char) log_raw_uart_read_input(p_char) /*!< Consume a character from the input buffer. */
  464. #if !defined(DEBUG) && !defined(DOXYGEN)
  465. #undef NRF_LOG_DEBUG
  466. #define NRF_LOG_DEBUG(...)
  467. #undef NRF_LOG_PRINTF_DEBUG
  468. #define NRF_LOG_PRINTF_DEBUG(...)
  469. #undef NRF_LOG_STR_DEBUG
  470. #define NRF_LOG_STR_DEBUG(...)
  471. #undef NRF_LOG_HEX_DEBUG
  472. #define NRF_LOG_HEX_DEBUG(...)
  473. #undef NRF_LOG_HEX_CHAR_DEBUG
  474. #define NRF_LOG_HEX_CHAR_DEBUG(...)
  475. #endif // !defined(DEBUG) && !defined(DOXYGEN)
  476. #else
  477. #include "nrf_error.h"
  478. #include "nordic_common.h"
  479. // Empty definitions
  480. #define NRF_LOG_INIT() NRF_SUCCESS
  481. #define NRF_LOG(...)
  482. #define NRF_LOG_DEBUG(...)
  483. #define NRF_LOG_ERROR(...)
  484. #define NRF_LOG_PRINTF(...)
  485. #define NRF_LOG_PRINTF_DEBUG(...)
  486. #define NRF_LOG_PRINTF_ERROR(...)
  487. #define NRF_LOG_HEX(val)
  488. #define NRF_LOG_HEX_DEBUG(val)
  489. #define NRF_LOG_HEX_ERROR(val)
  490. #define NRF_LOG_HEX_CHAR(val)
  491. #define NRF_LOG_HEX_CHAR_DEBUG(val)
  492. #define NRF_LOG_HEX_CHAR_ERROR(val)
  493. #define NRF_LOG_HAS_INPUT() 0
  494. #define NRF_LOG_READ_INPUT(ignore) NRF_SUCCESS
  495. #endif
  496. /**@brief Function for writing HEX values.
  497. *
  498. * @note This function not thread-safe. It is written for convenience.
  499. * If you log from different application contexts, you might get different results.
  500. *
  501. * @retval NULL By default.
  502. */
  503. const char* log_hex(uint32_t value);
  504. /**@brief Function for writing HEX characters.
  505. *
  506. * @note This function not thread-safe. It is written for convenience.
  507. * If you log from different application contexts, you might get different results.
  508. *
  509. * @retval NULL By default.
  510. */
  511. const char* log_hex_char(const char value);
  512. #else // DOXYGEN
  513. /** @defgroup nrf_log UART/RTT logging
  514. * @{
  515. * @ingroup app_common
  516. *
  517. * @brief Library to output logging information over SEGGER's Real Time Transfer
  518. * (RTT), UART, or raw UART.
  519. *
  520. * This library provides macros that call the respective functions depending on
  521. * which protocol is used. Define LOG_USES_RTT=1 to enable logging over RTT,
  522. * NRF_LOG_USES_UART=1 to enable logging over UART, or NRF_LOG_USES_RAW_UART=1
  523. * to enable logging over raw UART. One of these defines must be set for any of
  524. * the macros to have effect. If you choose to not output information, all
  525. * logging macros can be left in the code without any cost; they will just be
  526. * ignored.
  527. */
  528. /**@brief Macro for initializing the logger.
  529. *
  530. * @retval NRF_SUCCESS If initialization was successful.
  531. * @retval NRF_ERROR Otherwise.
  532. */
  533. uint32_t NRF_LOG_INIT(void);
  534. /**@brief Macro for logging null-terminated strings.
  535. *
  536. * @details This function is more efficient than using printf.
  537. * The null termination will not be logged.
  538. *
  539. * @param msg Null-terminated string.
  540. */
  541. void NRF_LOG(const char* msg);
  542. /**@brief Macro for logging a printf string.
  543. *
  544. * @details Printf requires more processor time
  545. * than other logging functions. Therefore, applications that require logging
  546. * but need it to interfere as little as possible with the execution, should
  547. * avoid using printf.
  548. *
  549. * @note When NRF_LOG_USES_UART is set to 1, this macro is non-blocking.
  550. * If too much data is sent, some characters might be skipped.
  551. *
  552. * @param format_msg Printf format string.
  553. * @param ... Additional arguments replacing format specifiers in format_msg.
  554. */
  555. void NRF_LOG_PRINTF(const char * format_msg, ...);
  556. /**@brief Macro for logging an integer value as HEX.
  557. *
  558. * @details The output data is formatted as, for example, 0x89ABCDEF.
  559. * This function is more efficient than printf.
  560. *
  561. * @note When NRF_LOG_USES_UART is set to 1, this macro is non-blocking.
  562. * If too much data is sent, some characters might be skipped.
  563. *
  564. * @param value Integer value to be printed as HEX.
  565. */
  566. void NRF_LOG_HEX(uint32_t value);
  567. /**@brief Macro for logging a single character as HEX.
  568. *
  569. * @details The output string is formatted as, for example, AA.
  570. *
  571. * @note When NRF_LOG_USES_UART is set to 1, this macro is non-blocking.
  572. * If too much data is sent, some characters might be skipped.
  573. *
  574. * @param c Character.
  575. */
  576. void NRF_LOG_HEX_CHAR(uint8_t c);
  577. /**@brief Macro for checking if data is available in the input buffer.
  578. *
  579. * @note When NRF_LOG_USES_UART is set to 1, this macro is non-blocking.
  580. * If too much data is sent, some characters might be skipped.
  581. *
  582. * @retval 1 If characters are available to read.
  583. * @retval 0 If no characters are available.
  584. */
  585. int NRF_LOG_HAS_INPUT(void);
  586. /**@brief Macro for reading one character from the input buffer.
  587. *
  588. * @param[out] p_char Pointer where to store the character.
  589. *
  590. * @retval NRF_SUCCESS If the character was read out.
  591. * @retval NRF_ERROR_INVALID_DATA If no character could be read.
  592. */
  593. uint32_t NRF_LOG_READ_INPUT(char* p_char);
  594. void log_spi_printf(const char * format_msg, ...);
  595. /** @} */
  596. #endif // DOXYGEN
  597. #endif // NRF_LOG_H_