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