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.
19 #include <libubox/blobmsg_json.h>
20 #include <libubox/uloop.h>
32 static const struct blobmsg_policy log_policy
[] = {
33 [LOG_MSG
] = { .name
= "msg", .type
= BLOBMSG_TYPE_STRING
},
34 [LOG_ID
] = { .name
= "id", .type
= BLOBMSG_TYPE_INT32
},
35 [LOG_PRIO
] = { .name
= "priority", .type
= BLOBMSG_TYPE_INT32
},
36 [LOG_SOURCE
] = { .name
= "source", .type
= BLOBMSG_TYPE_INT32
},
37 [LOG_TIME
] = { .name
= "time", .type
= BLOBMSG_TYPE_INT64
},
46 static struct ubus_subscriber log_event
;
48 static void log_handle_remove(struct ubus_context
*ctx
, struct ubus_subscriber
*s
,
51 fprintf(stderr
, "Object %08x went away\n", id
);
54 static int log_notify(struct ubus_context
*ctx
, struct ubus_object
*obj
,
55 struct ubus_request_data
*req
, const char *method
,
56 struct blob_attr
*msg
)
58 struct blob_attr
*tb
[__LOG_MAX
];
63 blobmsg_parse(log_policy
, ARRAY_SIZE(log_policy
), tb
, blob_data(msg
), blob_len(msg
));
64 if (!tb
[LOG_ID
] || !tb
[LOG_PRIO
] || !tb
[LOG_SOURCE
] || !tb
[LOG_TIME
])
67 t
= blobmsg_get_u64(tb
[LOG_TIME
]) / 1000;
69 c
[strlen(c
) - 1] = '\0';
70 str
= blobmsg_format_json(msg
, true);
71 printf("%s - %s: %s\n",
72 c
, (blobmsg_get_u32(tb
[LOG_SOURCE
])) ? ("syslog") : ("kernel"), method
);
78 static void follow_log(struct ubus_context
*ctx
, int id
)
85 log_event
.remove_cb
= log_handle_remove
;
86 log_event
.cb
= log_notify
;
87 ret
= ubus_register_subscriber(ctx
, &log_event
);
89 fprintf(stderr
, "Failed to add watch handler: %s\n", ubus_strerror(ret
));
91 ret
= ubus_subscribe(ctx
, &log_event
, id
);
93 fprintf(stderr
, "Failed to add watch handler: %s\n", ubus_strerror(ret
));
105 static const struct blobmsg_policy read_policy
[] = {
106 [READ_LINE
] = { .name
= "lines", .type
= BLOBMSG_TYPE_ARRAY
},
109 static void read_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
111 struct blob_attr
*cur
;
112 struct blob_attr
*_tb
[__READ_MAX
];
119 blobmsg_parse(read_policy
, ARRAY_SIZE(read_policy
), _tb
, blob_data(msg
), blob_len(msg
));
122 blobmsg_for_each_attr(cur
, _tb
[READ_LINE
], rem
) {
123 struct blob_attr
*tb
[__LOG_MAX
];
126 if (blobmsg_type(cur
) != BLOBMSG_TYPE_TABLE
)
129 blobmsg_parse(log_policy
, ARRAY_SIZE(log_policy
), tb
, blobmsg_data(cur
), blobmsg_data_len(cur
));
130 if (!tb
[LOG_MSG
] || !tb
[LOG_ID
] || !tb
[LOG_PRIO
] || !tb
[LOG_SOURCE
] || !tb
[LOG_TIME
])
133 t
= blobmsg_get_u64(tb
[LOG_TIME
]) / 1000;
135 c
[strlen(c
) - 1] = '\0';
136 printf("%s - %s: %s\n",
137 c
, (blobmsg_get_u32(tb
[LOG_SOURCE
])) ? ("syslog") : ("kernel"),
138 blobmsg_get_string(tb
[LOG_MSG
]));
142 static int usage(const char *prog
)
144 fprintf(stderr
, "Usage: %s [options]\n"
146 " -s <path> Path to ubus socket\n"
147 " -l <count> Got only the last 'count' messages\n"
148 " -f Follow log messages\n"
153 int main(int argc
, char **argv
)
155 struct ubus_context
*ctx
;
157 const char *ubus_socket
= NULL
;
158 int ch
, ret
, subscribe
= 0, lines
= 0;
159 static struct blob_buf b
;
161 while ((ch
= getopt(argc
, argv
, "fs:l:")) != -1) {
164 ubus_socket
= optarg
;
170 lines
= atoi(optarg
);
178 ctx
= ubus_connect(ubus_socket
);
180 fprintf(stderr
, "Failed to connect to ubus\n");
184 ret
= ubus_lookup_id(ctx
, "log", &id
);
186 fprintf(stderr
, "Failed to find log object: %s\n", ubus_strerror(ret
));
188 if (!subscribe
|| lines
) {
189 blob_buf_init(&b
, 0);
191 blobmsg_add_u32(&b
, "lines", lines
);
192 ubus_invoke(ctx
, id
, "read", b
.head
, read_cb
, 0, 3000);