RyanMqttLog.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #ifndef __RyanMqttLog__
  2. #define __RyanMqttLog__
  3. #include <stdio.h>
  4. #include <stdarg.h>
  5. #include "platformSystem.h"
  6. // 日志等级
  7. #define rlogLvlError 0
  8. #define rlogLvlWarning 1
  9. #define rlogLvlInfo 2
  10. #define rlogLvlDebug 3
  11. // 日志打印等级
  12. #ifndef rlogLevel
  13. #define rlogLevel (rlogLvlDebug)
  14. #endif
  15. // 日志tag
  16. #ifndef rlogTag
  17. #define rlogTag "LOG"
  18. #endif
  19. /**
  20. * @brief 日志相关
  21. *
  22. */
  23. #ifdef rlogEnable
  24. static void rlog_output(char *lvl, uint8_t color_n, char *fileStr, uint32_t lineNum, char *const fmt, ...)
  25. {
  26. // RyanLogPrintf("\033[字背景颜色;字体颜色m 用户字符串 \033[0m" );
  27. char dbgBuffer[256] = {0};
  28. uint16_t len = 0;
  29. // 打印颜色
  30. #ifdef rlogColorEnable
  31. len += snprintf(dbgBuffer + len, sizeof(dbgBuffer) - len, "\033[%dm", color_n);
  32. #endif
  33. // 打印提示符
  34. len += snprintf(dbgBuffer + len, sizeof(dbgBuffer) - len, "[%s/%s]", lvl, rlogTag);
  35. // 打印文件路径和行号
  36. len += snprintf(dbgBuffer + len, sizeof(dbgBuffer) - len, " %s:%d ", fileStr, lineNum);
  37. platformPrint(dbgBuffer, len);
  38. len = 0;
  39. // 打印用户输入
  40. va_list args;
  41. va_start(args, fmt);
  42. len += vsnprintf(dbgBuffer + len, sizeof(dbgBuffer) - len, fmt, args);
  43. va_end(args);
  44. // 打印颜色
  45. #ifdef rlogColorEnable
  46. len += snprintf(dbgBuffer + len, sizeof(dbgBuffer) - len, "\033[0m");
  47. #endif
  48. len += snprintf(dbgBuffer + len, sizeof(dbgBuffer) - len, "\r\n");
  49. platformPrint(dbgBuffer, len);
  50. }
  51. static void rlog_output_raw(char *const fmt, ...)
  52. {
  53. char dbgBuffer[256];
  54. uint16_t len;
  55. va_list args;
  56. va_start(args, fmt);
  57. len = vsnprintf(dbgBuffer, sizeof(dbgBuffer), fmt, args);
  58. va_end(args);
  59. platformPrint(dbgBuffer, len);
  60. }
  61. #else
  62. #define rlog_output(...)
  63. #define rlog_output_raw(...)
  64. #endif
  65. /**
  66. * @brief log等级检索
  67. *
  68. */
  69. #if (rlogLevel >= rlogLvlDebug)
  70. #define rlog_d(fmt, ...) rlog_output("D", 0, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
  71. #else
  72. #define rlog_d(...)
  73. #endif
  74. #if (rlogLevel >= rlogLvlInfo)
  75. #define rlog_i(fmt, ...) rlog_output("I", 32, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
  76. #else
  77. #define rlog_i(...)
  78. #endif
  79. #if (rlogLevel >= rlogLvlWarning)
  80. #define rlog_w(fmt, ...) rlog_output("W", 33, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
  81. #else
  82. #define rlog_w(...)
  83. #endif
  84. #if (rlogLevel >= rlogLvlError)
  85. #define rlog_e(fmt, ...) rlog_output("E", 31, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
  86. #else
  87. #define rlog_e(...)
  88. #endif
  89. #define rlog_raw(...) rlog_output_raw(__VA_ARGS__)
  90. #endif