log_trace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * File : log_trace.c
  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. #include <rtthread.h>
  26. #include <rthw.h>
  27. #include <stdio.h>
  28. #include "log_trace.h"
  29. #ifdef RT_USING_FINSH
  30. #include <finsh.h>
  31. #else
  32. #define FINSH_FUNCTION_EXPORT(...)
  33. #define FINSH_FUNCTION_EXPORT_ALIAS(...)
  34. #endif
  35. /* log pseudo device */
  36. static struct rt_device _log_device;
  37. static rt_device_t _traceout_device = RT_NULL;
  38. /* define a default lg session. The name is empty. */
  39. static struct log_trace_session _def_session = {{"\0"}, LOG_TRACE_LEVEL_INFO};
  40. static const struct log_trace_session *_the_sessions[LOG_TRACE_MAX_SESSION] = {&_def_session};
  41. /* there is a default session at least */
  42. static rt_uint16_t _the_sess_nr = 1;
  43. rt_inline int _idname_len(log_trace_idnum_t id)
  44. {
  45. /* little endian */
  46. if ((id & 0x000000FF) == 0)
  47. return 0;
  48. if ((id & 0x0000FF00) == 0)
  49. return 1;
  50. if ((id & 0x00FF0000) == 0)
  51. return 2;
  52. if ((id & 0xFF000000) == 0)
  53. return 3;
  54. #ifndef LOG_TRACE_USE_LONGNAME
  55. return 4;
  56. #else
  57. {
  58. rt_uint32_t id2 = id >> 32;
  59. if ((id2 & 0x000000FF) == 0)
  60. return 4;
  61. if ((id2 & 0x0000FF00) == 0)
  62. return 5;
  63. if ((id2 & 0x00FF0000) == 0)
  64. return 6;
  65. if ((id2 & 0xFF000000) == 0)
  66. return 7;
  67. return 8;
  68. }
  69. #endif
  70. }
  71. /* lookup the session according to name.
  72. *
  73. * @param len is the length of the name
  74. * @return the pointer to the named session. RT_NULL when there is no such a
  75. * session.
  76. */
  77. static struct log_trace_session* _lg_lookup_session(log_trace_idnum_t num)
  78. {
  79. static const struct log_trace_session *_cache = &_def_session;
  80. rt_uint16_t first, last;
  81. if (_cache->id.num == num)
  82. return (struct log_trace_session *)_cache;
  83. first = 0;
  84. last = _the_sess_nr;
  85. do {
  86. unsigned int i = (first + last)/2;
  87. RT_ASSERT(_the_sessions[i]);
  88. if (_the_sessions[i]->id.num == num)
  89. {
  90. /* there is no need to protect the _cache because write a pointer
  91. * is atomic. So we cannot get a invalid pointer. The worst thing
  92. * could happen is there is an interrupt in the read/modify/write
  93. * process and we wrote the old one to _cache. But it doesn't harm
  94. * a lot because it will be flushed in the next time. */
  95. _cache = _the_sessions[i];
  96. return (struct log_trace_session *)_the_sessions[i];
  97. }
  98. else if (_the_sessions[i]->id.num > num)
  99. {
  100. last = i;
  101. }
  102. else // _the_sessions[i]->id.num < num
  103. {
  104. first = i;
  105. }
  106. } while (first != last-1);
  107. return RT_NULL;
  108. }
  109. rt_err_t log_trace_register_session(const struct log_trace_session *session)
  110. {
  111. unsigned int lvl, i;
  112. if (_the_sess_nr == LOG_TRACE_MAX_SESSION)
  113. return -RT_EFULL;
  114. if (session == RT_NULL)
  115. return RT_EOK;
  116. lvl = rt_hw_interrupt_disable();
  117. /* inserting the sessions in ascending order.
  118. *
  119. * this might take relatively long time. But since the register should only
  120. * happen when initialize the whole system, this should not be a matter. */
  121. for (i = 0; i < _the_sess_nr; i++)
  122. {
  123. if (_the_sessions[i]->id.num > session->id.num)
  124. {
  125. rt_memmove(_the_sessions+i+1, _the_sessions+i,
  126. (_the_sess_nr-i)*sizeof(&_the_sessions[0]));
  127. _the_sessions[i] = session;
  128. break;
  129. }
  130. else if (_the_sessions[i]->id.num == session->id.num)
  131. {
  132. rt_kprintf("registering session 0x%p twice\n", session);
  133. rt_hw_interrupt_enable(lvl);
  134. return -RT_ERROR;
  135. }
  136. }
  137. if (i == _the_sess_nr)
  138. _the_sessions[i] = session;
  139. _the_sess_nr++;
  140. rt_hw_interrupt_enable(lvl);
  141. return RT_EOK;
  142. }
  143. struct log_trace_session* log_trace_session_find(const char *name)
  144. {
  145. union log_trace_id *idp;
  146. RT_ASSERT(name);
  147. idp = (union log_trace_id*)name;
  148. return _lg_lookup_session(idp->num);
  149. }
  150. void log_trace_set_level(rt_uint8_t level)
  151. {
  152. _def_session.lvl = level;
  153. }
  154. FINSH_FUNCTION_EXPORT_ALIAS(log_trace_set_level, log_level, set the filter level of log trace);
  155. void log_trace_session_set_level(struct log_trace_session *sess, rt_uint8_t level)
  156. {
  157. RT_ASSERT(sess);
  158. sess->lvl = level;
  159. }
  160. /* parse the level info in fmt
  161. *
  162. * @param flen the length of the format.
  163. * @param lvlp the pointer to level. It will store the level in the memory the
  164. * lvlp points to. The default value is LOG_TRACE_LEVEL_DEFAULT.
  165. * @return the number of char it scaned.
  166. */
  167. static rt_size_t _lg_parse_lvl(const char *fmt, rt_size_t flen, int *lvlp)
  168. {
  169. RT_ASSERT(fmt);
  170. RT_ASSERT(lvlp);
  171. /* setup default value */
  172. *lvlp = LOG_TRACE_LEVEL_DEFAULT;
  173. if (flen < 3)
  174. {
  175. return 0;
  176. }
  177. if (fmt[0] == '<' && fmt[2] == '>')
  178. {
  179. *lvlp = fmt[1] - '0';
  180. return 3;
  181. }
  182. return 0;
  183. }
  184. /* parse the header in fmt
  185. *
  186. * @param flen the length of the format.
  187. * @param sessp the pointer of pointer to the session. It will store the
  188. * session pointer in the memory the sessp points to. When failed to
  189. * find the session, it will be setted to the default session.
  190. * @return the number of char it scaned, i.e., the length of header.
  191. */
  192. static rt_size_t _lg_parse_session(
  193. const char *fmt, rt_size_t flen, struct log_trace_session **sessp)
  194. {
  195. unsigned int i;
  196. struct log_trace_session *tmpsess;
  197. union log_trace_id id;
  198. RT_ASSERT(fmt);
  199. RT_ASSERT(sessp);
  200. /* setup default value */
  201. *sessp = &_def_session;
  202. /* no name space left */
  203. if (flen < sizeof(id) + 2)
  204. return 0;
  205. if (fmt[0] != '[')
  206. return 0;
  207. id.num = 0;
  208. /* skip '[' and convert the string to id number. */
  209. for (i = 1; fmt[i] != ']'; i++)
  210. {
  211. if (i - 1 == sizeof(id))
  212. return 0;
  213. id.name[i-1] = fmt[i];
  214. }
  215. tmpsess = _lg_lookup_session(id.num);
  216. if (tmpsess != RT_NULL)
  217. {
  218. *sessp = tmpsess;
  219. /* only count the header length when we found the session. So
  220. * the wrong [name] will be printed out. */
  221. return i + 1;
  222. }
  223. return 0;
  224. }
  225. void __logtrace_vfmtout(const struct log_trace_session *session,
  226. const char *fmt,
  227. va_list argptr)
  228. {
  229. /* 1 for ']' */
  230. static char _trace_buf[1+LOG_TRACE_BUFSZ];
  231. char *ptr;
  232. rt_size_t length;
  233. RT_ASSERT(session);
  234. RT_ASSERT(fmt);
  235. rt_snprintf(_trace_buf, sizeof(_trace_buf), "[%08x][", rt_tick_get());
  236. if (_traceout_device != RT_NULL)
  237. {
  238. rt_device_write(_traceout_device, -1, _trace_buf, 11);
  239. rt_device_write(_traceout_device, -1,
  240. session->id.name, _idname_len(session->id.num));
  241. }
  242. _trace_buf[0] = ']';
  243. ptr = &_trace_buf[1];
  244. length = rt_vsnprintf(ptr, LOG_TRACE_BUFSZ, fmt, argptr);
  245. if (length >= LOG_TRACE_BUFSZ)
  246. length = LOG_TRACE_BUFSZ - 1;
  247. if (_traceout_device != RT_NULL)
  248. {
  249. rt_device_write(_traceout_device, -1, _trace_buf, length + 1);
  250. }
  251. }
  252. void log_trace(const char *fmt, ...)
  253. {
  254. va_list args;
  255. int level;
  256. struct log_trace_session *session;
  257. RT_ASSERT(fmt);
  258. fmt += _lg_parse_lvl(fmt, strlen(fmt), &level);
  259. fmt += _lg_parse_session(fmt, strlen(fmt), &session);
  260. /* filter by level */
  261. if (level > session->lvl)
  262. return;
  263. va_start(args, fmt);
  264. __logtrace_vfmtout(session, fmt, args);
  265. va_end(args);
  266. }
  267. FINSH_FUNCTION_EXPORT(log_trace, log trace);
  268. void log_session(const struct log_trace_session *session, const char *fmt, ...)
  269. {
  270. va_list args;
  271. int level;
  272. RT_ASSERT(session);
  273. RT_ASSERT(fmt);
  274. fmt += _lg_parse_lvl(fmt, strlen(fmt), &level);
  275. if (level > session->lvl)
  276. return;
  277. va_start(args, fmt);
  278. __logtrace_vfmtout(session, fmt, args);
  279. va_end(args);
  280. }
  281. void log_trace_flush(void)
  282. {
  283. rt_device_control(_traceout_device, LOG_TRACE_CTRL_FLUSH, RT_NULL);
  284. }
  285. FINSH_FUNCTION_EXPORT_ALIAS(log_trace_flush, log_flush, flush log on the buffer);
  286. /* RT-Thread common device interface */
  287. static rt_size_t _log_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  288. {
  289. char c;
  290. int level;
  291. rt_size_t head_len;
  292. const char *ptr = buffer;
  293. struct log_trace_session *session;
  294. head_len = _lg_parse_lvl(ptr, size, &level);
  295. head_len += _lg_parse_session(ptr+head_len, size-head_len, &session);
  296. /* filter by level */
  297. if (level > session->lvl)
  298. return size;
  299. if (_traceout_device != RT_NULL)
  300. {
  301. c = '[';
  302. rt_device_write(_traceout_device, -1, &c, 1);
  303. rt_device_write(_traceout_device, -1, session->id.name, _idname_len(session->id.num));
  304. c = ']';
  305. rt_device_write(_traceout_device, -1, &c, 1);
  306. rt_device_write(_traceout_device, -1, ((char*)buffer)+head_len, size - head_len);
  307. }
  308. return size;
  309. }
  310. static rt_err_t _log_control(rt_device_t dev, rt_uint8_t cmd, void *arg)
  311. {
  312. if (_traceout_device == RT_NULL) return -RT_ERROR;
  313. return rt_device_control(_traceout_device, cmd, arg);
  314. }
  315. void log_trace_init(void)
  316. {
  317. rt_memset(&_log_device, 0x00, sizeof(_log_device));
  318. _log_device.type = RT_Device_Class_Char;
  319. _log_device.init = RT_NULL;
  320. _log_device.open = RT_NULL;
  321. _log_device.close = RT_NULL;
  322. _log_device.read = RT_NULL;
  323. _log_device.write = _log_write;
  324. _log_device.control = _log_control;
  325. /* no indication and complete callback */
  326. _log_device.rx_indicate = RT_NULL;
  327. _log_device.tx_complete = RT_NULL;
  328. rt_device_register(&_log_device, "log", RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_RDWR);
  329. return ;
  330. }
  331. rt_device_t log_trace_get_device(void)
  332. {
  333. return _traceout_device;
  334. }
  335. rt_err_t log_trace_set_device(const char *device_name)
  336. {
  337. struct rt_device *output_device;
  338. /* find out output device */
  339. output_device = rt_device_find(device_name);
  340. if (output_device != RT_NULL)
  341. {
  342. rt_err_t result;
  343. /* open device */
  344. result = rt_device_open(output_device, RT_DEVICE_FLAG_STREAM | RT_DEVICE_OFLAG_RDWR);
  345. if (result != RT_EOK)
  346. {
  347. rt_kprintf("Open trace device failed.\n");
  348. return -RT_ERROR;
  349. }
  350. }
  351. /* set trace out device */
  352. if (_traceout_device != RT_NULL)
  353. rt_device_close(_traceout_device);
  354. _traceout_device = output_device;
  355. return RT_EOK;
  356. }
  357. FINSH_FUNCTION_EXPORT_ALIAS(log_trace_set_device, log_device, set device of log trace);