X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fprocd.git;a=blobdiff_plain;f=logread.c;h=5d1be68cef0c34fdd30b83290d8520dc4e1798ab;hp=b7f9ebeebae9223d11dc50061530540708b942ad;hb=3018420f7fd004b48715100d2f60b27c64d48b75;hpb=cbf7009718dd00ab1a782532f54ea3f08796f553 diff --git a/logread.c b/logread.c index b7f9ebe..5d1be68 100644 --- a/logread.c +++ b/logread.c @@ -16,7 +16,11 @@ #include #include +#define SYSLOG_NAMES +#include + #include +#include #include #include "libubus.h" @@ -44,6 +48,7 @@ enum { }; static struct ubus_subscriber log_event; +static struct uloop_fd sender; static void log_handle_remove(struct ubus_context *ctx, struct ubus_subscriber *s, uint32_t id) @@ -56,6 +61,8 @@ static int log_notify(struct ubus_context *ctx, struct ubus_object *obj, struct blob_attr *msg) { struct blob_attr *tb[__LOG_MAX]; + char buf[256]; + uint32_t p; char *str; time_t t; char *c; @@ -66,16 +73,21 @@ static int log_notify(struct ubus_context *ctx, struct ubus_object *obj, t = blobmsg_get_u64(tb[LOG_TIME]) / 1000; c = ctime(&t); + p = blobmsg_get_u32(tb[LOG_PRIO]); c[strlen(c) - 1] = '\0'; str = blobmsg_format_json(msg, true); - printf("%s - %s: %s\n", - c, (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("syslog") : ("kernel"), method); + snprintf(buf, sizeof(buf), "%s %s.%s%s %s\n", + c, facilitynames[LOG_FAC(p)].c_name, prioritynames[LOG_PRI(p)].c_name, + (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"), + method); + write(sender.fd, buf, strlen(buf)); + free(str); return 0; } -static void follow_log(struct ubus_context *ctx, int id) +static void follow_log(struct ubus_context *ctx, int id, const char *url, const char *port) { int ret; @@ -92,6 +104,18 @@ static void follow_log(struct ubus_context *ctx, int id) if (ret) fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret)); + if (url && port) { + sender.fd = usock(USOCK_TCP | USOCK_NUMERIC, url, port); + if (sender.fd < 0) { + fprintf(stderr, "failed to connect: %s\n", strerror(errno)); + exit(-1); + } else { + uloop_fd_add(&sender, ULOOP_READ); + } + } else { + sender.fd = STDOUT_FILENO; + } + uloop_run(); ubus_free(ctx); uloop_done(); @@ -121,6 +145,7 @@ static void read_cb(struct ubus_request *req, int type, struct blob_attr *msg) return; blobmsg_for_each_attr(cur, _tb[READ_LINE], rem) { struct blob_attr *tb[__LOG_MAX]; + uint32_t p; char *c; if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) @@ -130,11 +155,14 @@ static void read_cb(struct ubus_request *req, int type, struct blob_attr *msg) if (!tb[LOG_MSG] || !tb[LOG_ID] || !tb[LOG_PRIO] || !tb[LOG_SOURCE] || !tb[LOG_TIME]) continue; - t = blobmsg_get_u64(tb[LOG_TIME]) / 1000; + t = blobmsg_get_u64(tb[LOG_TIME]); + p = blobmsg_get_u32(tb[LOG_PRIO]); c = ctime(&t); c[strlen(c) - 1] = '\0'; - printf("%s - %s: %s\n", - c, (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("syslog") : ("kernel"), + + printf("%s %s.%s%s %s\n", + c, facilitynames[LOG_FAC(p)].c_name, prioritynames[LOG_PRI(p)].c_name, + (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"), blobmsg_get_string(tb[LOG_MSG])); } } @@ -145,6 +173,7 @@ static int usage(const char *prog) "Options:\n" " -s Path to ubus socket\n" " -l Got only the last 'count' messages\n" + " -r Stream message to a server\n" " -f Follow log messages\n" "\n", prog); return 1; @@ -154,15 +183,19 @@ int main(int argc, char **argv) { struct ubus_context *ctx; uint32_t id; - const char *ubus_socket = NULL; + const char *ubus_socket = NULL, *url = NULL, *port = NULL; int ch, ret, subscribe = 0, lines = 0; static struct blob_buf b; - while ((ch = getopt(argc, argv, "fs:l:")) != -1) { + while ((ch = getopt(argc, argv, "fs:l:r:")) != -1) { switch (ch) { case 's': ubus_socket = optarg; break; + case 'r': + url = optarg++; + port = argv[optind++]; + break; case 'f': subscribe = 1; break; @@ -170,8 +203,7 @@ int main(int argc, char **argv) lines = atoi(optarg); break; default: - usage(*argv); - break; + return usage(*argv); } } @@ -193,7 +225,7 @@ int main(int argc, char **argv) } if (subscribe) - follow_log(ctx, id); + follow_log(ctx, id, url, port); return 0; }