logger.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include <stdint.h>
  4. #include "logger.h"
  5. #include "app/protocol.h"
  6. static uint32_t level_data[2];
  7. static int _get_level(int mod){
  8. int index = mod_bit_start(mod)/(32+1);
  9. mod = mod_bit_start(mod) % 32;
  10. return (level_data[index] >> mod) & LEVEL_MASK;
  11. }
  12. void set_log_level(int mod, int l){
  13. int index = mod_bit_start(mod)/(32+1);
  14. mod = mod_bit_start(mod) % 32;
  15. level_data[index] = (level_data[index] & (~(LEVEL_MASK<<mod))) | ((l & LEVEL_MASK)<<mod);
  16. }
  17. static void log_out(char *fmt, va_list args){
  18. vprintf(fmt, args);
  19. }
  20. void log_debug(int mod, char *fmt, ...){
  21. if (_get_level(mod) >= L_debug){
  22. va_list args;
  23. va_start(args, fmt);
  24. log_out(fmt, args);
  25. va_end(args);
  26. }
  27. }
  28. void log_warning(int mod, char *fmt, ...){
  29. if (_get_level(mod) >= L_warning){
  30. va_list args;
  31. va_start(args, fmt);
  32. log_out(fmt, args);
  33. va_end(args);
  34. }
  35. }
  36. void log_error(int mod, char *fmt, ...){
  37. if (_get_level(mod) >= L_error){
  38. va_list args;
  39. va_start(args, fmt);
  40. log_out(fmt, args);
  41. va_end(args);
  42. }
  43. }
  44. //rewrite the fputc, so that the printf,vprintf can log the info the can
  45. static uint8_t log_buffer[64];
  46. static int log_index = 0;
  47. int fputc(int c, FILE *fp){
  48. if (c == '\n'){ //don't send '\n'
  49. if (log_index != 0){
  50. protocol_send_debug_info(0x72, log_buffer, log_index);
  51. }
  52. log_index = 0;
  53. }else if (log_index == 64){
  54. protocol_send_debug_info(0x70 ,log_buffer, log_index);
  55. log_index = 0;
  56. log_buffer[log_index++] = c;
  57. }else{
  58. log_buffer[log_index++] = c;
  59. }
  60. return 1;
  61. }