Implemented debug logging.

Fix high CPU usage caused by infinite loop.
This commit is contained in:
2019-07-22 14:23:50 +03:00
parent 258424f36f
commit 752c0e0720
8 changed files with 149 additions and 8 deletions
+16 -3
View File
@@ -62,7 +62,18 @@ namespace MTSC.Client
{
foreach (ILogger logger in loggers)
{
logger.Log(log + "\n");
logger.Log(log);
}
}
/// <summary>
/// Logs the debug message onto the associated loggers.
/// </summary>
/// <param name="debugMessage"></param>
public void LogDebug(string debugMessage)
{
foreach (ILogger logger in loggers)
{
logger.LogDebug(debugMessage);
}
}
/// <summary>
@@ -195,7 +206,7 @@ namespace MTSC.Client
* When a message has been received, process it.
*/
Message message = CommunicationPrimitives.GetMessage(tcpClient);
Log("Received a message of size: " + message.MessageLength);
LogDebug("Received a message of size: " + message.MessageLength);
/*
* Preprocess message.
*/
@@ -224,7 +235,9 @@ namespace MTSC.Client
}
catch(Exception e)
{
foreach(IExceptionHandler exceptionHandler in exceptionHandlers)
LogDebug("Exception: " + e.Message);
LogDebug("Stacktrace: " + e.StackTrace);
foreach (IExceptionHandler exceptionHandler in exceptionHandlers)
{
exceptionHandler.HandleException(e);
}
+2 -1
View File
@@ -34,7 +34,8 @@ namespace MTSC.Client.Handlers
public bool HandleReceivedMessage(TcpClient client, Message message)
{
managedClient.Log("Broadcast: " + ASCIIEncoding.ASCII.GetString(message.MessageBytes));
managedClient.LogDebug("Broadcast: " + ASCIIEncoding.ASCII.GetString(message.MessageBytes));
managedClient.Log(">" + ASCIIEncoding.ASCII.GetString(message.MessageBytes));
return false;
}
+9
View File
@@ -18,5 +18,14 @@ namespace MTSC.Logging
Console.WriteLine(message);
return false;
}
/// <summary>
/// Ignores debug messages.
/// </summary>
/// <param name="message"></param>
/// <returns>False</returns>
public bool LogDebug(string message)
{
return false;
}
}
}
+29
View File
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace MTSC.Logging
{
public class DebugConsoleLogger : ILogger
{
/// <summary>
/// Ignores simple logging messages.
/// </summary>
/// <param name="message"></param>
/// <returns>False</returns>
public bool Log(string message)
{
return false;
}
/// <summary>
/// Outputs the debug message to console.
/// </summary>
/// <param name="message">Message to be output.</param>
/// <returns>False</returns>
public bool LogDebug(string message)
{
Console.WriteLine(message);
return false;
}
}
}
+6
View File
@@ -15,5 +15,11 @@ namespace MTSC.Logging
/// <param name="message">Message to be received.</param>
/// <returns>True if the message has been logged and no other logger should log this message.</returns>
bool Log(string message);
/// <summary>
/// Logs the received debug message.
/// </summary>
/// <param name="message">Debug message to be logged.</param>
/// <returns>True if the message has been logged and no other logger should log this message.</returns>
bool LogDebug(string message);
}
}
+2 -2
View File
@@ -28,8 +28,8 @@ namespace MTSC.Server.Handlers
public bool HandleReceivedMessage(ClientStruct client, Message message)
{
managedServer.Log("Broadcast: " + ASCIIEncoding.ASCII.GetString(message.MessageBytes));
managedServer.Log("From: " + client.TcpClient.Client.RemoteEndPoint.ToString());
managedServer.LogDebug("Broadcast: " + ASCIIEncoding.ASCII.GetString(message.MessageBytes));
managedServer.LogDebug("From: " + client.TcpClient.Client.RemoteEndPoint.ToString());
foreach(ClientStruct clientStruct in managedServer.Clients)
{
managedServer.QueueMessage(clientStruct, message.MessageBytes);
+65
View File
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace MTSC.Server.Handlers
{
class HTTPHandler
{
public enum Methods
{
OPTIONS,
GET,
HEAD,
POST,
PUT,
DELETE,
TRACE,
CONNECT,
extension_method
}
public enum StatusCode
{
Continue = 100,
SwitchingProtocols = 101,
OK = 200,
Created = 201,
Accepted = 202,
NonAuthoritativeInformation = 203,
NoContent = 204,
ResetContent = 205,
PartialContent = 206,
MultipleChoices = 300,
MovedPermanently = 301,
Found = 302,
SeeOther = 303,
NotModified = 304,
UseProxy = 305,
TemporaryRedirect = 307,
BadRequest = 400,
Unauthorized = 401,
PaymentRequired = 402,
Forbidden = 403,
NotFound = 404,
MethodNotAllowed = 405,
NotAcceptable = 406,
ProxyAuthenticationRequired = 407,
RequestTimeout = 408,
Conflict = 409,
Gone = 410,
LengthRequired = 411,
PreconditionFailed = 412,
RequestEntityTooLarge = 413,
RequestURITooLarge = 414,
UnsupportedMediaType = 415,
RequestRangeNotSatisfiable = 416,
ExpectationFailed = 417,
InternalServerError = 500,
NotImplemented = 501,
BadGateway = 502,
ServiceUnavailable = 503,
GatewayTimeout = 504,
HttpVersionNotSupported = 505
}
}
}
+20 -2
View File
@@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MTSC.Server
@@ -115,7 +116,18 @@ namespace MTSC.Server
{
foreach (ILogger logger in loggers)
{
logger.Log(log + "\n");
logger.Log(log);
}
}
/// <summary>
/// Adds a debug message to be logged by the associated loggers.
/// </summary>
/// <param name="">Debug message to be logged</param>
public void LogDebug(string debugMessage)
{
foreach (ILogger logger in loggers)
{
logger.LogDebug(debugMessage);
}
}
/// <summary>
@@ -173,7 +185,7 @@ namespace MTSC.Server
if (client.TcpClient.Available > 0)
{
Message message = CommunicationPrimitives.GetMessage(client.TcpClient);
Log("Received message from " + client.TcpClient.Client.RemoteEndPoint.ToString() +
LogDebug("Received message from " + client.TcpClient.Client.RemoteEndPoint.ToString() +
"\nMessage length: " + message.MessageLength);
foreach (IHandler handler in handlers)
{
@@ -190,9 +202,15 @@ namespace MTSC.Server
}
}
}
else
{
Thread.Sleep(100);
}
}
catch(Exception e)
{
LogDebug("Exception: " + e.Message);
LogDebug("Stacktrace: " + e.StackTrace);
foreach(IExceptionHandler exceptionHandler in exceptionHandlers)
{
if (exceptionHandler.HandleException(e))