1
0

log_trace.h 6.3 KB

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