]> PatchyGit - patchyircd.git/commitdiff
centralize all client-related logging
authorEgor <egor@patchysicp.xyz>
Fri, 13 Jun 2025 00:37:02 +0000 (03:37 +0300)
committerEgor <egor@patchysicp.xyz>
Fri, 13 Jun 2025 01:18:16 +0000 (04:18 +0300)
and get rid of PRISocket

src/irc.cpp
src/main.cpp
src/net.cpp
src/net.h
src/netsock.h

index d75b028..7825168 100644 (file)
@@ -14,7 +14,7 @@ stringset autojoin = {};
 
 const char *hostname = 0;
 void sendbuf(struct client_data *client) {
-       fprintf(stderr, "SERVER %" PRISocket ": %.*s", client->fd, (int)databuf.len, (char*)databuf.buf);
+       log_client(client, "SERVER: %.*s", (int)databuf.len, (char*)databuf.buf);
        sendClient(client, databuf.buf, databuf.len);
 }
 static void chanlog_time(time_t timestamp) {
@@ -119,12 +119,12 @@ int parsePacket(struct client_data *client) {
                        if (end > p && end[-1] == '\r')
                                *--end = '\n';
                        if (memchr(p, '\0', end-p)) {
-                               fprintf(stderr, "Client %" PRISocket " sent a NULL\n", client->fd);
+                               log_client(client, "sent a NULL\n");
                                killConnection(client, 0);
                                return 1; // null detected
                        }
                        if (memchr(p, '\r', end-p)) {
-                               fprintf(stderr, "Client %" PRISocket " sent an extra CR\n", client->fd);
+                               log_client(client, "sent an extra CR\n");
                                killConnection(client, 0);
                                return 1; // internal \r detected
                        }
@@ -169,7 +169,7 @@ int parsePacket(struct client_data *client) {
                }
                args[argc] = NULL;
        }
-       fprintf(stderr, "parsed %" PRISocket ": %s", client->fd, command);
+       log_client(client, "parsed: %s", command);
        if (strcmp(command, "PASS") != 0 && strcmp(command, "OPER") != 0 && strcmp(command, "NEWACC") != 0
                        && strcmp(command, "AUTHENTICATE") != 0 && strcmp(command, "WEBIRC") != 0) {
                for (int i = 0; i < argc; i++){
@@ -1242,7 +1242,7 @@ void newUser(struct client_data *client) {
 #ifdef HAVE_LIBTLS
        // TODO: move this when we add sasl
        if (client->cctx && tls_peer_cert_provided(client->cctx))
-               fprintf(stderr, "CertFP %" PRISocket ": %s\n", client->fd, tls_peer_cert_hash(client->cctx));
+               log_client(client, "CertFP: %s\n", tls_peer_cert_hash(client->cctx));
 #endif
        rpl_welcome(client),  sendbuf(client);
        rpl_yourhost(client), sendbuf(client);
index d9e116a..28e56ab 100644 (file)
@@ -130,14 +130,12 @@ static void accept_from_sock(struct listener *listener)
                        strcpy_lit(client_addr, "?");
                        port = 0;
                }
-               fprintf(stderr, "Connection accepted from Socket: %" PRISocket ", IP: %s PORT: %d\n", fd,
-                               client_addr, port);
                struct tls *cctx = NULL;
                if (listener->ctx) {
 #ifdef HAVE_LIBTLS
                        if (tls_accept_socket(listener->ctx, &cctx, fd) == -1) {
                                close(fd);
-                               fprintf(stderr, "tls_accept_socket: %s\n", tls_error(listener->ctx));
+                               fprintf(stderr, "tls_accept_socket: %s (ip %s port %d)\n", tls_error(listener->ctx), client_addr, port);
                                continue;
                        }
 #endif
@@ -161,6 +159,7 @@ static void accept_from_sock(struct listener *listener)
                }
                if (!cl->hostname)
                        abortf("strdup fail (hostname)\n");
+               log_client(cl, "accepted (ip %s port %d)\n", client_addr, port);
                netpoll::add(cl->fd, SOCKTYPE_CLIENT, cl);
                update_poll_reg(cl);
                copy_strf(&databuf, ":%s NOTICE * :Hello welcome to PatchyIRCD!\r\n", hostname);
index 162ea0c..233a02e 100644 (file)
@@ -1,6 +1,7 @@
 // Copyright (c) 2022 Sam Smith
 // Copyright (c) 2022-2025 Egor
 // SPDX-License-Identifier: GPL-2.0-or-later
+#include <stdarg.h>
 #include "net.h"
 #include "irc.h"
 #include "ircfmt.h"
@@ -37,6 +38,15 @@ void update_poll_reg(struct client_data *cl)
        }
 }
 
+void log_client(struct client_data *cl, const char *format, ...)
+{
+       va_list ap;
+       va_start(ap, format);
+       fprintf(stderr, "C:%d ", cl->fd);
+       vfprintf(stderr, format, ap);
+       va_end(ap);
+}
+
 void killConnection(struct client_data *cl, int byChoice)
 {
        if (!cl)
@@ -46,7 +56,7 @@ void killConnection(struct client_data *cl, int byChoice)
        cl->dying = 1;
        job_kill(cl);
        if (cl->peername)
-               fprintf(stderr, "Client disconnected from Socket: %" PRISocket ", IP: %s PORT: %d\n", cl->fd, cl->peername, cl->peerport);
+               log_client(cl, "disconnected (ip %s port %d)\n", cl->peername, cl->peerport);
        if (!byChoice && cl->nick && cl->username) {
                msg_quit(cl, "", "Has left the building (Forced)");
                sendbuf_visible(cl);
@@ -107,9 +117,8 @@ void killConnectionPart2(struct client_data *cl)
                                update_poll_reg(cl);
                                return;
                        }
-                       if (rv == -1) {
-                               fprintf(stderr, "tls_close (%" PRISocket "): %s\n", cl->fd, tls_error(cl->cctx));
-                       }
+                       if (rv == -1)
+                               log_client(cl, "tls_close: %s\n", tls_error(cl->cctx));
                        tls_free(cl->cctx);
                        cl->cctx = NULL;
                }
index 307ac97..47330d1 100644 (file)
--- a/src/net.h
+++ b/src/net.h
@@ -1,5 +1,5 @@
 // Copyright (c) 2022 Sam Smith
-// Copyright (c) 2022-2024 Egor
+// Copyright (c) 2022-2025 Egor
 // SPDX-License-Identifier: GPL-2.0-or-later
 #ifndef NET_H_
 #define NET_H_
@@ -9,6 +9,7 @@ void sendbuf(struct client_data *client);
 void sendbuf_visible(struct client_data *client);
 struct client_data *allocClient(void);
 void update_poll_reg(struct client_data *cl);
+void log_client(struct client_data *cl, const char *format, ...);
 void killConnection(struct client_data *client, int byChoice);
 void killConnectionPart2(struct client_data *cl);
 void recvProc(struct client_data *cl);
index 1966477..571b2ba 100644 (file)
@@ -45,6 +45,5 @@
 #include <netdb.h>
 #include <fcntl.h>
 #include <signal.h>
-#define PRISocket "d"
 #define net_wouldblock() (errno == EAGAIN || errno == EWOULDBLOCK)
 #endif