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.
15 #include <linux/types.h>
17 #include <libubox/uloop.h>
18 #include <libubox/blobmsg_json.h>
24 struct ubus_context *_ctx;
25 static struct blob_buf b;
27 static const struct blobmsg_policy read_policy =
28 { .name = "lines", .type = BLOBMSG_TYPE_INT32 };
30 static const struct blobmsg_policy write_policy =
31 { .name = "event", .type = BLOBMSG_TYPE_STRING };
33 static int read_log(struct ubus_context *ctx, struct ubus_object *obj,
34 struct ubus_request_data *req, const char *method,
35 struct blob_attr *msg)
43 blobmsg_parse(&read_policy, 1, &tb, blob_data(msg), blob_len(msg));
45 count = blobmsg_get_u32(tb);
49 lines = blobmsg_open_array(&b, "lines");
51 l = log_list(count, NULL);
54 entry = blobmsg_open_table(&b, NULL);
55 blobmsg_add_string(&b, "msg", l->data);
56 blobmsg_add_u32(&b, "id", l->id);
57 blobmsg_add_u32(&b, "priority", l->priority);
58 blobmsg_add_u32(&b, "source", l->source);
59 blobmsg_add_u64(&b, "time", l->ts.tv_sec);
60 blobmsg_close_table(&b, entry);
61 l = log_list(count, l);
63 blobmsg_close_table(&b, lines);
64 ubus_send_reply(ctx, req, b.head);
69 static int write_log(struct ubus_context *ctx, struct ubus_object *obj,
70 struct ubus_request_data *req, const char *method,
71 struct blob_attr *msg)
77 blobmsg_parse(&write_policy, 1, &tb, blob_data(msg), blob_len(msg));
79 event = blobmsg_get_string(tb);
80 log_add(event, strlen(event) + 1, SOURCE_SYSLOG);
87 static void log_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
89 notify = obj->has_subscribers;
92 static const struct ubus_method log_methods[] = {
93 { .name = "read", .handler = read_log, .policy = &read_policy, .n_policy = 1 },
94 { .name = "write", .handler = write_log, .policy = &write_policy, .n_policy = 1 },
97 static struct ubus_object_type log_object_type =
98 UBUS_OBJECT_TYPE("log", log_methods);
100 static struct ubus_object log_object = {
102 .type = &log_object_type,
103 .methods = log_methods,
104 .n_methods = ARRAY_SIZE(log_methods),
105 .subscribe_cb = log_subscribe_cb,
108 void ubus_notify_log(struct log_head *l)
115 blob_buf_init(&b, 0);
116 blobmsg_add_u32(&b, "id", l->id);
117 blobmsg_add_u32(&b, "priority", l->priority);
118 blobmsg_add_u32(&b, "source", l->source);
119 blobmsg_add_u64(&b, "time", (((__u64) l->ts.tv_sec) * 1000) + (l->ts.tv_nsec / 1000000));
121 ret = ubus_notify(_ctx, &log_object, l->data, b.head, -1);
123 ERROR("Failed to notify log: %s\n", ubus_strerror(ret));
126 void ubus_init_log(struct ubus_context *ctx)
132 ret = ubus_add_object(ctx, &log_object);
134 ERROR("Failed to add object: %s\n", ubus_strerror(ret));