log_syslog.c 2.7 KB

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