syslog.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #pragma GCC diagnostic error "-Wall"
  2. #pragma GCC diagnostic error "-Wextra"
  3. #if defined(HARDWARE_BT6711) && !defined(BT6702_SERVICE)
  4. #include "common_config.h"
  5. #include "syslog.h"
  6. #include "settings_api.h"
  7. #include "tcpip.h"
  8. #include "udp.h"
  9. #include "rtc.h"
  10. static struct udp_pcb *upcb;
  11. // TODO either lock the buffers against race conditions or raise the task stack sizes, or lower the memory consumption
  12. static char packet[256];
  13. static char msg[200];
  14. void openlog(void)
  15. {
  16. upcb = udp_new();
  17. udp_bind(upcb, IP_ADDR_ANY, 0);
  18. }
  19. static void timestamp_rfc3339(char *ts)
  20. {
  21. TM_RTC_t data;
  22. uint16_t sys_year;
  23. TM_RTC_GetDateTime(&data, TM_RTC_Format_BIN);
  24. sys_year = 2000 + data.year;
  25. uint32_t subseconds = (1024 - data.subseconds) * 999999 / 1024;
  26. // TODO timezone?
  27. sprintf(ts, "%04i-%02i-%02iT%02i:%02i:%02i.%06luZ", sys_year, data.month, data.date, data.hours, data.minutes, data.seconds, subseconds);
  28. }
  29. void syslog(char *fmt, ...)
  30. {
  31. //char msg[200]; // arbitrary length; "Any transport receiver MUST be able to accept messages of up to and including 480 octets in length."
  32. va_list va;
  33. va_start(va, fmt);
  34. vsnprintf(msg, sizeof(msg), fmt, va);
  35. syslog_str(msg);
  36. va_end(va);
  37. }
  38. void syslog_str(char *msg)
  39. {
  40. uint8_t priority = 13;
  41. #define SYSLOG_VERSION "1" // as defined in RFC5424
  42. #define BOM "\xef\xbb\xbf"
  43. struct pbuf* psend;
  44. // TODO to reduce memory consumption one can use a scatter-gather I/O instead of packet[]
  45. //char packet[256]; // arbitrary length; "Any transport receiver MUST be able to accept messages of up to and including 480 octets in length."
  46. char timestamp[30] = "-";
  47. timestamp_rfc3339(timestamp);
  48. unsigned len = snprintf(packet, sizeof(packet), "<%u>" SYSLOG_VERSION " %s - " HW_REV "_" VERSION " - - - " BOM "%s", priority, timestamp, msg);
  49. //psend = pbuf_alloc(PBUF_RAW, sizeof(packet), PBUF_REF);
  50. psend = pbuf_alloc(PBUF_RAW, len, PBUF_REF);
  51. psend->payload = packet;
  52. //psend->len = len;
  53. udp_sendto(upcb, psend, &sSettings.sSyslog.server_ip, sSettings.sSyslog.server_port);
  54. pbuf_free(psend);
  55. }
  56. #endif // defined(HARDWARE_BT6711) && !defined(BT6702_SERVICE)