memlog.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * File : memlog.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. * 2013-06-26 Grissiom the first version
  23. */
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. #include <log_trace.h>
  27. #define PIPE_SZ 2048
  28. #define PIPE_NAME "memlog"
  29. static rt_pipe_t *_log_pipe = NULL;
  30. static rt_uint8_t outbuf[1024];
  31. void memlog_flush(void)
  32. {
  33. rt_size_t readsz;
  34. rt_device_t console;
  35. console = rt_console_get_device();
  36. if (!console) return;
  37. readsz = rt_device_read((rt_device_t)&(_log_pipe->parent), 0, outbuf, sizeof(outbuf));
  38. if (readsz)
  39. rt_device_write(console, 0, outbuf, readsz);
  40. }
  41. int memlog_init(void)
  42. {
  43. _log_pipe = rt_pipe_create(PIPE_NAME, PIPE_SZ);
  44. if (_log_pipe == RT_NULL)
  45. {
  46. rt_kprintf("init pipe device failed.\n");
  47. return -1;
  48. }
  49. log_trace_set_device(PIPE_NAME);
  50. rt_thread_idle_sethook(memlog_flush);
  51. return 0;
  52. }
  53. INIT_APP_EXPORT(memlog_init);