X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fprocd.git;a=blobdiff_plain;f=logread.c;h=bd8c69710bb50671e225a0783444a5c5a1b0296b;hp=ba12f446119762221f01d772f373858607ad20f4;hb=b5e17105b163442a030559b7b2c2454b82b3fd4c;hpb=8e1c60874f0ec195340b52953311e1521c3a063e diff --git a/logread.c b/logread.c index ba12f44..bd8c697 100644 --- a/logread.c +++ b/logread.c @@ -60,6 +60,16 @@ static const char *log_file, *log_ip, *log_port, *pid_file; static int log_type = LOG_STDOUT; static int log_size, log_udp; +static const char* getcodetext(int value, CODE *codetable) { + CODE *i; + + if (value >= 0) + for (i = codetable; i->c_val != -1; i++) + if (i->c_val == value) + return (i->c_name); + return ""; +}; + static void log_handle_reconnect(struct uloop_timeout *timeout) { sender.fd = usock((log_udp) ? (USOCK_UDP) : (USOCK_TCP), log_ip, log_port); @@ -116,7 +126,7 @@ static int log_notify(struct ubus_context *ctx, struct ubus_object *obj, rename(log_file, old); free(old); } - sender.fd = open(log_file, O_CREAT | O_WRONLY | O_TRUNC); + sender.fd = open(log_file, O_CREAT | O_WRONLY | O_APPEND, 0600); if (sender.fd < 0) { // fprintf(stderr, "failed to open %s: %s\n", log_file, strerror(errno)); exit(-1); @@ -129,13 +139,27 @@ static int log_notify(struct ubus_context *ctx, struct ubus_object *obj, c[strlen(c) - 1] = '\0'; str = blobmsg_format_json(msg, true); if (log_type == LOG_NET) { + int err; + snprintf(buf, sizeof(buf), "%s%s\n", (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : ("kernel: "), method); - send(sender.fd, buf, strlen(buf), 0); + if (log_udp) + err = write(sender.fd, buf, strlen(buf)); + else + err = send(sender.fd, buf, strlen(buf), 0); + + if (err < 0) { + syslog(0, "failed to send log data to %s:%s via %s\n", + log_ip, log_port, (log_udp) ? ("udp") : ("tcp")); + uloop_fd_delete(&sender); + close(sender.fd); + sender.fd = -1; + uloop_timeout_set(&retry, 1000); + } } else { snprintf(buf, sizeof(buf), "%s %s.%s%s %s\n", - c, facilitynames[LOG_FAC(p)].c_name, prioritynames[LOG_PRI(p)].c_name, + c, getcodetext(LOG_FAC(p) << 3, facilitynames), getcodetext(LOG_PRI(p), prioritynames), (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"), method); write(sender.fd, buf, strlen(buf)); @@ -184,7 +208,7 @@ static void follow_log(struct ubus_context *ctx, int id) uloop_timeout_set(&retry, 1000); } else if (log_file) { log_type = LOG_FILE; - sender.fd = open(log_file, O_CREAT | O_WRONLY); + sender.fd = open(log_file, O_CREAT | O_WRONLY| O_APPEND, 0600); if (sender.fd < 0) { fprintf(stderr, "failed to open %s: %s\n", log_file, strerror(errno)); exit(-1); @@ -203,6 +227,8 @@ enum { __READ_MAX }; + + static const struct blobmsg_policy read_policy[] = { [READ_LINE] = { .name = "lines", .type = BLOBMSG_TYPE_ARRAY }, }; @@ -238,7 +264,7 @@ static void read_cb(struct ubus_request *req, int type, struct blob_attr *msg) c[strlen(c) - 1] = '\0'; printf("%s %s.%s%s %s\n", - c, facilitynames[LOG_FAC(p)].c_name, prioritynames[LOG_PRI(p)].c_name, + c, getcodetext(LOG_FAC(p) << 3, facilitynames), getcodetext(LOG_PRI(p), prioritynames), (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"), blobmsg_get_string(tb[LOG_MSG])); }