2 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
17 #include <linux/types.h>
19 #include <libubox/uloop.h>
20 #include <libubox/blobmsg.h>
27 static struct blob_buf b;
28 static struct ubus_auto_conn conn;
30 static const struct blobmsg_policy read_policy =
31 { .name = "lines", .type = BLOBMSG_TYPE_INT32 };
33 static const struct blobmsg_policy write_policy =
34 { .name = "event", .type = BLOBMSG_TYPE_STRING };
37 read_log(struct ubus_context *ctx, struct ubus_object *obj,
38 struct ubus_request_data *req, const char *method,
39 struct blob_attr *msg)
47 blobmsg_parse(&read_policy, 1, &tb, blob_data(msg), blob_len(msg));
49 count = blobmsg_get_u32(tb);
53 lines = blobmsg_open_array(&b, "lines");
55 l = log_list(count, NULL);
58 entry = blobmsg_open_table(&b, NULL);
59 blobmsg_add_string(&b, "msg", l->data);
60 blobmsg_add_u32(&b, "id", l->id);
61 blobmsg_add_u32(&b, "priority", l->priority);
62 blobmsg_add_u32(&b, "source", l->source);
63 blobmsg_add_u64(&b, "time", l->ts.tv_sec);
64 blobmsg_close_table(&b, entry);
65 l = log_list(count, l);
67 blobmsg_close_table(&b, lines);
68 ubus_send_reply(ctx, req, b.head);
74 write_log(struct ubus_context *ctx, struct ubus_object *obj,
75 struct ubus_request_data *req, const char *method,
76 struct blob_attr *msg)
82 blobmsg_parse(&write_policy, 1, &tb, blob_data(msg), blob_len(msg));
84 event = blobmsg_get_string(tb);
85 log_add(event, strlen(event) + 1, SOURCE_SYSLOG);
93 log_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
95 notify = obj->has_subscribers;
98 static const struct ubus_method log_methods[] = {
99 { .name = "read", .handler = read_log, .policy = &read_policy, .n_policy = 1 },
100 { .name = "write", .handler = write_log, .policy = &write_policy, .n_policy = 1 },
103 static struct ubus_object_type log_object_type =
104 UBUS_OBJECT_TYPE("log", log_methods);
106 static struct ubus_object log_object = {
108 .type = &log_object_type,
109 .methods = log_methods,
110 .n_methods = ARRAY_SIZE(log_methods),
111 .subscribe_cb = log_subscribe_cb,
115 ubus_notify_log(struct log_head *l)
122 blob_buf_init(&b, 0);
123 blobmsg_add_u32(&b, "id", l->id);
124 blobmsg_add_u32(&b, "priority", l->priority);
125 blobmsg_add_u32(&b, "source", l->source);
126 blobmsg_add_u64(&b, "time", (((__u64) l->ts.tv_sec) * 1000) + (l->ts.tv_nsec / 1000000));
128 ret = ubus_notify(&conn.ctx, &log_object, l->data, b.head, -1);
130 fprintf(stderr, "Failed to notify log: %s\n", ubus_strerror(ret));
134 ubus_connect_handler(struct ubus_context *ctx)
138 ret = ubus_add_object(ctx, &log_object);
140 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
141 fprintf(stderr, "log: connected to ubus\n");
145 main(int argc, char **argv)
147 signal(SIGPIPE, SIG_IGN);
151 conn.cb = ubus_connect_handler;
152 ubus_auto_connect(&conn);