Skip to content

Commit

Permalink
Clean up some logging in TouchPortalClient and TouchPortalSocket to r…
Browse files Browse the repository at this point in the history
…emove interpolation; TouchPortalClient.SendCommand will now log serialized JSON message being sent (at debug level); typos in TouchPortalSocket comments.
  • Loading branch information
mpaperno committed Nov 26, 2024
1 parent 134268b commit 0e954f0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
11 changes: 7 additions & 4 deletions TouchPortalSDK/Clients/TouchPortalClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private void Close(string message, Exception exception = default)
return;
_closing = true;

_logger?.LogInformation(exception, $"Closing TouchPortal Plugin: '{message}'");
_logger?.LogInformation(exception, "Closing TouchPortal Plugin: '{message}'", message);

_eventHandler.OnClosedEvent(message);

Expand Down Expand Up @@ -271,7 +271,10 @@ bool ICommandHandler.SendCommand<TCommand>(TCommand command, string callerMember

var success = _touchPortalSocket.SendMessage(jsonMessage);

_logger?.LogDebug($"[{callerMemberName}] sent: '{success}'.");
_logger?.LogDebug(
"[{CallerMemberName}] sent: {success}; message: \n{Message}",
callerMemberName, success, System.Text.Encoding.UTF8.GetString(jsonMessage)
);

return success;
}
Expand Down Expand Up @@ -347,13 +350,13 @@ private void MessageHandlerTask()
}
// Catch any parsing exceptions (unlikely)
catch (JsonException e) {
_logger?.LogWarning(e, $"JSON parsing exception, see trace for details. Continuing execution with next message.'");
_logger?.LogWarning(e, "JSON parsing exception, see trace for details. Continuing execution with next message.'");
continue;
}
// Catch any exceptions in the plugin user's callback code itself.
// This does assume the plugin author is looking at their logs/console and not relying on us crashing on their exceptions.
catch (Exception e) {
_logger?.LogWarning(e, $"Exception in message event handler. Continuing execution with next message.'");
_logger?.LogWarning(e, "Exception in message event handler. Continuing execution with next message.'");
continue;
}
}
Expand Down
14 changes: 7 additions & 7 deletions TouchPortalSDK/Clients/TouchPortalSocket.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
Expand Down Expand Up @@ -145,7 +145,7 @@ public void CloseSocket()
// Note we must check _socket.Connected here in case we are exiting due to a socket error,
// in which case this method is likely being called from within that thread (via handler.OnError()). Hence if we try to
// wait for the thread to exit, that's just not going to work. We rest assured that it will exit once we're done here.
// OTOH if we're just disconnecting "nicely" then we do want to make sure the listener thread finished up after we cancelled the token.
// OTOH if we're just disconnecting "nicely" then we do want to make sure the listener thread finished up after we canceled the token.
if (_socket != null && _socket.Connected && _listenerThread.IsAlive && !_listenerThread.Join(_socket.ReceiveTimeout * 3)) {
_logger?.LogWarning("Network stream is hung up, interrupting the listener thread.");
_listenerThread.Interrupt();
Expand All @@ -166,7 +166,7 @@ private void WriteLine(in byte[] msgbytes)
sent = 0,
startAt = 0;

_logger?.LogDebug($"WriteLine() Starting message send with {len} bytes.");
_logger?.LogTrace("WriteLine() Starting message send with {Length} bytes.", len);
do {
try {
sent += _socket.Send(msgbytes, startAt, unsent, SocketFlags.None);
Expand All @@ -190,7 +190,7 @@ private void WriteLine(in byte[] msgbytes)
}
while (sent < len && _socket.Connected && !_cancellationToken.IsCancellationRequested);

_logger?.LogDebug($"WriteLine() Sent {sent} bytes.");
_logger?.LogDebug("WriteLine() Sent {Sent} out of {Length} bytes.", sent, len);
}

private void ListenerThreadSync()
Expand Down Expand Up @@ -262,10 +262,10 @@ private void ListenerThreadSync()
// _socket.Send(empty, 0, 0);
//}

} // outer "wait for data or cacellation" loop
} // outer "wait for data or cancellation" loop

// This will happen when using socket.Available and the socket is closed unexpectedly (bbasically equivalent of the catch statements above).
// We could probably remove this check if not using Available, but it dosn't hurt anything to leave it in.
// This will happen when using socket.Available and the socket is closed unexpectedly (basically equivalent of the catch statements above).
// We could probably remove this check if not using Available, but it doesn't hurt anything to leave it in.
if (!_socket.Connected && !_cancellationToken.IsCancellationRequested)
_messageHandler.OnError("Connection Terminated, Touch Portal quit without a goodbye.", null);
}
Expand Down

0 comments on commit 0e954f0

Please sign in to comment.