1
0

log_trace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * Bernard the first version
  9. * 2013-06-26 Grissiom refactor
  10. */
  11. #include <rtthread.h>
  12. #include <rthw.h>
  13. #include <stdio.h>
  14. #include "log_trace.h"
  15. #ifdef RT_USING_FINSH
  16. #include <finsh.h>
  17. #else
  18. #define FINSH_FUNCTION_EXPORT(...)
  19. #define FINSH_FUNCTION_EXPORT_ALIAS(...)
  20. #endif
  21. /* log pseudo device */
  22. static struct rt_device _log_device;
  23. static rt_device_t _traceout_device = RT_NULL;
  24. /* define a default lg session. The name is empty. */
  25. static struct log_trace_session _def_session = {{"\0"}, LOG_TRACE_LEVEL_DEFAULT};
  26. static const struct log_trace_session *_the_sessions[LOG_TRACE_MAX_SESSION] = {&_def_session};
  27. /* there is a default session at least */
  28. static rt_uint16_t _the_sess_nr = 1;
  29. rt_inline int _idname_len(log_trace_idnum_t id)
  30. {
  31. /* little endian */
  32. if ((id & 0x000000FF) == 0)
  33. return 0;
  34. if ((id & 0x0000FF00) == 0)
  35. return 1;
  36. if ((id & 0x00FF0000) == 0)
  37. return 2;
  38. if ((id & 0xFF000000) == 0)
  39. return 3;
  40. #ifndef LOG_TRACE_USE_LONGNAME
  41. return 4;
  42. #else
  43. {
  44. rt_uint32_t id2 = id >> 32;
  45. if ((id2 & 0x000000FF) == 0)
  46. return 4;
  47. if ((id2 & 0x0000FF00) == 0)
  48. return 5;
  49. if ((id2 & 0x00FF0000) == 0)
  50. return 6;
  51. if ((id2 & 0xFF000000) == 0)
  52. return 7;
  53. return 8;
  54. }
  55. #endif
  56. }
  57. /* lookup the session according to name.
  58. *
  59. * @param len is the length of the name
  60. * @return the pointer to the named session. RT_NULL when there is no such a
  61. * session.
  62. */
  63. static struct log_trace_session* _lg_lookup_session(log_trace_idnum_t num)
  64. {
  65. static const struct log_trace_session *_cache = &_def_session;
  66. rt_uint16_t first, last;
  67. if (_cache->id.num == num)
  68. return (struct log_trace_session *)_cache;
  69. first = 0;
  70. last = _the_sess_nr;
  71. do {
  72. unsigned int i = (first + last)/2;
  73. RT_ASSERT(_the_sessions[i]);
  74. if (_the_sessions[i]->id.num == num)
  75. {
  76. /* there is no need to protect the _cache because write a pointer
  77. * is atomic. So we cannot get a invalid pointer. The worst thing
  78. * could happen is there is an interrupt in the read/modify/write
  79. * process and we wrote the old one to _cache. But it doesn't harm
  80. * a lot because it will be flushed in the next time. */
  81. _cache = _the_sessions[i];
  82. return (struct log_trace_session *)_the_sessions[i];
  83. }
  84. else if (_the_sessions[i]->id.num > num)
  85. {
  86. last = i;
  87. }
  88. else // _the_sessions[i]->id.num < num
  89. {
  90. first = i;
  91. }
  92. } while (first != last-1);
  93. return RT_NULL;
  94. }
  95. rt_err_t log_trace_register_session(const struct log_trace_session *session)
  96. {
  97. unsigned int lvl, i;
  98. if (_the_sess_nr == LOG_TRACE_MAX_SESSION)
  99. return -RT_EFULL;
  100. if (session == RT_NULL)
  101. return RT_EOK;
  102. lvl = rt_hw_interrupt_disable();
  103. /* inserting the sessions in ascending order.
  104. *
  105. * this might take relatively long time. But since the register should only
  106. * happen when initialize the whole system, this should not be a matter. */
  107. for (i = 0; i < _the_sess_nr; i++)
  108. {
  109. if (_the_sessions[i]->id.num > session->id.num)
  110. {
  111. rt_memmove(_the_sessions+i+1, _the_sessions+i,
  112. (_the_sess_nr-i)*sizeof(&_the_sessions[0]));
  113. _the_sessions[i] = session;
  114. break;
  115. }
  116. else if (_the_sessions[i]->id.num == session->id.num)
  117. {
  118. rt_kprintf("registering session 0x%p twice\n", session);
  119. rt_hw_interrupt_enable(lvl);
  120. return -RT_ERROR;
  121. }
  122. }
  123. if (i == _the_sess_nr)
  124. _the_sessions[i] = session;
  125. _the_sess_nr++;
  126. rt_hw_interrupt_enable(lvl);
  127. return RT_EOK;
  128. }
  129. struct log_trace_session* log_trace_session_find(const char *name)
  130. {
  131. union log_trace_id *idp;
  132. RT_ASSERT(name);
  133. idp = (union log_trace_id*)name;
  134. return _lg_lookup_session(idp->num);
  135. }
  136. void log_trace_set_level(rt_uint8_t level)
  137. {
  138. _def_session.lvl = level;
  139. }
  140. FINSH_FUNCTION_EXPORT_ALIAS(log_trace_set_level, log_level, set the filter level of log trace);
  141. void log_trace_session_set_level(struct log_trace_session *sess, rt_uint8_t level)
  142. {
  143. RT_ASSERT(sess);
  144. sess->lvl = level;
  145. }
  146. /* parse the level info in fmt
  147. *
  148. * @param flen the length of the format.
  149. * @param lvlp the pointer to level. It will store the level in the memory the
  150. * lvlp points to. The default value is LOG_TRACE_LEVEL_DEFAULT.
  151. * @return the number of char it scaned.
  152. */
  153. static rt_size_t _lg_parse_lvl(const char *fmt, rt_size_t flen, int *lvlp)
  154. {
  155. RT_ASSERT(fmt);
  156. RT_ASSERT(lvlp);
  157. /* setup default value */
  158. *lvlp = LOG_TRACE_LEVEL_DEFAULT;
  159. if (flen < 3)
  160. {
  161. return 0;
  162. }
  163. if (fmt[0] == '<' && fmt[2] == '>')
  164. {
  165. *lvlp = fmt[1] - '0';
  166. return 3;
  167. }
  168. return 0;
  169. }
  170. /* parse the header in fmt
  171. *
  172. * @param flen the length of the format.
  173. * @param sessp the pointer of pointer to the session. It will store the
  174. * session pointer in the memory the sessp points to. When failed to
  175. * find the session, it will be setted to the default session.
  176. * @return the number of char it scaned, i.e., the length of header.
  177. */
  178. static rt_size_t _lg_parse_session(
  179. const char *fmt, rt_size_t flen, struct log_trace_session **sessp)
  180. {
  181. unsigned int i;
  182. struct log_trace_session *tmpsess;
  183. union log_trace_id id;
  184. RT_ASSERT(fmt);
  185. RT_ASSERT(sessp);
  186. /* setup default value */
  187. *sessp = &_def_session;
  188. /* no name space left */
  189. if (flen < sizeof(id) + 2)
  190. return 0;
  191. if (fmt[0] != '[')
  192. return 0;
  193. id.num = 0;
  194. /* skip '[' and convert the string to id number. */
  195. for (i = 1; fmt[i] != ']'; i++)
  196. {
  197. if (i - 1 == sizeof(id))
  198. return 0;
  199. id.name[i-1] = fmt[i];
  200. }
  201. tmpsess = _lg_lookup_session(id.num);
  202. if (tmpsess != RT_NULL)
  203. {
  204. *sessp = tmpsess;
  205. /* only count the header length when we found the session. So
  206. * the wrong [name] will be printed out. */
  207. return i + 1;
  208. }
  209. return 0;
  210. }
  211. void __logtrace_vfmtout(const struct log_trace_session *session,
  212. const char *fmt,
  213. va_list argptr)
  214. {
  215. /* 1 for ']' */
  216. static char _trace_buf[1+LOG_TRACE_BUFSZ];
  217. char *ptr;
  218. rt_size_t length;
  219. RT_ASSERT(session);
  220. RT_ASSERT(fmt);
  221. /* it's default session */
  222. if (session->id.name[0] == '\0')
  223. {
  224. rt_snprintf(_trace_buf, sizeof(_trace_buf), "[%08x]", rt_tick_get());
  225. if (_traceout_device != RT_NULL)
  226. {
  227. rt_device_write(_traceout_device, -1, _trace_buf, 10);
  228. }
  229. ptr = &_trace_buf[0];
  230. }
  231. else
  232. {
  233. rt_snprintf(_trace_buf, sizeof(_trace_buf), "[%08x][", rt_tick_get());
  234. if (_traceout_device != RT_NULL)
  235. {
  236. rt_device_write(_traceout_device, -1, _trace_buf, 11);
  237. rt_device_write(_traceout_device, -1,
  238. session->id.name, _idname_len(session->id.num));
  239. }
  240. _trace_buf[0] = ']';
  241. ptr = &_trace_buf[1];
  242. }
  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. __logtrace_vfmtout(session, fmt, args);
  264. va_end(args);
  265. }
  266. FINSH_FUNCTION_EXPORT(log_trace, log trace);
  267. void log_session(const 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. __logtrace_vfmtout(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, int cmd, void *arg)
  310. {
  311. if (_traceout_device == RT_NULL) return -RT_ERROR;
  312. return rt_device_control(_traceout_device, cmd, arg);
  313. }
  314. #ifdef RT_USING_DEVICE_OPS
  315. const static struct rt_device_ops log_device_ops =
  316. {
  317. RT_NULL,
  318. RT_NULL,
  319. RT_NULL,
  320. RT_NULL,
  321. _log_write,
  322. _log_control
  323. };
  324. #endif
  325. int log_trace_init(void)
  326. {
  327. rt_memset(&_log_device, 0x00, sizeof(_log_device));
  328. _log_device.type = RT_Device_Class_Char;
  329. #ifdef RT_USING_DEVICE_OPS
  330. _log_device.ops = &log_device_ops;
  331. #else
  332. _log_device.init = RT_NULL;
  333. _log_device.open = RT_NULL;
  334. _log_device.close = RT_NULL;
  335. _log_device.read = RT_NULL;
  336. _log_device.write = _log_write;
  337. _log_device.control = _log_control;
  338. #endif
  339. /* no indication and complete callback */
  340. _log_device.rx_indicate = RT_NULL;
  341. _log_device.tx_complete = RT_NULL;
  342. rt_device_register(&_log_device, "log", RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_RDWR);
  343. /* set console as default device */
  344. _traceout_device = rt_console_get_device();
  345. return 0;
  346. }
  347. INIT_DEVICE_EXPORT(log_trace_init);
  348. rt_device_t log_trace_get_device(void)
  349. {
  350. return _traceout_device;
  351. }
  352. rt_err_t log_trace_set_device(const char *device_name)
  353. {
  354. struct rt_device *output_device;
  355. /* find out output device */
  356. output_device = rt_device_find(device_name);
  357. if (output_device != RT_NULL)
  358. {
  359. rt_err_t result;
  360. /* open device */
  361. result = rt_device_open(output_device, RT_DEVICE_FLAG_STREAM | RT_DEVICE_OFLAG_RDWR);
  362. if (result != RT_EOK)
  363. {
  364. rt_kprintf("Open trace device failed.\n");
  365. return -RT_ERROR;
  366. }
  367. }
  368. /* set trace out device */
  369. if (_traceout_device != RT_NULL)
  370. rt_device_close(_traceout_device);
  371. _traceout_device = output_device;
  372. return RT_EOK;
  373. }
  374. FINSH_FUNCTION_EXPORT_ALIAS(log_trace_set_device, log_device, set device of log trace);