foxygit / doom Log in
commit f7f8ce6c6328b41a2aa9394f148f07c5203cec11
Author:     Simon Howard <fraggle@soulsphere.org>
AuthorDate: Sun Jan 27 23:33:46 2019 -0500
Commit:     Simon Howard <fraggle@soulsphere.org>
CommitDate: Sun Jan 27 23:33:46 2019 -0500

    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.
---
 src/net_client.c    |  1 +
 src/net_common.c    | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/net_common.h    |  4 +++
 src/net_dedicated.c |  3 ++-
 4 files changed, 82 insertions(+), 1 deletion(-)

diff --git a/src/net_client.c b/src/net_client.c
index 4c6c24f4..78b64987 100644
--- a/src/net_client.c
+++ b/src/net_client.c
@@ -1136,6 +1136,7 @@ void NET_CL_Init(void)

 void NET_Init(void)
 {
+    NET_OpenLog();
     NET_CL_Init();
 }

diff --git a/src/net_common.c b/src/net_common.c
index deadcded..08eeb0ee 100644
--- a/src/net_common.c
+++ b/src/net_common.c
@@ -16,12 +16,14 @@

 #include <stdio.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <string.h>

 #include "doomtype.h"
 #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 @@ struct net_reliable_packet_s
     net_reliable_packet_t *next;
 };

+static FILE *net_debug = NULL;
+
 // Why did the server reject us?
 char *net_client_reject_reason = NULL;

@@ -478,3 +482,74 @@ boolean NET_ValidGameSettings(GameMode_t mode, GameMission_t mission,
     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");
+}
+
diff --git a/src/net_common.h b/src/net_common.h
index 909bf154..15a7684a 100644
--- a/src/net_common.h
+++ b/src/net_common.h
@@ -98,5 +98,9 @@ unsigned int NET_ExpandTicNum(unsigned int relative, unsigned int b);
 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 */

diff --git a/src/net_dedicated.c b/src/net_dedicated.c
index 723867f8..22fd3052 100644
--- 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 @@ void NET_DedicatedServer(void)
 {
     CheckForClientOptions();

+    NET_OpenLog();
     NET_SV_Init();
     NET_SV_AddModule(&net_sdl_module);
     NET_SV_RegisterWithMaster();