2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
22 #include <libubox/blobmsg_json.h>
23 #include <libubox/usock.h>
24 #include <libubox/uloop.h>
36 static const struct blobmsg_policy log_policy[] = {
37 [LOG_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
38 [LOG_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
39 [LOG_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
40 [LOG_SOURCE] = { .name = "source", .type = BLOBMSG_TYPE_INT32 },
41 [LOG_TIME] = { .name = "time", .type = BLOBMSG_TYPE_INT64 },
50 static struct ubus_subscriber log_event;
51 static struct uloop_fd sender;
53 static void log_handle_remove(struct ubus_context *ctx, struct ubus_subscriber *s,
56 fprintf(stderr, "Object %08x went away\n", id);
59 static int log_notify(struct ubus_context *ctx, struct ubus_object *obj,
60 struct ubus_request_data *req, const char *method,
61 struct blob_attr *msg)
63 struct blob_attr *tb[__LOG_MAX];
70 blobmsg_parse(log_policy, ARRAY_SIZE(log_policy), tb, blob_data(msg), blob_len(msg));
71 if (!tb[LOG_ID] || !tb[LOG_PRIO] || !tb[LOG_SOURCE] || !tb[LOG_TIME])
74 t = blobmsg_get_u64(tb[LOG_TIME]) / 1000;
76 p = blobmsg_get_u32(tb[LOG_PRIO]);
77 c[strlen(c) - 1] = '\0';
78 str = blobmsg_format_json(msg, true);
79 snprintf(buf, sizeof(buf), "%s %s.%s%s %s\n",
80 c, facilitynames[LOG_FAC(p)].c_name, prioritynames[LOG_PRI(p)].c_name,
81 (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"),
83 write(sender.fd, buf, strlen(buf));
90 static void follow_log(struct ubus_context *ctx, int id, const char *url, const char *port)
97 log_event.remove_cb = log_handle_remove;
98 log_event.cb = log_notify;
99 ret = ubus_register_subscriber(ctx, &log_event);
101 fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret));
103 ret = ubus_subscribe(ctx, &log_event, id);
105 fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret));
108 sender.fd = usock(USOCK_TCP | USOCK_NUMERIC, url, port);
110 fprintf(stderr, "failed to connect: %s\n", strerror(errno));
113 uloop_fd_add(&sender, ULOOP_READ);
116 sender.fd = STDOUT_FILENO;
129 static const struct blobmsg_policy read_policy[] = {
130 [READ_LINE] = { .name = "lines", .type = BLOBMSG_TYPE_ARRAY },
133 static void read_cb(struct ubus_request *req, int type, struct blob_attr *msg)
135 struct blob_attr *cur;
136 struct blob_attr *_tb[__READ_MAX];
143 blobmsg_parse(read_policy, ARRAY_SIZE(read_policy), _tb, blob_data(msg), blob_len(msg));
146 blobmsg_for_each_attr(cur, _tb[READ_LINE], rem) {
147 struct blob_attr *tb[__LOG_MAX];
151 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE)
154 blobmsg_parse(log_policy, ARRAY_SIZE(log_policy), tb, blobmsg_data(cur), blobmsg_data_len(cur));
155 if (!tb[LOG_MSG] || !tb[LOG_ID] || !tb[LOG_PRIO] || !tb[LOG_SOURCE] || !tb[LOG_TIME])
158 t = blobmsg_get_u64(tb[LOG_TIME]);
159 p = blobmsg_get_u32(tb[LOG_PRIO]);
161 c[strlen(c) - 1] = '\0';
163 printf("%s %s.%s%s %s\n",
164 c, facilitynames[LOG_FAC(p)].c_name, prioritynames[LOG_PRI(p)].c_name,
165 (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"),
166 blobmsg_get_string(tb[LOG_MSG]));
170 static int usage(const char *prog)
172 fprintf(stderr, "Usage: %s [options]\n"
174 " -s <path> Path to ubus socket\n"
175 " -l <count> Got only the last 'count' messages\n"
176 " -r <server> <port> Stream message to a server\n"
177 " -f Follow log messages\n"
182 int main(int argc, char **argv)
184 struct ubus_context *ctx;
186 const char *ubus_socket = NULL, *url = NULL, *port = NULL;
187 int ch, ret, subscribe = 0, lines = 0;
188 static struct blob_buf b;
190 while ((ch = getopt(argc, argv, "fs:l:r:")) != -1) {
193 ubus_socket = optarg;
197 port = argv[optind++];
203 lines = atoi(optarg);
210 ctx = ubus_connect(ubus_socket);
212 fprintf(stderr, "Failed to connect to ubus\n");
216 ret = ubus_lookup_id(ctx, "log", &id);
218 fprintf(stderr, "Failed to find log object: %s\n", ubus_strerror(ret));
220 if (!subscribe || lines) {
221 blob_buf_init(&b, 0);
223 blobmsg_add_u32(&b, "lines", lines);
224 ubus_invoke(ctx, id, "read", b.head, read_cb, 0, 3000);
228 follow_log(ctx, id, url, port);