logger.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include "bsp/bsp.h"
  4. #include "logger.h"
  5. #include "os/co_task.h"
  6. #include "os/queue.h"
  7. static uint32_t level_data[2];
  8. static co_queue_t log_queue = NULL;
  9. static void log_co_task(void *args) {
  10. log_chan_t log_v;
  11. while(1) {
  12. if (queue_get(log_queue, &log_v)){
  13. sys_debug("%d: %d\n", log_v.id, log_v.value);
  14. }
  15. co_task_yield();
  16. }
  17. }
  18. void log_start_task(void) {
  19. log_queue = queue_create(1000, sizeof(log_chan_t));
  20. co_task_create(log_co_task, NULL, 512);
  21. }
  22. void log_chan_value(u16 id, s32 value) {
  23. log_chan_t log_v;
  24. log_v.id = id;
  25. log_v.value = value;
  26. queue_put(log_queue, &log_v);
  27. }
  28. static int _get_level(int mod){
  29. int index = mod_bit_start(mod)/(32+1);
  30. mod = mod_bit_start(mod) % 32;
  31. return (level_data[index] >> mod) & LEVEL_MASK;
  32. }
  33. void set_log_level(int mod, int l){
  34. int index = mod_bit_start(mod)/(32+1);
  35. mod = mod_bit_start(mod) % 32;
  36. level_data[index] = (level_data[index] & (~(LEVEL_MASK<<mod))) | ((l & LEVEL_MASK)<<mod);
  37. }
  38. static void log_out(char *fmt, va_list args){
  39. vprintf(fmt, args);
  40. if (fmt[strlen(fmt) - 1] != '\n'){
  41. printf("\n");
  42. }
  43. }
  44. void log_debug(int mod, char *fmt, ...){
  45. if (_get_level(mod) >= L_debug){
  46. va_list args;
  47. va_start(args, fmt);
  48. log_out(fmt, args);
  49. va_end(args);
  50. }
  51. }
  52. void log_warning(int mod, char *fmt, ...){
  53. if (_get_level(mod) >= L_warning){
  54. va_list args;
  55. va_start(args, fmt);
  56. log_out(fmt, args);
  57. va_end(args);
  58. }
  59. }
  60. void log_error(int mod, char *fmt, ...){
  61. if (_get_level(mod) >= L_error){
  62. va_list args;
  63. va_start(args, fmt);
  64. log_out(fmt, args);
  65. va_end(args);
  66. }
  67. }
  68. //rewrite the fputc, so that the printf,vprintf can log the info the can
  69. #if LOG_UART==0
  70. static char log_buffer[8];
  71. static int log_index = 0;
  72. int fputc(int c, FILE *fp){
  73. can_id_t frame_id;
  74. frame_id.id=0;
  75. frame_id.src = CAN_MY_ADDRESS;
  76. frame_id.type = ptype_indicater;
  77. if (c == '\n'){ //don't send '\n'
  78. if (log_index != 0){
  79. frame_id.dest = 0x72;
  80. shark_can0_send_message(frame_id.id, log_buffer, log_index);
  81. }
  82. log_index = 0;
  83. }else if (log_index == sizeof(log_buffer)){
  84. frame_id.dest = 0x70;
  85. shark_can0_send_message(frame_id.id, log_buffer, log_index);
  86. log_index = 0;
  87. log_buffer[log_index++] = c;
  88. }else{
  89. log_buffer[log_index++] = c;
  90. }
  91. return 1;
  92. }
  93. #endif