Add debug logs

This commit is contained in:
Stone_Red
2023-12-14 22:11:50 +01:00
parent 62eca1c474
commit c84ebdf7a1
4 changed files with 33 additions and 7 deletions
+3 -1
View File
@@ -28,7 +28,9 @@ internal class InputHandler
_ = commander.Register(clientCommands.ShowInfo, (HelpText)"Displays the private and public IP address.", "info", "inf"); _ = commander.Register(clientCommands.ShowInfo, (HelpText)"Displays the private and public IP address.", "info", "inf");
_ = commander.Register(hostCommands.Discover, (HelpText)"Tries to find other active hosts on the local network.", "discover", "disc"); _ = commander.Register(hostCommands.Discover, (HelpText)"Tries to find other active hosts on the local network.", "discover", "disc");
_ = commander.Register(hostCommands.Sync, (HelpText)"Syncs the host list", "sync"); _ = commander.Register(hostCommands.Sync, (HelpText)"Syncs the host list", "sync");
_ = commander.Register(logCommands.Log, (HelpText)"Displays live log.", "log");
Command logCommand = commander.Register((_) => logCommands.Log(false), (HelpText)"Displays live log.", "log");
_ = logCommand.Register((_) => logCommands.Log(true), (HelpText)"Displays live log.", "debug");
Command getCommand = commander.Register(downloadCommands.GetFile, (HelpText)"Attempts to retrieve a file from another host using a hash.", "get"); Command getCommand = commander.Register(downloadCommands.GetFile, (HelpText)"Attempts to retrieve a file from another host using a hash.", "get");
_ = getCommand.Register(downloadCommands.GetFileFrom, (HelpText)"Attempts to retrieve a file from another host using a .hyper file.", "from"); _ = getCommand.Register(downloadCommands.GetFileFrom, (HelpText)"Attempts to retrieve a file from another host using a .hyper file.", "from");
+15 -2
View File
@@ -8,10 +8,23 @@ namespace HyperbolicDownloader;
internal class LogCommands internal class LogCommands
{ {
public void Log(string _) private bool debug = false;
public void Log(bool debug)
{ {
this.debug = debug;
Console.Clear(); Console.Clear();
if (debug)
{
Console.WriteLine("Debug log opened");
}
else
{
Console.WriteLine("Log opened");
}
ApiManager.OnNotificationMessageRecived += OnNotificationMessageRecived; ApiManager.OnNotificationMessageRecived += OnNotificationMessageRecived;
char c = ' '; char c = ' ';
@@ -34,7 +47,7 @@ internal class LogCommands
private void OnNotificationMessageRecived(object? sender, NotificationMessageEventArgs e) private void OnNotificationMessageRecived(object? sender, NotificationMessageEventArgs e)
{ {
if (e.NotificationMessageType == NotificationMessageType.Log) if (e.NotificationMessageType == NotificationMessageType.Log || (debug && e.NotificationMessageType == NotificationMessageType.Debug))
{ {
lock (Console.Out) lock (Console.Out)
{ {
@@ -10,6 +10,7 @@ public enum NotificationMessageType
{ {
Info, Info,
Log, Log,
Debug,
Success, Success,
Warning, Warning,
Error Error
@@ -92,14 +92,25 @@ internal class NetworkClient(FilesManager filesManager)
{ {
try try
{ {
ApiManager.SendNotificationMessageNewLine("Listening for connections...", NotificationMessageType.Debug);
TcpClient client = tcpListener.AcceptTcpClient(); TcpClient client = tcpListener.AcceptTcpClient();
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Connected", NotificationMessageType.Debug);
NetworkStream nwStream = client.GetStream(); NetworkStream nwStream = client.GetStream();
byte[] buffer = new byte[client.ReceiveBufferSize]; byte[] buffer = new byte[client.ReceiveBufferSize];
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Reading data", NotificationMessageType.Debug);
int bytesRead = await nwStream.ReadAsync(buffer.AsMemory(0, client.ReceiveBufferSize)); int bytesRead = await nwStream.ReadAsync(buffer.AsMemory(0, client.ReceiveBufferSize));
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Data read", NotificationMessageType.Debug);
string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead); string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Data: {dataReceived}", NotificationMessageType.Debug);
if (dataReceived.StartsWith("Download")) if (dataReceived.StartsWith("Download"))
{ {
_ = Upload(client, dataReceived[8..]); _ = Upload(client, dataReceived[8..]);
@@ -129,13 +140,12 @@ internal class NetworkClient(FilesManager filesManager)
} }
client.Close(); client.Close();
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Disconnected", NotificationMessageType.Debug);
} }
catch (SocketException ex) catch (SocketException ex)
{ {
if (ex.SocketErrorCode != SocketError.Interrupted) ApiManager.SendNotificationMessageNewLine(ex.Message, NotificationMessageType.Log);
{
throw;
}
} }
} }
tcpListener.Stop(); tcpListener.Stop();