salof.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * @Author: jiejie
  3. * @Github: https://github.com/jiejieTop
  4. * @Date: 2019-12-27 23:10:36
  5. * @LastEditTime: 2020-06-17 15:22:56
  6. * @Description: the code belongs to jiejie, please keep the author insalof_formation and source code according to the license.
  7. */
  8. /** synchronous asynchronous log output framework */
  9. #include "salof.h"
  10. #ifdef SALOF_USING_LOG
  11. #ifndef SALOF_BUFF_SIZE
  12. #define SALOF_BUFF_SIZE (1024U)
  13. #endif
  14. #ifndef SALOF_FIFO_SIZE
  15. #define SALOF_FIFO_SIZE (2048U)
  16. #endif
  17. static int salof_out(char *buf, int len);
  18. #if SALOF_USING_SALOF
  19. #include <string.h>
  20. static salof_fifo_t _salof_fifo = NULL;
  21. static int _len;
  22. static char _out_buff[SALOF_BUFF_SIZE];
  23. #if !SALOF_USING_IDLE_HOOK
  24. static salof_tcb _salof_task;
  25. void salof_task(void *parm);
  26. #else
  27. #if !defined(salof_handler)
  28. #error "salof_handler need to be defined as your hook function"
  29. #endif
  30. #endif
  31. #endif
  32. static char _salof_format_buff[SALOF_BUFF_SIZE];
  33. int salof_init(void)
  34. {
  35. #if SALOF_USING_SALOF
  36. _salof_fifo = salof_fifo_create(SALOF_FIFO_SIZE);
  37. if(_salof_fifo == NULL)
  38. return -1;
  39. #if !SALOF_USING_IDLE_HOOK
  40. _salof_task = salof_task_create("salof_task", salof_task, NULL, SALOF_TASK_STACK_SIZE, SALOF_TASK_PRIO, SALOF_TASK_TICK);
  41. if(_salof_task == NULL)
  42. return -1;
  43. #endif
  44. #endif
  45. return 0;
  46. }
  47. void salof(const char *fmt, ...)
  48. {
  49. va_list args;
  50. int len;
  51. va_start(args, fmt);
  52. len = salof_format_nstr(_salof_format_buff, SALOF_BUFF_SIZE - 1, fmt, args);
  53. if(len > SALOF_BUFF_SIZE)
  54. len = SALOF_BUFF_SIZE - 1;
  55. #if SALOF_USING_SALOF
  56. salof_fifo_write(_salof_fifo, _salof_format_buff, len, 100);
  57. #else
  58. salof_out(_salof_format_buff, len);
  59. #endif
  60. va_end(args);
  61. }
  62. static int salof_out(char *buf, int len)
  63. {
  64. return send_buff(buf, len);
  65. }
  66. #if SALOF_USING_SALOF
  67. void salof_handler( void )
  68. {
  69. _len = salof_fifo_read(_salof_fifo, _out_buff, sizeof(_out_buff), 0);
  70. if(_len > 0) {
  71. salof_out((char *)_out_buff, _len);
  72. memset(_out_buff, 0, _len);
  73. }
  74. }
  75. #endif
  76. #if !SALOF_USING_IDLE_HOOK
  77. void salof_task(void *parm)
  78. {
  79. (void)parm;
  80. while(1)
  81. {
  82. #if SALOF_USING_SALOF
  83. salof_handler();
  84. #endif
  85. }
  86. }
  87. #endif
  88. #endif