log_syslog.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. net_init();
  36. /* Disable hardware and put it into the reset state */
  37. eth_halt();
  38. /* Set current device according to environment variables */
  39. eth_set_current();
  40. /* Get hardware ready for send and receive operations */
  41. ret = eth_init();
  42. if (ret < 0) {
  43. eth_halt();
  44. goto out;
  45. }
  46. memset(msg, 0, BUFFER_SIZE);
  47. /* Set ethernet header */
  48. eth_hdr_size = net_set_ether((uchar *)ptr, net_bcast_ethaddr, PROT_IP);
  49. ptr += eth_hdr_size;
  50. iphdr = ptr;
  51. ptr += IP_UDP_HDR_SIZE;
  52. log_msg = ptr;
  53. /*
  54. * The syslog log levels defined in RFC 5424 match the U-Boot ones up to
  55. * level 7 (debug).
  56. */
  57. log_level = rec->level;
  58. if (log_level > 7)
  59. log_level = 7;
  60. /* Leave high bits as 0 to write a 'kernel message' */
  61. /* Write log message to buffer */
  62. append(&ptr, msg_end, "<%u>", log_level);
  63. log_hostname = env_get("log_hostname");
  64. if (log_hostname)
  65. append(&ptr, msg_end, "%s ", log_hostname);
  66. append(&ptr, msg_end, "uboot: ");
  67. if (fmt & BIT(LOGF_LEVEL))
  68. append(&ptr, msg_end, "%s.",
  69. log_get_level_name(rec->level));
  70. if (fmt & BIT(LOGF_CAT))
  71. append(&ptr, msg_end, "%s,",
  72. log_get_cat_name(rec->cat));
  73. if (fmt & BIT(LOGF_FILE))
  74. append(&ptr, msg_end, "%s:", rec->file);
  75. if (fmt & BIT(LOGF_LINE))
  76. append(&ptr, msg_end, "%d-", rec->line);
  77. if (fmt & BIT(LOGF_FUNC))
  78. append(&ptr, msg_end, "%s()", rec->func);
  79. if (fmt & BIT(LOGF_MSG))
  80. append(&ptr, msg_end, "%s%s",
  81. fmt != BIT(LOGF_MSG) ? " " : "", rec->msg);
  82. /* Consider trailing 0x00 */
  83. ptr++;
  84. debug("log message: '%s'\n", log_msg);
  85. /* Broadcast message */
  86. bcast_ip.s_addr = 0xFFFFFFFFL;
  87. net_set_udp_header((uchar *)iphdr, bcast_ip, 514, 514, ptr - log_msg);
  88. net_send_packet((uchar *)msg, ptr - msg);
  89. out:
  90. return ret;
  91. }
  92. LOG_DRIVER(syslog) = {
  93. .name = "syslog",
  94. .emit = log_syslog_emit,
  95. };