log_trace.h 5.8 KB

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