commit 0de5d5e4c7745a7bcc8d3d300906535ea5044aa4
parent 5dcf3d1550bd72f05c2f543e7e83cb09ab6a86b5
Author: Joris Vink <joris@coders.se>
Date: Mon, 5 Sep 2022 10:59:06 +0200
Use a more structured log format.
Diffstat:
3 files changed, 57 insertions(+), 4 deletions(-)
diff --git a/include/kore/kore.h b/include/kore/kore.h
@@ -885,6 +885,10 @@ void kore_connection_check_idletimer(u_int64_t,
struct connection *);
int kore_connection_accept(struct listener *,
struct connection **);
+void kore_connection_log(struct connection *,
+ const char *, ...)
+ __attribute__((format (printf, 2, 3)));
+const char *kore_connection_ip(struct connection *);
void kore_log_init(void);
void kore_log_file(const char *);
diff --git a/src/connection.c b/src/connection.c
@@ -399,3 +399,52 @@ kore_connection_nonblock(int fd, int nodelay)
return (KORE_RESULT_OK);
}
+
+void
+kore_connection_log(struct connection *c, const char *fmt, ...)
+{
+ struct kore_buf buf;
+ va_list args;
+ char *ptr;
+
+ kore_buf_init(&buf, 128);
+ kore_buf_appendf(&buf, "ip=[%s] msg=[", kore_connection_ip(c));
+
+ va_start(args, fmt);
+ kore_buf_appendv(&buf, fmt, args);
+ va_end(args);
+
+ kore_buf_appendf(&buf, "]");
+
+ ptr = kore_buf_stringify(&buf, NULL);
+ kore_log(LOG_NOTICE, "%s", ptr);
+ kore_free(ptr);
+}
+
+const char *
+kore_connection_ip(struct connection *c)
+{
+ static char addr[INET6_ADDRSTRLEN];
+
+ memset(addr, 0, sizeof(addr));
+
+ switch (c->family) {
+ case AF_INET:
+ if (inet_ntop(c->family,
+ &(c->addr.ipv4.sin_addr), addr, sizeof(addr)) == NULL)
+ fatal("inet_ntop: %s", errno_s);
+ break;
+ case AF_INET6:
+ if (inet_ntop(c->family,
+ &(c->addr.ipv6.sin6_addr), addr, sizeof(addr)) == NULL)
+ fatal("inet_ntop: %s", errno_s);
+ break;
+ case AF_UNIX:
+ (void)kore_strlcpy(addr, "unix-socket", sizeof(addr));
+ break;
+ default:
+ fatal("unknown family %d", c->family);
+ }
+
+ return (addr);
+}
diff --git a/src/log.c b/src/log.c
@@ -95,9 +95,9 @@ kore_log(int prio, const char *fmt, ...)
str = kore_buf_stringify(&buf, NULL);
if (kore_foreground || fp != stdout)
- log_print(prio, "[parent]: %s\n", str);
+ log_print(prio, "proc=[parent] log=[%s]\n", str);
else
- syslog(prio, "[parent]: %s", str);
+ syslog(prio, "proc=[parent] log=[%s]", str);
}
kore_buf_cleanup(&buf);
@@ -120,10 +120,10 @@ log_from_worker(struct kore_msg *msg, const void *data)
name = kore_worker_name(wlog->wid);
if (kore_foreground || fp != stdout) {
- log_print(wlog->prio, "%s: %.*s\n",
+ log_print(wlog->prio, "proc=%s log=[%.*s]\n",
name, (int)wlog->loglen, wlog->logmsg);
} else {
- syslog(wlog->prio, "%s: %.*s",
+ syslog(wlog->prio, "proc=%s log=[%.*s]",
name, (int)wlog->loglen, wlog->logmsg);
}
}