diff --git a/Programs/examples/TestClient/Commands/System/LogPacketCommand.cs b/Programs/examples/TestClient/Commands/System/LogPacketCommand.cs index 76ef63f9..1226f0ed 100644 --- a/Programs/examples/TestClient/Commands/System/LogPacketCommand.cs +++ b/Programs/examples/TestClient/Commands/System/LogPacketCommand.cs @@ -34,22 +34,25 @@ namespace OpenMetaverse.TestClient return string.Format( "{0} is not a valid number of packets for {1}", rawNumberOfPackets, m_client.Self.Name); - if (m_isLogging) - return string.Format( - "Still waiting to finish logging {0} packets for {1}", - m_packetsToLogRemaining, m_client.Self.Name); - - try + lock (this) { - lock (this) + if (m_isLogging) + return string.Format( + "Still waiting to finish logging {0} packets for {1}", + m_packetsToLogRemaining, m_client.Self.Name); + + try + { m_logStreamWriter = new StreamWriter(path); - } - catch (Exception e) - { - return string.Format("Could not open file with path [{0}], exception {1}", path, e); + } + catch (Exception e) + { + return string.Format("Could not open file with path [{0}], exception {1}", path, e); + } + + m_isLogging = true; } - m_isLogging = true; m_packetsToLogRemaining = numberOfPackets; m_client.Network.RegisterCallback(PacketType.Default, HandlePacket); return string.Format("Now logging {0} packets for {1}", m_packetsToLogRemaining, m_client.Self.Name); @@ -68,15 +71,29 @@ namespace OpenMetaverse.TestClient // Console.WriteLine( // "Received packet {0} from {1} for {2}", args.Packet.Type, args.Simulator.Name, m_client.Self.Name); - m_logStreamWriter.WriteLine("Received at {0}", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff")); - m_logStreamWriter.WriteLine(PacketDecoder.PacketToString(args.Packet)); - - if (--m_packetsToLogRemaining <= 0) + lock (this) { - m_client.Network.UnregisterCallback(PacketType.Default, HandlePacket); - m_logStreamWriter.Close(); - Console.WriteLine("Finished logging packets for {0}", m_client.Self.Name); - m_isLogging = false; + if (!m_isLogging) + return; + + m_logStreamWriter.WriteLine("Received: {0}", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff")); + + try + { + m_logStreamWriter.WriteLine(PacketDecoder.PacketToString(args.Packet)); + } + catch (Exception e) + { + m_logStreamWriter.WriteLine("Failed to write decode of {0}, exception {1}", args.Packet.Type, e); + } + + if (--m_packetsToLogRemaining <= 0) + { + m_client.Network.UnregisterCallback(PacketType.Default, HandlePacket); + m_logStreamWriter.Close(); + Console.WriteLine("Finished logging packets for {0}", m_client.Self.Name); + m_isLogging = false; + } } } }