log_syslog.c 2.7 KB

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