log.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. #ifndef ENABLE_OPENHPOC
  39. if(0!=logToUsbFlag) ON_SHOW_INFO(pinfo);
  40. #endif
  41. if(0!=logToOutterFlag) logToOutter(pinfo);
  42. #if defined LOG_TO_WIN
  43. fflush(stdout);
  44. #endif
  45. }
  46. void setLogRoute(char typeUsb, char typeOutter){
  47. logToOutterFlag=typeOutter;
  48. logToUsbFlag=typeUsb;
  49. }