log_trace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. 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 _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(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. static void _lg_fmtout(
  226. struct log_trace_session *session, const char *fmt, va_list argptr)
  227. {
  228. /* 1 for ']' */
  229. static char _trace_buf[1+LOG_TRACE_BUFSZ];
  230. char *ptr;
  231. rt_size_t length;
  232. RT_ASSERT(session);
  233. RT_ASSERT(fmt);
  234. rt_snprintf(_trace_buf, sizeof(_trace_buf), "[%08x][", rt_tick_get());
  235. if (_traceout_device != RT_NULL)
  236. {
  237. rt_device_write(_traceout_device, -1, _trace_buf, 11);
  238. rt_device_write(_traceout_device, -1,
  239. session->id.name, _idname_len(session->id.num));
  240. }
  241. _trace_buf[0] = ']';
  242. ptr = &_trace_buf[1];
  243. length = rt_vsnprintf(ptr, LOG_TRACE_BUFSZ, fmt, argptr);
  244. if (length >= LOG_TRACE_BUFSZ)
  245. length = LOG_TRACE_BUFSZ - 1;
  246. if (_traceout_device != RT_NULL)
  247. {
  248. rt_device_write(_traceout_device, -1, _trace_buf, length + 1);
  249. }
  250. }
  251. void log_trace(const char *fmt, ...)
  252. {
  253. va_list args;
  254. int level;
  255. struct log_trace_session *session;
  256. RT_ASSERT(fmt);
  257. fmt += _lg_parse_lvl(fmt, strlen(fmt), &level);
  258. fmt += _lg_parse_session(fmt, strlen(fmt), &session);
  259. /* filter by level */
  260. if (level > session->lvl)
  261. return;
  262. va_start(args, fmt);
  263. _lg_fmtout(session, fmt, args);
  264. va_end(args);
  265. }
  266. FINSH_FUNCTION_EXPORT(log_trace, log trace);
  267. void log_session(struct log_trace_session *session, const char *fmt, ...)
  268. {
  269. va_list args;
  270. int level;
  271. RT_ASSERT(session);
  272. RT_ASSERT(fmt);
  273. fmt += _lg_parse_lvl(fmt, strlen(fmt), &level);
  274. if (level > session->lvl)
  275. return;
  276. va_start(args, fmt);
  277. _lg_fmtout(session, fmt, args);
  278. va_end(args);
  279. }
  280. void log_trace_flush(void)
  281. {
  282. rt_device_control(_traceout_device, LOG_TRACE_CTRL_FLUSH, RT_NULL);
  283. }
  284. FINSH_FUNCTION_EXPORT_ALIAS(log_trace_flush, log_flush, flush log on the buffer);
  285. /* RT-Thread common device interface */
  286. static rt_size_t _log_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  287. {
  288. char c;
  289. int level;
  290. rt_size_t head_len;
  291. const char *ptr = buffer;
  292. struct log_trace_session *session;
  293. head_len = _lg_parse_lvl(ptr, size, &level);
  294. head_len += _lg_parse_session(ptr+head_len, size-head_len, &session);
  295. /* filter by level */
  296. if (level > session->lvl)
  297. return size;
  298. if (_traceout_device != RT_NULL)
  299. {
  300. c = '[';
  301. rt_device_write(_traceout_device, -1, &c, 1);
  302. rt_device_write(_traceout_device, -1, session->id.name, _idname_len(session->id.num));
  303. c = ']';
  304. rt_device_write(_traceout_device, -1, &c, 1);
  305. rt_device_write(_traceout_device, -1, ((char*)buffer)+head_len, size - head_len);
  306. }
  307. return size;
  308. }
  309. static rt_err_t _log_control(rt_device_t dev, rt_uint8_t cmd, void *arg)
  310. {
  311. if (_traceout_device == RT_NULL) return -RT_ERROR;
  312. return rt_device_control(_traceout_device, cmd, arg);
  313. }
  314. void log_trace_init(void)
  315. {
  316. rt_memset(&_log_device, 0x00, sizeof(_log_device));
  317. _log_device.type = RT_Device_Class_Char;
  318. _log_device.init = RT_NULL;
  319. _log_device.open = RT_NULL;
  320. _log_device.close = RT_NULL;
  321. _log_device.read = RT_NULL;
  322. _log_device.write = _log_write;
  323. _log_device.control = _log_control;
  324. /* no indication and complete callback */
  325. _log_device.rx_indicate = RT_NULL;
  326. _log_device.tx_complete = RT_NULL;
  327. rt_device_register(&_log_device, "log", RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_RDWR);
  328. return ;
  329. }
  330. rt_device_t log_trace_get_device(void)
  331. {
  332. return _traceout_device;
  333. }
  334. rt_err_t log_trace_set_device(const char *device_name)
  335. {
  336. struct rt_device *output_device;
  337. /* find out output device */
  338. output_device = rt_device_find(device_name);
  339. if (output_device != RT_NULL)
  340. {
  341. rt_err_t result;
  342. /* open device */
  343. result = rt_device_open(output_device, RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_RDWR);
  344. if (result != RT_EOK)
  345. {
  346. rt_kprintf("Open trace device failed.\n");
  347. return -RT_ERROR;
  348. }
  349. }
  350. /* set trace out device */
  351. if (_traceout_device != RT_NULL)
  352. rt_device_close(_traceout_device);
  353. _traceout_device = output_device;
  354. return RT_EOK;
  355. }
  356. FINSH_FUNCTION_EXPORT_ALIAS(log_trace_set_device, log_device, set device of log trace);