Moved logging system to separate repository.

This commit is contained in:
Harrison Deng 2021-04-22 20:39:22 -05:00
parent f9cbd0871d
commit 0073efc9ac
14 changed files with 30 additions and 115 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "SimpleLogger"]
path = SimpleLogger
url = https://systems.reslate.xyz/git/ydeng/SimpleLogger.git

1
SimpleLogger Submodule

@ -0,0 +1 @@
Subproject commit f9931eea4295353befffcf1880ea510cc259e9f2

View File

@ -3,6 +3,7 @@
<ItemGroup>
<ProjectReference Include="..\GameServiceWarden.ModuleFramework\GameServiceWarden.ModuleFramework.csproj" />
<ProjectReference Include="..\GameServiceWarden.InteractionAPI\GameServiceWarden.InteractionAPI.csproj" />
<ProjectReference Include="..\..\SimpleLogger\SimpleLogger.csproj" />
</ItemGroup>
<PropertyGroup>

View File

@ -1,28 +0,0 @@
using System;
namespace GameServiceWarden.Core.Logging
{
public interface ILogReceiver
{
string Identifier { get; }
/// <summary>
/// The severity of the messages this log should receive.
/// </summary>
/// <value>The severity of the logs.</value>
LogLevel Level { get; }
/// <summary>
/// Logs the message.
/// </summary>
/// <param name="message">The message to be logged.</param>
/// <param name="time">The time at which this message was requested to be logged.</param>
/// <param name="level">The severity of this message.</param>
void LogMessage(string message, DateTime time, LogLevel level);
/// <summary>
/// Called when this receiver should explicitly flush received messages.
/// </summary>
void Flush();
}
}

View File

@ -1,10 +0,0 @@
namespace GameServiceWarden.Core.Logging
{
public enum LogLevel : int
{
FATAL,
INFO,
WARNING,
DEBUG,
}
}

View File

@ -1,52 +0,0 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace GameServiceWarden.Core.Logging
{
public static class Logger {
private static readonly ConcurrentDictionary<string, ILogReceiver> listeners = new ConcurrentDictionary<string, ILogReceiver>();
/// <summary>
/// Logs the message to listeners that are listening to the set severity of the message or greater.
/// </summary>
/// <param name="message">The message to log.</param>
/// <param name="level">The level of severity, by default, info.</param>
public static void Log(string message, LogLevel level = LogLevel.INFO) {
foreach (ILogReceiver listener in listeners.Values)
{
if (level <= listener.Level) {
listener.LogMessage(message, DateTime.Now, level);
}
}
}
/// <summary>
/// Adds a log listener.
/// </summary>
/// <param name="listener">The listener to add.</param>
public static void AddLogListener(ILogReceiver listener) {
listeners[listener.Identifier] = listener;
}
/// <summary>
/// Removes a log listener.
/// </summary>
/// <param name="listener">The listener to remove.</param>
public static void RemoveLogListener(ILogReceiver listener) {
ILogReceiver receiver;
listeners.TryRemove(listener.Identifier, out receiver);
}
/// <summary>
/// Called when all listeners should perform any flushing they need.
/// </summary>
public static void FlushListeners()
{
foreach (ILogReceiver listener in listeners.Values)
{
listener.Flush();
}
}
}
}

View File

@ -10,7 +10,7 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
using GameServiceWarden.Core.Module.Exceptions;
using GameServiceWarden.Core.Logging;
using SimpleLogger;
using GameServiceWarden.ModuleFramework;
using System.Net.Sockets;
@ -90,7 +90,7 @@ namespace GameServiceWarden.Core.Module
/// <exception cref="InvalidOperationException">Is thrown when the service is not running.</exception>
public void ExecuteCommand(string command)
{
Logger.Log($"\"{ServiceName}\" is executing command \"{command}\".", LogLevel.DEBUG);
Logger.Log($"\"{ServiceName}\" is executing command \"{command}\".", LogLevel.Debug);
if (state != ServiceState.Running) throw new InvalidOperationException("Service instance not running.");
service.ExecuteCommand(command);
}
@ -145,7 +145,7 @@ namespace GameServiceWarden.Core.Module
private void OnServiceStateChange(object sender, ServiceState state)
{
this.state = state;
Logger.Log($"The service \"{ServiceName}\" is changing states to {this.state}.", LogLevel.DEBUG);
Logger.Log($"The service \"{ServiceName}\" is changing states to {this.state}.", LogLevel.Debug);
ServiceStateChangeEvent?.Invoke(this, this.state);
}

View File

@ -3,7 +3,7 @@ using System.Text.Json;
using GameServiceWarden.InteractionAPI;
using GameServiceWarden.InteractionAPI.Communicable.Requests;
using GameServiceWarden.Core.Module;
using GameServiceWarden.Core.Logging;
using SimpleLogger;
namespace GameServiceWarden.Core.UI
{

View File

@ -10,7 +10,7 @@ using System.Threading.Tasks;
using GameServiceWarden.InteractionAPI;
using GameServiceWarden.InteractionAPI.Communicable.Requests;
using GameServiceWarden.InteractionAPI.Communicable.Responses;
using GameServiceWarden.Core.Logging;
using SimpleLogger;
namespace GameServiceWarden.Core.UI
{
@ -41,7 +41,7 @@ namespace GameServiceWarden.Core.UI
active = true;
stopAcceptingToken = new CancellationTokenSource();
connectionTask = AcceptConnections();
Logger.Log($"IPCMediator \"{name}\" has begun asynchronously accepting interfaces.", LogLevel.DEBUG);
Logger.Log($"IPCMediator \"{name}\" has begun asynchronously accepting interfaces.", LogLevel.Debug);
}
public void Close()
@ -115,7 +115,7 @@ namespace GameServiceWarden.Core.UI
while (active)
{
connectingPipe = new NamedPipeServerStream(PipeName, PipeDirection.InOut, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
Logger.Log("Waiting for connection.", LogLevel.DEBUG);
Logger.Log("Waiting for connection.", LogLevel.Debug);
await connectingPipe.WaitForConnectionAsync(stopAcceptingToken.Token);
connectionTasks.Add(OnConnection(connectingPipe));
for (int i = 0; i < connectionTasks.Count; i++)
@ -127,7 +127,7 @@ namespace GameServiceWarden.Core.UI
}
}
}
Logger.Log("Waiting for connection tasks.", LogLevel.DEBUG);
Logger.Log("Waiting for connection tasks.", LogLevel.Debug);
foreach (Task task in connectionTasks)
{
task.Wait();
@ -137,7 +137,7 @@ namespace GameServiceWarden.Core.UI
private async Task OnConnection(NamedPipeServerStream pipe)
{
Logger.Log("Interface attempting to connect.", LogLevel.DEBUG);
Logger.Log("Interface attempting to connect.", LogLevel.Debug);
byte[] headerBuffer = new byte[sizeof(uint) * 2];
int headerFill = 0;
CancellationTokenSource headerCancel = new CancellationTokenSource(TIMEOUT);
@ -153,13 +153,13 @@ namespace GameServiceWarden.Core.UI
catch (AggregateException e)
{
e.Handle((exception) => exception is TaskCanceledException);
Logger.Log($"Interface did not send header data within {TIMEOUT}ms.", LogLevel.DEBUG);
Logger.Log($"Interface did not send header data within {TIMEOUT}ms.", LogLevel.Debug);
} finally {
await pipe.DisposeAsync();
headerCancel.Dispose();
}
if (headerFill != headerBuffer.Length) {
Logger.Log($"Interface failed to send header data.", LogLevel.DEBUG);
Logger.Log($"Interface failed to send header data.", LogLevel.Debug);
return;
}
@ -182,13 +182,13 @@ namespace GameServiceWarden.Core.UI
catch (AggregateException e)
{
e.Handle((exception) => exception is TaskCanceledException);
Logger.Log($"Interface failed to send body data within {TIMEOUT}.", LogLevel.DEBUG);
Logger.Log($"Interface failed to send body data within {TIMEOUT}.", LogLevel.Debug);
} finally {
await pipe.DisposeAsync();
bodyCancel.Dispose();
}
if (bodyFill != bodyBuffer.Length) {
Logger.Log($"Interface failed to send body data.", LogLevel.DEBUG);
Logger.Log($"Interface failed to send body data.", LogLevel.Debug);
return;
}
@ -199,12 +199,12 @@ namespace GameServiceWarden.Core.UI
if (string.IsNullOrWhiteSpace(request.requestedIdentifier)) {
response.invalidName = true;
response.errorMsg = $"The requested identifier \"{request.requestedIdentifier}\" is null or whitespace.";
Logger.Log(response.errorMsg, LogLevel.DEBUG);
Logger.Log(response.errorMsg, LogLevel.Debug);
} else if (pipes.ContainsKey(request.requestedIdentifier)) {
response.invalidName = true;
response.nameTaken = true;
response.errorMsg = $"Interface requested identifier \"{request.requestedIdentifier}\" is taken.";
Logger.Log(response.errorMsg, LogLevel.DEBUG);
Logger.Log(response.errorMsg, LogLevel.Debug);
} else {
requestAccepted = true;
response.identifier = request.requestedIdentifier;
@ -217,7 +217,7 @@ namespace GameServiceWarden.Core.UI
catch (AggregateException e)
{
e.Handle((exception) => exception is TaskCanceledException);
Logger.Log($"Interface did not receive response within {TIMEOUT}ms.", LogLevel.DEBUG);
Logger.Log($"Interface did not receive response within {TIMEOUT}ms.", LogLevel.Debug);
}
if (!requestAccepted) {
cancelResponse.Dispose();
@ -232,7 +232,7 @@ namespace GameServiceWarden.Core.UI
private async Task Listen(string identifier, NamedPipeServerStream pipe)
{
Logger.Log($"Started listening to interface \"{identifier}\".", LogLevel.DEBUG);
Logger.Log($"Started listening to interface \"{identifier}\".", LogLevel.Debug);
byte[] buffer = new byte[1024];
byte[] headerBuffer = new byte[sizeof(uint) * 2];
byte[] bodyBuffer = null;
@ -274,11 +274,11 @@ namespace GameServiceWarden.Core.UI
}
}
}
Logger.Log($"Pipe for interface \"{identifier}\" has closed.", LogLevel.DEBUG);
Logger.Log($"Pipe for interface \"{identifier}\" has closed.", LogLevel.Debug);
(NamedPipeServerStream, Task) removedPipe;
pipes.TryRemove(identifier, out removedPipe);
await removedPipe.Item1.DisposeAsync();
Logger.Log($"Stopped listening to interface \"{identifier}\".", LogLevel.DEBUG);
Logger.Log($"Stopped listening to interface \"{identifier}\".", LogLevel.Debug);
}
}
}

View File

@ -16,6 +16,7 @@
<ItemGroup>
<ProjectReference Include="..\..\src\GameServiceWarden.Core\GameServiceWarden.Core.csproj" />
<ProjectReference Include="..\..\src\GameServiceWarden.ModuleFramework\GameServiceWarden.ModuleFramework.csproj" />
<ProjectReference Include="..\..\SimpleLogger\SimpleLogger.csproj" />
</ItemGroup>
</Project>

View File

@ -1,11 +1,10 @@
using System.Collections.Generic;
using System.IO;
using GameServiceWarden.Core.Module;
using GameServiceWarden.Core.Logging;
using GameServiceWarden.ModuleFramework;
using Xunit;
using Xunit.Abstractions;
using System.Text;
using SimpleLogger;
namespace GameServiceWarden.Core.Tests.Modules
{

View File

@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.IO;
using GameServiceWarden.Core.Module;
using GameServiceWarden.Core.Logging;
using SimpleLogger;
using GameServiceWarden.ModuleFramework;
using Xunit;
using Xunit.Abstractions;

View File

@ -1,5 +1,5 @@
using System;
using GameServiceWarden.Core.Logging;
using SimpleLogger;
using GameServiceWarden.Core.UI;
using Xunit;
using Xunit.Abstractions;

View File

@ -1,12 +1,12 @@
using System;
using GameServiceWarden.Core.Logging;
using SimpleLogger;
using Xunit.Abstractions;
namespace GameServiceWarden.Core.Tests
{
public class XUnitLogger : ILogReceiver
{
public LogLevel Level => LogLevel.DEBUG;
public LogLevel Level => LogLevel.Debug;
public string Identifier => GetType().Name;