nrf_log.h~RFfbebebe.TMP 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. #define NRF_LOG_INIT() log_uart_init() /*!< Initialize the module. */
  296. #define NRF_LOG_PRINTF(...) log_uart_printf(__VA_ARGS__) /*!< Print a log message using printf. */
  297. #define NRF_LOG_PRINTF_DEBUG(...) log_uart_printf(__VA_ARGS__) /*!< If DEBUG is set, print a log message using printf. */
  298. #define NRF_LOG_PRINTF_ERROR(...) log_uart_printf(__VA_ARGS__) /*!< Print a log message using printf to the error stream. */
  299. #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. */
  300. #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. */
  301. #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. */
  302. #define NRF_LOG_HEX(val) log_uart_write_hex(val) /*!< Log an integer as HEX value (example output: 0x89ABCDEF). */
  303. #define NRF_LOG_HEX_DEBUG(val) log_uart_write_hex(val) /*!< If DEBUG is set, log an integer as HEX value (example output: 0x89ABCDEF). */
  304. #define NRF_LOG_HEX_ERROR(val) log_uart_write_hex(val) /*!< Log an integer as HEX value to the error stream (example output: 0x89ABCDEF). */
  305. #define NRF_LOG_HEX_CHAR(val) log_uart_write_hex_char(val) /*!< Log a character as HEX value (example output: AA). */
  306. #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). */
  307. #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). */
  308. #define NRF_LOG_HAS_INPUT() log_uart_has_input() /*!< Check if the input buffer has unconsumed characters. */
  309. #define NRF_LOG_READ_INPUT(p_char) log_uart_read_input(p_char) /*!< Consume a character from the input buffer. */
  310. #if !defined(DEBUG) && !defined(DOXYGEN)
  311. #undef NRF_LOG_DEBUG
  312. #define NRF_LOG_DEBUG(...)
  313. #undef NRF_LOG_PRINTF_DEBUG
  314. #define NRF_LOG_PRINTF_DEBUG(...)
  315. #undef NRF_LOG_STR_DEBUG
  316. #define NRF_LOG_STR_DEBUG(...)
  317. #undef NRF_LOG_HEX_DEBUG
  318. #define NRF_LOG_HEX_DEBUG(...)
  319. #undef NRF_LOG_HEX_CHAR_DEBUG
  320. #define NRF_LOG_HEX_CHAR_DEBUG(...)
  321. #endif // !defined(DEBUG) && !defined(DOXYGEN)
  322. #elif defined(NRF_LOG_USES_RAW_UART) && NRF_LOG_USES_RAW_UART == 1
  323. /**@brief Function for initializing the raw UART logger.
  324. *
  325. * This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
  326. *
  327. * @note Do not call this function directly. Use the macro @ref NRF_LOG_INIT instead.
  328. *
  329. * @retval NRF_SUCCESS If initialization was successful.
  330. * @retval NRF_ERROR Otherwise.
  331. */
  332. uint32_t log_raw_uart_init(void);
  333. /**@brief Function for logging a printf string to raw UART.
  334. *
  335. * @details Printf requires more processor time
  336. * than other logging functions. Therefore, applications that require logging
  337. * but need it to interfere as little as possible with the execution, should
  338. * avoid using printf.
  339. *
  340. * This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
  341. *
  342. * @note This function is non-blocking. If too much data is sent to the UART,
  343. * some characters might be skipped.
  344. *
  345. * @note Do not call this function directly. Use one of the following macros instead:
  346. * - @ref NRF_LOG_PRINTF
  347. * - @ref NRF_LOG_PRINTF_DEBUG
  348. * - @ref NRF_LOG_PRINTF_ERROR
  349. *
  350. * @param format_msg Printf format string.
  351. */
  352. void log_raw_uart_printf(const char * format_msg, ...);
  353. /**@brief Function for logging a single character to raw UART.
  354. *
  355. * This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
  356. *
  357. * @param c Character.
  358. */
  359. void log_raw_uart_write_char(const char c);
  360. /**@brief Function for logging null-terminated strings to raw UART.
  361. *
  362. * @details This function is more efficient than using printf.
  363. * The null termination will not be logged.
  364. *
  365. * This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
  366. *
  367. * @note Do not call this function directly. Use one of the following macros instead:
  368. * - @ref NRF_LOG
  369. * - @ref NRF_LOG_DEBUG
  370. * - @ref NRF_LOG_ERROR
  371. *
  372. * @param num_args Number of arguments.
  373. */
  374. void log_raw_uart_write_string_many(int num_args, ...);
  375. /**@brief Function for logging a null-terminated string to raw UART.
  376. *
  377. * @details This function is more efficient than using printf.
  378. * The null termination will not be logged.
  379. *
  380. * This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
  381. *
  382. * @note Do not call this function directly. Use one of the following macros instead:
  383. * - @ref NRF_LOG
  384. * - @ref NRF_LOG_DEBUG
  385. * - @ref NRF_LOG_ERROR
  386. *
  387. * @param str Null-terminated string.
  388. */
  389. void log_raw_uart_write_string(const char * str);
  390. /**@brief Function for logging an integer value as HEX to raw UART.
  391. *
  392. * @details The output data is formatted as, for example, 0x89ABCDEF.
  393. * This function is more efficient than printf.
  394. *
  395. * This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
  396. *
  397. * @note This function is non-blocking. If too much data is sent to the UART,
  398. * some characters might be skipped.
  399. *
  400. * @note Do not call this function directly. Use one of the following macros instead:
  401. * - @ref NRF_LOG_HEX
  402. * - @ref NRF_LOG_HEX_DEBUG
  403. * - @ref NRF_LOG_HEX_ERROR
  404. *
  405. * @param value Integer value to be printed as HEX.
  406. */
  407. void log_raw_uart_write_hex(uint32_t value);
  408. /**@brief Function for logging a single character as HEX to raw UART.
  409. *
  410. * @details The output string is formatted as, for example, AA.
  411. *
  412. * This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
  413. *
  414. * @note This function is non-blocking. If too much data is sent to the UART,
  415. * some characters might be skipped.
  416. *
  417. * @note Do not call this function directly. Use one of the following macros instead:
  418. * - @ref NRF_LOG_HEX_CHAR
  419. * - @ref NRF_LOG_HEX_CHAR_DEBUG
  420. * - @ref NRF_LOG_HEX_CHAR_ERROR
  421. *
  422. * @param c Character.
  423. */
  424. void log_raw_uart_write_hex_char(uint8_t c);
  425. /**@brief Function for checking if data is available in the input buffer.
  426. *
  427. * This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
  428. *
  429. * @note Do not call this function directly. Use @ref NRF_LOG_HAS_INPUT instead.
  430. *
  431. * @retval 1 If characters are available to read.
  432. * @retval 0 If no characters are available.
  433. */
  434. int log_raw_uart_has_input(void);
  435. /**@brief Function for reading one character from the input buffer.
  436. *
  437. * @param[out] p_char Pointer where to store the character.
  438. *
  439. * This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
  440. *
  441. * @note Do not call this function directly. Use NRF_LOG_READ_INPUT instead.
  442. *
  443. * @retval NRF_SUCCESS If the character was read out.
  444. * @retval NRF_ERROR_INVALID_DATA If no character could be read.
  445. */
  446. uint32_t log_raw_uart_read_input(char* p_char);
  447. #define NRF_LOG_INIT() log_raw_uart_init() /*!< nitialize the module. */
  448. #define NRF_LOG_PRINTF(...) log_raw_uart_printf(__VA_ARGS__) /*!< Print a log message using printf. */
  449. #define NRF_LOG_PRINTF_DEBUG(...) log_raw_uart_printf(__VA_ARGS__) /*!< If DEBUG is set, print a log message using printf. */
  450. #define NRF_LOG_PRINTF_ERROR(...) log_raw_uart_printf(__VA_ARGS__) /*!< Print a log message using printf to the error stream. */
  451. #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. */
  452. #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. */
  453. #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. */
  454. #define NRF_LOG_HEX(val) log_raw_uart_write_hex(val) /*!< Log an integer as HEX value (example output: 0x89ABCDEF). */
  455. #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). */
  456. #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). */
  457. #define NRF_LOG_HEX_CHAR(val) log_raw_uart_write_hex_char(val) /*!< Log a character as HEX value (example output: AA). */
  458. #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). */
  459. #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). */
  460. #define NRF_LOG_HAS_INPUT() log_raw_uart_has_input() /*!< Check if the input buffer has unconsumed characters. */
  461. #define NRF_LOG_READ_INPUT(p_char) log_raw_uart_read_input(p_char) /*!< Consume a character from the input buffer. */
  462. #if !defined(DEBUG) && !defined(DOXYGEN)
  463. #undef NRF_LOG_DEBUG
  464. #define NRF_LOG_DEBUG(...)
  465. #undef NRF_LOG_PRINTF_DEBUG
  466. #define NRF_LOG_PRINTF_DEBUG(...)
  467. #undef NRF_LOG_STR_DEBUG
  468. #define NRF_LOG_STR_DEBUG(...)
  469. #undef NRF_LOG_HEX_DEBUG
  470. #define NRF_LOG_HEX_DEBUG(...)
  471. #undef NRF_LOG_HEX_CHAR_DEBUG
  472. #define NRF_LOG_HEX_CHAR_DEBUG(...)
  473. #endif // !defined(DEBUG) && !defined(DOXYGEN)
  474. #else
  475. #include "nrf_error.h"
  476. #include "nordic_common.h"
  477. // Empty definitions
  478. #define NRF_LOG_INIT() NRF_SUCCESS
  479. #define NRF_LOG(...)
  480. #define NRF_LOG_DEBUG(...)
  481. #define NRF_LOG_ERROR(...)
  482. #define NRF_LOG_PRINTF(...)
  483. #define NRF_LOG_PRINTF_DEBUG(...)
  484. #define NRF_LOG_PRINTF_ERROR(...)
  485. #define NRF_LOG_HEX(val)
  486. #define NRF_LOG_HEX_DEBUG(val)
  487. #define NRF_LOG_HEX_ERROR(val)
  488. #define NRF_LOG_HEX_CHAR(val)
  489. #define NRF_LOG_HEX_CHAR_DEBUG(val)
  490. #define NRF_LOG_HEX_CHAR_ERROR(val)
  491. #define NRF_LOG_HAS_INPUT() 0
  492. #define NRF_LOG_READ_INPUT(ignore) NRF_SUCCESS
  493. #endif
  494. /**@brief Function for writing HEX values.
  495. *
  496. * @note This function not thread-safe. It is written for convenience.
  497. * If you log from different application contexts, you might get different results.
  498. *
  499. * @retval NULL By default.
  500. */
  501. const char* log_hex(uint32_t value);
  502. /**@brief Function for writing HEX characters.
  503. *
  504. * @note This function not thread-safe. It is written for convenience.
  505. * If you log from different application contexts, you might get different results.
  506. *
  507. * @retval NULL By default.
  508. */
  509. const char* log_hex_char(const char value);
  510. #else // DOXYGEN
  511. /** @defgroup nrf_log UART/RTT logging
  512. * @{
  513. * @ingroup app_common
  514. *
  515. * @brief Library to output logging information over SEGGER's Real Time Transfer
  516. * (RTT), UART, or raw UART.
  517. *
  518. * This library provides macros that call the respective functions depending on
  519. * which protocol is used. Define LOG_USES_RTT=1 to enable logging over RTT,
  520. * NRF_LOG_USES_UART=1 to enable logging over UART, or NRF_LOG_USES_RAW_UART=1
  521. * to enable logging over raw UART. One of these defines must be set for any of
  522. * the macros to have effect. If you choose to not output information, all
  523. * logging macros can be left in the code without any cost; they will just be
  524. * ignored.
  525. */
  526. /**@brief Macro for initializing the logger.
  527. *
  528. * @retval NRF_SUCCESS If initialization was successful.
  529. * @retval NRF_ERROR Otherwise.
  530. */
  531. uint32_t NRF_LOG_INIT(void);
  532. /**@brief Macro for logging null-terminated strings.
  533. *
  534. * @details This function is more efficient than using printf.
  535. * The null termination will not be logged.
  536. *
  537. * @param msg Null-terminated string.
  538. */
  539. void NRF_LOG(const char* msg);
  540. /**@brief Macro for logging a printf string.
  541. *
  542. * @details Printf requires more processor time
  543. * than other logging functions. Therefore, applications that require logging
  544. * but need it to interfere as little as possible with the execution, should
  545. * avoid using printf.
  546. *
  547. * @note When NRF_LOG_USES_UART is set to 1, this macro is non-blocking.
  548. * If too much data is sent, some characters might be skipped.
  549. *
  550. * @param format_msg Printf format string.
  551. * @param ... Additional arguments replacing format specifiers in format_msg.
  552. */
  553. void NRF_LOG_PRINTF(const char * format_msg, ...);
  554. /**@brief Macro for logging an integer value as HEX.
  555. *
  556. * @details The output data is formatted as, for example, 0x89ABCDEF.
  557. * This function is more efficient than printf.
  558. *
  559. * @note When NRF_LOG_USES_UART is set to 1, this macro is non-blocking.
  560. * If too much data is sent, some characters might be skipped.
  561. *
  562. * @param value Integer value to be printed as HEX.
  563. */
  564. void NRF_LOG_HEX(uint32_t value);
  565. /**@brief Macro for logging a single character as HEX.
  566. *
  567. * @details The output string is formatted as, for example, AA.
  568. *
  569. * @note When NRF_LOG_USES_UART is set to 1, this macro is non-blocking.
  570. * If too much data is sent, some characters might be skipped.
  571. *
  572. * @param c Character.
  573. */
  574. void NRF_LOG_HEX_CHAR(uint8_t c);
  575. /**@brief Macro for checking if data is available in the input buffer.
  576. *
  577. * @note When NRF_LOG_USES_UART is set to 1, this macro is non-blocking.
  578. * If too much data is sent, some characters might be skipped.
  579. *
  580. * @retval 1 If characters are available to read.
  581. * @retval 0 If no characters are available.
  582. */
  583. int NRF_LOG_HAS_INPUT(void);
  584. /**@brief Macro for reading one character from the input buffer.
  585. *
  586. * @param[out] p_char Pointer where to store the character.
  587. *
  588. * @retval NRF_SUCCESS If the character was read out.
  589. * @retval NRF_ERROR_INVALID_DATA If no character could be read.
  590. */
  591. uint32_t NRF_LOG_READ_INPUT(char* p_char);
  592. /** @} */
  593. #endif // DOXYGEN
  594. #endif // NRF_LOG_H_