log_syslog.c 2.6 KB

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