syslog.c 2.1 KB

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