logger.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. void log_disable_all(void){
  18. level_data[0] = 0;
  19. level_data[1] = 0;
  20. }
  21. static void log_out(char *fmt, va_list args){
  22. vprintf(fmt, args);
  23. }
  24. void log_debug(int mod, char *fmt, ...){
  25. if (_get_level(mod) >= L_debug){
  26. va_list args;
  27. va_start(args, fmt);
  28. log_out(fmt, args);
  29. va_end(args);
  30. }
  31. }
  32. void log_warning(int mod, char *fmt, ...){
  33. if (_get_level(mod) >= L_warning){
  34. va_list args;
  35. va_start(args, fmt);
  36. log_out(fmt, args);
  37. va_end(args);
  38. }
  39. }
  40. void log_error(int mod, char *fmt, ...){
  41. if (_get_level(mod) >= L_error){
  42. va_list args;
  43. va_start(args, fmt);
  44. log_out(fmt, args);
  45. va_end(args);
  46. }
  47. }
  48. //rewrite the fputc, so that the printf,vprintf can log the info the can
  49. static uint8_t log_buffer[64];
  50. static int log_index = 0;
  51. int fputc(int c, FILE *fp){
  52. if (c == '\n'){ //don't send '\n'
  53. if (log_index != 0){
  54. protocol_send_debug_info(0x72, log_buffer, log_index);
  55. }
  56. log_index = 0;
  57. }else if (log_index == 64){
  58. protocol_send_debug_info(0x70 ,log_buffer, log_index);
  59. log_index = 0;
  60. log_buffer[log_index++] = c;
  61. }else{
  62. log_buffer[log_index++] = c;
  63. }
  64. return 1;
  65. }