log.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "log.h"
  2. #include "platform.h"
  3. #include "common.h"
  4. #include <stdio.h>
  5. #include <stdarg.h>
  6. #include <string.h>
  7. #include "pt-timer.h"
  8. #include "app.h"
  9. static char logToOutterFlag=0;//默认不打log,发RT+ROUTE=0,1 开启打印LOG
  10. static char logToUsbFlag=1;
  11. //#define NEED_TIMESTEMP //打印添加时间
  12. #if defined NEED_TIMESTEMP
  13. #define HEAD_PART_LEN (35+2) //根据以下的snprintf(pinfo 的长度计算 再加回车换行
  14. char timestp[25];
  15. char *timeStmp(void){
  16. MY_CLOCK myclock;
  17. getMyClock(&myclock);
  18. snprintf(timestp, sizeof(timestp),"%02d-%02d-%02d %02d:%02d:%02d", myclock.year%100, myclock.month, myclock.day, myclock.hour, myclock.min, myclock.sec);
  19. return timestp;
  20. }
  21. #else
  22. #define HEAD_PART_LEN (16+2) //根据以下的snprintf(pinfo 的长度计算 再加回车换行
  23. #endif
  24. #define MAX_LOG_INFO_LEN 200 //日志打印的用户段最大有效长度
  25. void wjlprintf(const char *level,const char *format,...){
  26. va_list vags;
  27. char pinfo[MAX_LOG_INFO_LEN+HEAD_PART_LEN],info[MAX_LOG_INFO_LEN];
  28. if(TRUE==app.tmMode || TRUE==app.tgMode) return;//TM/TG模式下不打印
  29. if(logToOutterFlag==0 && logToUsbFlag==0) return;//禁止打印直接返回,省事
  30. #if defined NEED_TIMESTEMP
  31. snprintf(pinfo, sizeof(pinfo), "<%s>[%s %03d]:",level,timeStmp(),getSysTick()%1000); //"error[20-20-20 20:20:20 000]:" max 29, make it 35 is safe
  32. #else
  33. snprintf(pinfo, sizeof(pinfo), "[%s]",level);//"Hlog info/warn/error" max 10, make it 16 is safe
  34. #endif
  35. va_start(vags, format);
  36. vsnprintf(info, sizeof(info),format, vags);
  37. va_end(vags);
  38. strcat(pinfo, info);
  39. strcat(pinfo,"\r\n");
  40. if(0!=logToUsbFlag) ON_SHOW_INFO(pinfo);
  41. #ifndef ENABLE_UART1_MCU_CMD_ONLY
  42. if(0!=logToOutterFlag) logToOutter(pinfo);
  43. #endif
  44. #ifdef ENABLE_USB_PORT
  45. if(0!=logToOutterFlag) logToOutter(pinfo);
  46. #endif
  47. #if defined LOG_TO_WIN
  48. fflush(stdout);
  49. #endif
  50. }
  51. void setLogRoute(char typeUsb, char typeOutter){
  52. logToOutterFlag=typeOutter;
  53. logToUsbFlag=typeUsb;
  54. }