log.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. #define HLOG_INFO "Hlog "
  9. static char logToOutterFlag=0;
  10. static char logToUsbFlag=0;
  11. #if defined NEED_TIMESTEMP
  12. #define HEAD_PART_LEN (35+2) //根据以下的snprintf(pinfo 的长度计算 再加回车换行
  13. char timestp[25];
  14. char *timeStmp(void){
  15. MY_CLOCK myclock;
  16. getMyClock(&myclock);
  17. snprintf(timestp, sizeof(timestp),"%02d-%02d-%02d %02d:%02d:%02d", myclock.year%100, myclock.month, myclock.day, myclock.hour, myclock.min, myclock.sec);
  18. return timestp;
  19. }
  20. #else
  21. #define HEAD_PART_LEN (16+2) //根据以下的snprintf(pinfo 的长度计算 再加回车换行
  22. #endif
  23. #define MAX_LOG_INFO_LEN 100 //日志打印的用户段最大有效长度
  24. void wjlprintf(const char *level,const char *format,...){
  25. va_list vags;
  26. char pinfo[MAX_LOG_INFO_LEN+HEAD_PART_LEN],info[MAX_LOG_INFO_LEN];
  27. if(logToOutterFlag==0 && logToUsbFlag==0) return;
  28. #if defined NEED_TIMESTEMP
  29. 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
  30. #else
  31. snprintf(pinfo, sizeof(pinfo), "%s%s:",HLOG_INFO,level);//"Hlog info/warn/error" max 10, make it 16 is safe
  32. #endif
  33. va_start(vags, format);
  34. vsnprintf(info, sizeof(info),format, vags);
  35. va_end(vags);
  36. strcat(pinfo, info);
  37. strcat(pinfo,"\r\n");
  38. if(0!=logToUsbFlag) ON_SHOW_INFO(pinfo);
  39. if(0!=logToOutterFlag) logToOutter(pinfo);
  40. #if defined LOG_TO_WIN
  41. fflush(stdout);
  42. #endif
  43. }
  44. void setLogRoute(char typeUsb, char typeOutter){
  45. logToOutterFlag=typeOutter;
  46. logToUsbFlag=typeUsb;
  47. }