log_trace.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * Bernard the first version
  9. * 2013-06-26 Grissiom refactor
  10. */
  11. #ifndef __LOG_TRACE_H__
  12. #define __LOG_TRACE_H__
  13. #include <rtthread.h>
  14. #define LOG_TRACE_LEVEL_MASK 0x0f
  15. #define LOG_TRACE_LEVEL_NOTRACE 0x00
  16. #define LOG_TRACE_LEVEL_ERROR 0x01
  17. #define LOG_TRACE_LEVEL_WARNING 0x03
  18. #define LOG_TRACE_LEVEL_INFO 0x05
  19. #define LOG_TRACE_LEVEL_VERBOSE 0x07
  20. #define LOG_TRACE_LEVEL_DEBUG 0x09
  21. #define LOG_TRACE_LEVEL_ALL 0x0f
  22. #if defined(LOG_TRACE_USING_LEVEL_NOTRACE)
  23. #define LOG_TRACE_LEVEL_DEFAULT LOG_TRACE_LEVEL_NOTRACE
  24. #elif defined(LOG_TRACE_USING_LEVEL_ERROR)
  25. #define LOG_TRACE_LEVEL_DEFAULT LOG_TRACE_LEVEL_ERROR
  26. #elif defined(LOG_TRACE_USING_LEVEL_WARNING)
  27. #define LOG_TRACE_LEVEL_DEFAULT LOG_TRACE_LEVEL_WARNING
  28. #elif defined(LOG_TRACE_USING_LEVEL_INFO)
  29. #define LOG_TRACE_LEVEL_DEFAULT LOG_TRACE_LEVEL_INFO
  30. #elif defined(LOG_TRACE_USING_LEVEL_VERBOSE)
  31. #define LOG_TRACE_LEVEL_DEFAULT LOG_TRACE_LEVEL_VERBOSE
  32. #elif defined(LOG_TRACE_USING_LEVEL_DEBUG)
  33. #define LOG_TRACE_LEVEL_DEFAULT LOG_TRACE_LEVEL_DEBUG
  34. #else
  35. #define LOG_TRACE_LEVEL_DEFAULT LOG_TRACE_LEVEL_INFO
  36. #endif
  37. #define LOG_TRACE_ERROR "<1>"
  38. #define LOG_TRACE_WARNING "<3>"
  39. #define LOG_TRACE_INFO "<5>"
  40. #define LOG_TRACE_VERBOSE "<7>"
  41. #define LOG_TRACE_DEBUG "<9>"
  42. #define LOG_TRACE_OPT_NOTS 0x10 /* no timestamp */
  43. #define LOG_TRACE_OPT_LN 0x20 /* terminate the current line */
  44. #define LOG_TRACE_CTRL_FLUSH 0x10
  45. #define LOG_TRACE_CTRL_RESET 0x11
  46. #define LOG_TRACE_CTRL_DUMP 0x12
  47. //#define LOG_TRACE_USE_LONGNAME
  48. #ifndef LOG_TRACE_BUFSZ
  49. #define LOG_TRACE_BUFSZ RT_CONSOLEBUF_SIZE
  50. #endif
  51. /** maximum number of sessions that can be registered to the log_trace system
  52. */
  53. #ifndef LOG_TRACE_MAX_SESSION
  54. #define LOG_TRACE_MAX_SESSION 16
  55. #endif
  56. #ifdef LOG_TRACE_USE_LONGNAME
  57. typedef rt_uint64_t log_trace_idnum_t;
  58. #else
  59. typedef rt_uint32_t log_trace_idnum_t;
  60. #endif
  61. /* use a integer to represent a string to avoid strcmp. Even 4 chars
  62. * should be enough for most of the cases.
  63. * NOTE: take care when converting the name string to id number, you
  64. * can trapped in endianness.
  65. */
  66. union log_trace_id {
  67. char name[sizeof(log_trace_idnum_t)];
  68. log_trace_idnum_t num;
  69. };
  70. struct log_trace_session
  71. {
  72. union log_trace_id id;
  73. rt_uint8_t lvl;
  74. };
  75. /** initialize the log_trace system */
  76. int log_trace_init(void);
  77. /** register a session.
  78. *
  79. * @return RT_EOK on success. -RT_EFULL if there is no space for registration.
  80. */
  81. rt_err_t log_trace_register_session(const struct log_trace_session *session);
  82. /** find a session with name
  83. *
  84. * The name is not enclosed by parenthesis([]).
  85. *
  86. * @return RT_NULL if there is no such a session.
  87. */
  88. struct log_trace_session* log_trace_session_find(const char *name);
  89. /** set the log level of the default session. */
  90. void log_trace_set_level(rt_uint8_t level);
  91. /** set the log level of the session */
  92. void log_trace_session_set_level(
  93. struct log_trace_session *session, rt_uint8_t level);
  94. /** log according to the format
  95. *
  96. * the format of log_trace is: "<level>[name]log messages". It will output
  97. * "[systick][name]log messages" When there is no session named name, the
  98. * default session will be used.
  99. *
  100. * We have keep the level tag before the name tag because we don't print put
  101. * the level tag to the output and it's easier to implement if we place the
  102. * level tag in the very beginning.
  103. */
  104. void log_trace(const char *fmt, ...);
  105. /** log into session.
  106. *
  107. * the format of log_trace is: "<level>log messages". It will output
  108. * "[systick][name]log messages". The name is the name of the session. It is
  109. * faster than bare log_trace.
  110. */
  111. void log_session(const struct log_trace_session *session, const char *fmt, ...);
  112. extern void __logtrace_vfmtout(const struct log_trace_session *session,
  113. const char *fmt,
  114. va_list argptr);
  115. rt_inline void __logtrace_fmtout(const struct log_trace_session *session,
  116. const char *fmt, ...)
  117. {
  118. va_list args;
  119. va_start(args, fmt);
  120. __logtrace_vfmtout(session, fmt, args);
  121. va_end(args);
  122. }
  123. /**
  124. * log with numeric level
  125. *
  126. * The prototype of this function is:
  127. *
  128. * void log_session_lvl(struct log_trace_session *session,
  129. * int lvl,
  130. * const char *fmt, ...);
  131. *
  132. * If the @session is const and @level is greater than @session->lvl, the whole
  133. * function will be optimized out. This is suitable for performance critical
  134. * places where in most cases, the log is turned off by level.
  135. */
  136. #define log_session_lvl(session, level, fmt, ...) \
  137. do { \
  138. if ((level) > (session)->lvl) \
  139. { \
  140. break; \
  141. } \
  142. __logtrace_fmtout(session, fmt, ##__VA_ARGS__); \
  143. } while (0)
  144. /* here comes the global part. All sessions share the some output backend. */
  145. /** get the backend device */
  146. rt_device_t log_trace_get_device(void);
  147. /** set the backend device */
  148. rt_err_t log_trace_set_device(const char *device_name);
  149. void log_trace_flush(void);
  150. #ifdef RT_USING_DFS
  151. /** set the backend to file */
  152. void log_trace_set_file(const char *filename);
  153. void log_trace_file_init(const char *filename);
  154. #endif /* RT_USING_DFS */
  155. #endif