RyanMqttLog.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "RyanMqttLog.h"
  2. /**
  3. * @brief Outputs a formatted log message with color, log level, file, and line information.
  4. *
  5. * Constructs a log message that includes an ANSI color code, log level label, source file name, and line number, followed by a user-formatted message. The output is color-coded and terminated with a reset sequence and newline, then sent to the platform-specific output function.
  6. *
  7. * @param lvl Log level label (e.g., "INFO", "ERROR").
  8. * @param color_n ANSI color code for the log message.
  9. * @param fileStr Source file name.
  10. * @param lineNum Source code line number.
  11. * @param fmt Format string for the user message, followed by variable arguments.
  12. */
  13. void rlog_output(char *lvl, uint8_t color_n, char *fileStr, uint32_t lineNum, char *const fmt, ...)
  14. {
  15. // RyanLogPrintf("\033[字背景颜色;字体颜色m 用户字符串 \033[0m" );
  16. char dbgBuffer[256];
  17. uint16_t len = 0;
  18. // 打印颜色、提示符、打印文件路径、行号
  19. len += snprintf(dbgBuffer + len, sizeof(dbgBuffer) - len, "\033[%dm[%s] %s:%d ", color_n, lvl, fileStr, lineNum);
  20. // platformPrint(dbgBuffer, len);
  21. // len = 0;
  22. // 打印用户输入
  23. va_list args;
  24. va_start(args, fmt);
  25. len += vsnprintf(dbgBuffer + len, sizeof(dbgBuffer) - len, fmt, args);
  26. va_end(args);
  27. // 打印颜色
  28. len += snprintf(dbgBuffer + len, sizeof(dbgBuffer) - len, "\033[0m\r\n");
  29. platformPrint(dbgBuffer, len);
  30. }
  31. /**
  32. * @brief Outputs a formatted string directly to the platform output.
  33. *
  34. * Formats the input string with variable arguments and sends it to the platform output function without adding any metadata or color formatting.
  35. *
  36. * @param fmt Format string for the message, followed by variable arguments.
  37. */
  38. void rlog_output_raw(char *const fmt, ...)
  39. {
  40. char dbgBuffer[256];
  41. uint16_t len;
  42. va_list args;
  43. va_start(args, fmt);
  44. len = vsnprintf(dbgBuffer, sizeof(dbgBuffer), fmt, args);
  45. va_end(args);
  46. platformPrint(dbgBuffer, len);
  47. }