From: John Crispin Date: Fri, 10 May 2013 21:22:24 +0000 (+0200) Subject: logread X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fprocd.git;a=commitdiff_plain;h=f95bd54a07a5ae06115ffe5d6956bbc8f4a42eb5;hp=0c116c65f9bc4132fa0dfc99e875f57e9ee29af1 logread --- diff --git a/logread.c b/logread.c index 2a0ffde..3b73305 100644 --- a/logread.c +++ b/logread.c @@ -17,6 +17,7 @@ #include #include +#include #include #include "libubus.h" @@ -44,6 +45,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 +58,7 @@ 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]; char *str; time_t t; char *c; @@ -68,14 +71,16 @@ static int log_notify(struct ubus_context *ctx, struct ubus_object *obj, c = ctime(&t); c[strlen(c) - 1] = '\0'; str = blobmsg_format_json(msg, true); - printf("%s - %s: %s\n", + snprintf(buf, sizeof(buf), "%s - %s: %s\n", c, (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("syslog") : ("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 +97,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(); @@ -145,6 +162,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 +172,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 +192,7 @@ int main(int argc, char **argv) lines = atoi(optarg); break; default: - usage(*argv); - break; + return usage(*argv); } } @@ -193,7 +214,7 @@ int main(int argc, char **argv) } if (subscribe) - follow_log(ctx, id); + follow_log(ctx, id, url, port); return 0; }