Better logging

This commit is contained in:
Sergey Lyubka 2022-04-01 15:41:03 +01:00
parent 8c7fd8b330
commit b42ea6cd3c

View File

@ -89,25 +89,6 @@ bool mg_sock_would_block(void) {
}
static void iolog(struct mg_connection *c, char *buf, long n, bool r) {
int log_level = n > 0 ? MG_LL_VERBOSE : MG_LL_DEBUG;
char flags[] = {(char) ('0' + c->is_listening),
(char) ('0' + c->is_client),
(char) ('0' + c->is_accepted),
(char) ('0' + c->is_resolving),
(char) ('0' + c->is_connecting),
(char) ('0' + c->is_tls),
(char) ('0' + c->is_tls_hs),
(char) ('0' + c->is_udp),
(char) ('0' + c->is_websocket),
(char) ('0' + c->is_hexdumping),
(char) ('0' + c->is_draining),
(char) ('0' + c->is_closing),
(char) ('0' + c->is_readable),
(char) ('0' + c->is_writable),
'\0'};
MG_LOG(log_level,
("%lu %s %d:%d %ld err %d (%s)", c->id, flags, (int) c->send.len,
(int) c->recv.len, n, MG_SOCK_ERRNO, strerror(errno)));
if (n == 0) {
// Do nothing
} else if (n < 0) {
@ -156,6 +137,8 @@ static long mg_sock_send(struct mg_connection *c, const void *buf, size_t len) {
bool mg_send(struct mg_connection *c, const void *buf, size_t len) {
if (c->is_udp) {
long n = mg_sock_send(c, buf, len);
MG_DEBUG(("%lu %p %d:%d %ld err %d (%s)", c->id, c->fd, (int) c->send.len,
(int) c->recv.len, n, MG_SOCK_ERRNO, strerror(errno)));
iolog(c, (char *) buf, n, false);
return n > 0;
} else {
@ -269,6 +252,8 @@ static long read_conn(struct mg_connection *c) {
char *buf = (char *) &c->recv.buf[c->recv.len];
size_t len = c->recv.size - c->recv.len;
n = c->is_tls ? mg_tls_recv(c, buf, len) : mg_sock_recv(c, buf, len);
MG_DEBUG(("%lu %p %d:%d %ld err %d (%s)", c->id, c->fd, (int) c->send.len,
(int) c->recv.len, n, MG_SOCK_ERRNO, strerror(errno)));
iolog(c, buf, n, true);
}
return n;
@ -278,6 +263,8 @@ static void write_conn(struct mg_connection *c) {
char *buf = (char *) c->send.buf;
size_t len = c->send.len;
long n = c->is_tls ? mg_tls_send(c, buf, len) : mg_sock_send(c, buf, len);
MG_DEBUG(("%lu %p %d:%d %ld err %d (%s)", c->id, c->fd, (int) c->send.len,
(int) c->recv.len, n, MG_SOCK_ERRNO, strerror(errno)));
iolog(c, buf, n, false);
}
@ -354,6 +341,7 @@ void mg_connect_resolved(struct mg_connection *c) {
mg_error(c, "connect: %d", MG_SOCK_ERRNO);
}
}
MG_DEBUG(("%lu %p", c->id, c->fd));
}
static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) {
@ -503,13 +491,12 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
}
if ((rc = select((int) maxfd + 1, &rset, &wset, NULL, &tv)) < 0) {
MG_DEBUG(("select: %d %d", rc, MG_SOCK_ERRNO));
MG_ERROR(("select: %d %d", rc, MG_SOCK_ERRNO));
FD_ZERO(&rset);
FD_ZERO(&wset);
}
for (c = mgr->conns; c != NULL; c = c->next) {
// TLS might have stuff buffered, so dig everything
c->is_readable = FD(c) != INVALID_SOCKET && FD_ISSET(FD(c), &rset);
c->is_writable = FD(c) != INVALID_SOCKET && FD_ISSET(FD(c), &wset);
}