log_trace.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * File : log_trace.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2013, 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 0x02
  32. #define LOG_TRACE_LEVEL_INFO 0x03
  33. #define LOG_TRACE_LEVEL_DEBUG 0x04
  34. #define LOG_TRACE_LEVEL_ALL 0x0f
  35. #ifndef LOG_TRACE_LEVEL_DEFAULT
  36. #define LOG_TRACE_LEVEL_DEFAULT LOG_TRACE_LEVEL_INFO
  37. #endif
  38. #define LOG_TRACE_ERROR "<1>"
  39. #define LOG_TRACE_WARNING "<2>"
  40. #define LOG_TRACE_INFO "<3>"
  41. #define LOG_TRACE_DEBUG "<4>"
  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. void 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(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(struct log_trace_session *session, const char *fmt, ...);
  112. /* here comes the global part. All sessions share the some output backend. */
  113. /** get the backend device */
  114. rt_device_t log_trace_get_device(void);
  115. /** set the backend device */
  116. rt_err_t log_trace_set_device(const char *device_name);
  117. void log_trace_flush(void);
  118. /** set the backend to file */
  119. void log_trace_set_file(const char *filename);
  120. /* log trace for NAND Flash */
  121. void log_trace_nand_init(const char *nand_device);
  122. void log_trace_file_init(const char *filename);
  123. #endif