memlog.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <rtthread.h>
  2. #include <rtdevice.h>
  3. #include <log_trace.h>
  4. #define PIPE_SZ 2048
  5. #define PIPE_NAME "lgpipe"
  6. static rt_uint8_t pipebuf[PIPE_SZ];
  7. static struct rt_pipe_device pipedev;
  8. static rt_uint8_t outbuf[1024];
  9. void memlog_flush(void)
  10. {
  11. rt_size_t remainsz, readsz;
  12. rt_device_t console;
  13. console = rt_console_get_device();
  14. if (!console)
  15. return;
  16. rt_device_control((rt_device_t)&pipedev, PIPE_CTRL_GET_SPACE, &remainsz);
  17. if (remainsz == 0)
  18. {
  19. rt_kprintf("logtrace pipe "PIPE_NAME" is full, some log may lost\n");
  20. }
  21. readsz = rt_device_read((rt_device_t)&pipedev, 0, outbuf, sizeof(outbuf));
  22. if (readsz)
  23. rt_device_write(console, 0, outbuf, readsz);
  24. }
  25. void memlog_init(void)
  26. {
  27. rt_err_t res;
  28. /* make sure the RT_PIPE_FLAG_BLOCK_RD is not set. The Idle should not be
  29. * blocked. RT_PIPE_FLAG_FORCE_WR will let the pipe discard some old data
  30. * when pipe is full. */
  31. res = rt_pipe_init(&pipedev, PIPE_NAME, RT_PIPE_FLAG_FORCE_WR,
  32. pipebuf, sizeof(pipebuf));
  33. if (res != RT_EOK)
  34. {
  35. rt_kprintf("init pipe device failed: %d\n", res);
  36. return;
  37. }
  38. log_trace_set_device(PIPE_NAME);
  39. rt_thread_idle_sethook(memlog_flush);
  40. }