log_trace.c 11 KB

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