Skip to content

Commit

Permalink
fix: adding more verbose logging
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Frowen committed Oct 6, 2020
1 parent e6ef74a commit 4f51a9f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
29 changes: 25 additions & 4 deletions source/Log.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Conditional = System.Diagnostics.ConditionalAttribute;
using Debug = UnityEngine.Debug;

Expand All @@ -21,7 +22,7 @@ public static void Verbose(string msg)
if (level < Levels.verbose)
return;

Debug.Log($"INFO: <color=blue>{msg}</color>");
Debug.Log($"Verbose: <color=blue>{msg}</color>");
}

[Conditional("DEBUG")]
Expand All @@ -31,9 +32,29 @@ public static void Verbose(string msg, bool showColor)
return;

if (showColor)
Info(msg);
Verbose(msg);
else
Debug.Log($"INFO: {msg}");
Debug.Log($"Verbose: {msg}");
}

[Conditional("DEBUG")]
public static void DumpBuffer(byte[] buffer, int offset, int length)
{
if (level < Levels.verbose)
return;

string text = BitConverter.ToString(buffer, offset, length);
Verbose(text);
}

[Conditional("DEBUG")]
public static void DumpBuffer(string label, byte[] buffer, int offset, int length)
{
if (level < Levels.verbose)
return;

string text = BitConverter.ToString(buffer, offset, length);
Verbose($"{label}: {text}");
}

[Conditional("DEBUG")]
Expand Down Expand Up @@ -74,7 +95,7 @@ public static void Error(string msg, bool showColor)
return;

if (showColor)
Info(msg);
Error(msg);
else
Debug.Log($"ERROR: {msg}");
}
Expand Down
3 changes: 2 additions & 1 deletion source/WebSocketClientStandAlone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ private bool ReadOneMessage(Connection conn, Stream stream, byte[] headerBuffer)

ReadHelper.SafeRead(stream, buffer, HeaderLength, header.readLength);

Log.DumpBuffer("Message From Server", buffer, 0, buffer.Length);
HandleMessage(header.opcode, conn, buffer, header.msgOffset, header.msgLength);
return true;
}
Expand Down Expand Up @@ -282,7 +283,7 @@ void SendMessageToServer(Stream stream, ArraySegment<byte> msg)
MessageProcessor.ToggleMask(buffer, sendLength, msgLength, buffer, sendLength - 4);
sendLength += msgLength;

Log.Verbose("Send To Server");
Log.DumpBuffer("Send To Server", buffer, 0, sendLength);
stream.Write(buffer, 0, sendLength);
}

Expand Down
2 changes: 2 additions & 0 deletions source/WebSocketServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ private bool ReadOneMessage(Connection conn, Stream stream, byte[] headerBuffer)

MessageProcessor.ToggleMask(buffer, header.offset + 4, header.msgLength, buffer, header.offset);

Log.DumpBuffer($"Message From Client {conn}", buffer, 0, buffer.Length);
HandleMessage(header.opcode, conn, buffer, header.msgOffset, header.msgLength);
return true;
}
Expand Down Expand Up @@ -314,6 +315,7 @@ static void SendMessageToClient(Stream stream, ArraySegment<byte> msg)
Array.Copy(msg.Array, msg.Offset, buffer, sendLength, msgLength);
sendLength += msgLength;

Log.DumpBuffer("Send To Client", buffer, 0, sendLength);
stream.Write(buffer, 0, sendLength);
}

Expand Down

0 comments on commit 4f51a9f

Please sign in to comment.