log_syslog.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Log to syslog.
  4. *
  5. * Copyright (c) 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
  6. */
  7. #include <common.h>
  8. #include <log.h>
  9. #include <net.h>
  10. DECLARE_GLOBAL_DATA_PTR;
  11. #define BUFFER_SIZE 480
  12. static void append(char **buf, char *buf_end, const char *fmt, ...)
  13. {
  14. va_list args;
  15. size_t size = buf_end - *buf;
  16. va_start(args, fmt);
  17. vsnprintf(*buf, size, fmt, args);
  18. va_end(args);
  19. *buf += strlen(*buf);
  20. }
  21. static int log_syslog_emit(struct log_device *ldev, struct log_rec *rec)
  22. {
  23. int ret;
  24. int fmt = gd->log_fmt;
  25. char msg[BUFFER_SIZE];
  26. char *msg_end = msg + BUFFER_SIZE;
  27. char *ptr = msg;
  28. char *iphdr;
  29. char *log_msg;
  30. int eth_hdr_size;
  31. struct in_addr bcast_ip;
  32. unsigned int log_level;
  33. char *log_hostname;
  34. /* Setup packet buffers */
  35. ret = net_init();
  36. if (ret)
  37. return ret;
  38. /* Disable hardware and put it into the reset state */
  39. eth_halt();
  40. /* Set current device according to environment variables */
  41. eth_set_current();
  42. /* Get hardware ready for send and receive operations */
  43. ret = eth_init();
  44. if (ret < 0) {
  45. eth_halt();
  46. goto out;
  47. }
  48. memset(msg, 0, BUFFER_SIZE);
  49. /* Set ethernet header */
  50. eth_hdr_size = net_set_ether((uchar *)ptr, net_bcast_ethaddr, PROT_IP);
  51. ptr += eth_hdr_size;
  52. iphdr = ptr;
  53. ptr += IP_UDP_HDR_SIZE;
  54. log_msg = ptr;
  55. /*
  56. * The syslog log levels defined in RFC 5424 match the U-Boot ones up to
  57. * level 7 (debug).
  58. */
  59. log_level = rec->level;
  60. if (log_level > 7)
  61. log_level = 7;
  62. /* Leave high bits as 0 to write a 'kernel message' */
  63. /* Write log message to buffer */
  64. append(&ptr, msg_end, "<%u>", log_level);
  65. log_hostname = env_get("log_hostname");
  66. if (log_hostname)
  67. append(&ptr, msg_end, "%s ", log_hostname);
  68. append(&ptr, msg_end, "uboot: ");
  69. if (fmt & BIT(LOGF_LEVEL))
  70. append(&ptr, msg_end, "%s.",
  71. log_get_level_name(rec->level));
  72. if (fmt & BIT(LOGF_CAT))
  73. append(&ptr, msg_end, "%s,",
  74. log_get_cat_name(rec->cat));
  75. if (fmt & BIT(LOGF_FILE))
  76. append(&ptr, msg_end, "%s:", rec->file);
  77. if (fmt & BIT(LOGF_LINE))
  78. append(&ptr, msg_end, "%d-", rec->line);
  79. if (fmt & BIT(LOGF_FUNC))
  80. append(&ptr, msg_end, "%s()", rec->func);
  81. if (fmt & BIT(LOGF_MSG))
  82. append(&ptr, msg_end, "%s%s",
  83. fmt != BIT(LOGF_MSG) ? " " : "", rec->msg);
  84. /* Consider trailing 0x00 */
  85. ptr++;
  86. debug("log message: '%s'\n", log_msg);
  87. /* Broadcast message */
  88. bcast_ip.s_addr = 0xFFFFFFFFL;
  89. net_set_udp_header((uchar *)iphdr, bcast_ip, 514, 514, ptr - log_msg);
  90. net_send_packet((uchar *)msg, ptr - msg);
  91. out:
  92. return ret;
  93. }
  94. LOG_DRIVER(syslog) = {
  95. .name = "syslog",
  96. .emit = log_syslog_emit,
  97. };