1
0

memlog.c 1020 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. * 2013-06-26 Grissiom the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <log_trace.h>
  13. #define PIPE_SZ 2048
  14. #define PIPE_NAME "memlog"
  15. static rt_pipe_t *_log_pipe = NULL;
  16. static rt_uint8_t outbuf[1024];
  17. void memlog_flush(void)
  18. {
  19. rt_size_t readsz;
  20. rt_device_t console;
  21. console = rt_console_get_device();
  22. if (!console) return;
  23. readsz = rt_device_read((rt_device_t)&(_log_pipe->parent), 0, outbuf, sizeof(outbuf));
  24. if (readsz)
  25. rt_device_write(console, 0, outbuf, readsz);
  26. }
  27. int memlog_init(void)
  28. {
  29. _log_pipe = rt_pipe_create(PIPE_NAME, PIPE_SZ);
  30. if (_log_pipe == RT_NULL)
  31. {
  32. rt_kprintf("init pipe device failed.\n");
  33. return -1;
  34. }
  35. log_trace_set_device(PIPE_NAME);
  36. rt_thread_idle_sethook(memlog_flush);
  37. return 0;
  38. }
  39. INIT_APP_EXPORT(memlog_init);