logger.c 1.6 KB

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