shithub: choc

Download patch

ref: f7f8ce6c6328b41a2aa9394f148f07c5203cec11
parent: af804990166f96c87bbd9fb317251f5fe79de56b
author: Simon Howard <fraggle@soulsphere.org>
date: Sun Jan 27 18:33:46 EST 2019

net: Add network log file.

I've had some reports from multiplayer users about stalls where the
game just freezes up; having decent logging of network events seems
like an essential step for diagnosing such bugs. It seems like a
prudent feature to just have anyway; the network engine is already
littered with commented-out printf statements used in the past for
similar purposes.

--- a/src/net_client.c
+++ b/src/net_client.c
@@ -1136,6 +1136,7 @@
 
 void NET_Init(void)
 {
+    NET_OpenLog();
     NET_CL_Init();
 }
 
--- a/src/net_common.c
+++ b/src/net_common.c
@@ -16,6 +16,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <string.h>
 
 #include "doomtype.h"
@@ -22,6 +23,7 @@
 #include "d_mode.h"
 #include "i_system.h"
 #include "i_timer.h"
+#include "m_argv.h"
 
 #include "net_common.h"
 #include "net_io.h"
@@ -46,6 +48,8 @@
     net_reliable_packet_t *next;
 };
 
+static FILE *net_debug = NULL;
+
 // Why did the server reject us?
 char *net_client_reject_reason = NULL;
 
@@ -476,5 +480,76 @@
         return false;
 
     return true;
+}
+
+static void CloseLog(void)
+{
+    if (net_debug != NULL)
+    {
+        fclose(net_debug);
+        net_debug = NULL;
+    }
+}
+
+void NET_OpenLog(void)
+{
+    int p;
+
+    p = M_CheckParmWithArgs("-netlog", 1);
+    if (p > 0)
+    {
+        net_debug = fopen(myargv[p + 1], "w");
+        if (net_debug == NULL)
+        {
+            I_Error("Failed to open %s to write debug log.", myargv[p + 1]);
+        }
+        I_AtExit(CloseLog, true);
+    }
+}
+
+void NET_Log(const char *fmt, ...)
+{
+    va_list args;
+
+    if (net_debug == NULL)
+    {
+        return;
+    }
+
+    fprintf(net_debug, "%8d: ", I_GetTimeMS());
+    va_start(args, fmt);
+    vfprintf(net_debug, fmt, args);
+    va_end(args);
+    fprintf(net_debug, "\n");
+}
+
+void NET_LogPacket(net_packet_t *packet)
+{
+    int i, bytes;
+
+    if (net_debug == NULL)
+    {
+        return;
+    }
+
+    bytes = packet->len - packet->pos;
+    if (bytes == 0)
+    {
+        return;
+    }
+    fprintf(net_debug, "\t%02x", packet->data[packet->pos]);
+    for (i = 1; i < bytes; ++i)
+    {
+        if ((i % 16) == 0)
+        {
+            fprintf(net_debug, "\n\t");
+        }
+        else
+        {
+            fprintf(net_debug, " ");
+        }
+        fprintf(net_debug, "%02x", packet->data[packet->pos + i]);
+    }
+    fprintf(net_debug, "\n");
 }
 
--- a/src/net_common.h
+++ b/src/net_common.h
@@ -98,5 +98,9 @@
 boolean NET_ValidGameSettings(GameMode_t mode, GameMission_t mission,
                               net_gamesettings_t *settings);
 
+void NET_OpenLog(void);
+void NET_Log(const char *fmt, ...);
+void NET_LogPacket(net_packet_t *packet);
+
 #endif /* #ifndef NET_COMMON_H */
 
--- a/src/net_dedicated.c
+++ b/src/net_dedicated.c
@@ -25,7 +25,7 @@
 
 #include "m_argv.h"
 
-#include "net_defs.h"
+#include "net_common.h"
 #include "net_sdl.h"
 #include "net_server.h"
 
@@ -65,6 +65,7 @@
 {
     CheckForClientOptions();
 
+    NET_OpenLog();
     NET_SV_Init();
     NET_SV_AddModule(&net_sdl_module);
     NET_SV_RegisterWithMaster();