Skip to content

Commit

Permalink
Split up prefixing and translating logs for unity
Browse files Browse the repository at this point in the history
Should be separate classes.
  • Loading branch information
blowfishpro committed Mar 18, 2018
1 parent 30fd490 commit e152f67
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 16 deletions.
15 changes: 5 additions & 10 deletions ModuleManager/Logging/ModLogger.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
using System;
using UnityEngine;
using ModuleManager.Extensions;

namespace ModuleManager.Logging
{
public class ModLogger : IBasicLogger
{
private string prefix;
private ILogger logger;
private IBasicLogger logger;

public ModLogger(string prefix, ILogger logger)
public ModLogger(string prefix, IBasicLogger logger)
{
if (string.IsNullOrEmpty(prefix)) throw new ArgumentNullException(nameof(prefix));
this.prefix = "[" + prefix + "] ";
this.logger = logger;
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public void Log(LogType logType, string message) => logger.Log(logType, prefix + message);

public void Exception(string message, Exception exception)
{
this.Error(message);
logger.LogException(exception);
}
public void Exception(string message, Exception exception) => logger.Exception(prefix + message, exception);
}
}
24 changes: 24 additions & 0 deletions ModuleManager/Logging/UnityLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using UnityEngine;
using ModuleManager.Extensions;

namespace ModuleManager.Logging
{
public class UnityLogger : IBasicLogger
{
private ILogger logger;

public UnityLogger(ILogger logger)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public void Log(LogType logType, string message) => logger.Log(logType, message);

public void Exception(string message, Exception exception)
{
this.Error(message);
logger.LogException(exception);
}
}
}
2 changes: 1 addition & 1 deletion ModuleManager/MMPatchLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private void Awake()
partDatabasePath = Path.Combine(KSPUtil.ApplicationRootPath, "PartDatabase.cfg");
shaPath = Path.Combine(Path.Combine(KSPUtil.ApplicationRootPath, "GameData"), "ModuleManager.ConfigSHA");

logger = new ModLogger("ModuleManager", Debug.unityLogger);
logger = new ModLogger("ModuleManager", new UnityLogger(Debug.unityLogger));
}

private bool ready;
Expand Down
1 change: 1 addition & 0 deletions ModuleManager/ModuleManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<Compile Include="Logging\NormalMessage.cs" />
<Compile Include="Logging\QueueLogger.cs" />
<Compile Include="Collections\MessageQueue.cs" />
<Compile Include="Logging\UnityLogger.cs" />
<Compile Include="MMPatchLoader.cs" />
<Compile Include="ModuleManager.cs" />
<Compile Include="ModListGenerator.cs" />
Expand Down
42 changes: 37 additions & 5 deletions ModuleManagerTests/Logging/ModLoggerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,48 @@ namespace ModuleManagerTests.Logging
{
public class ModLoggerTest
{
private ILogger innerLogger;
private IBasicLogger innerLogger;
private ModLogger logger;

public ModLoggerTest()
{
innerLogger = Substitute.For<ILogger>();
innerLogger = Substitute.For<IBasicLogger>();
logger = new ModLogger("MyMod", innerLogger);
}

[Fact]
public void TestConstructor__PrefixNull()
{
ArgumentNullException e = Assert.Throws<ArgumentNullException>(delegate
{
new ModLogger(null, innerLogger);
});

Assert.Equal("prefix", e.ParamName);
}

[Fact]
public void TestConstructor__PrefixBlank()
{
ArgumentNullException e = Assert.Throws<ArgumentNullException>(delegate
{
new ModLogger("", innerLogger);
});

Assert.Equal("prefix", e.ParamName);
}

[Fact]
public void TestConstructor__LoggerNull()
{
ArgumentNullException e = Assert.Throws<ArgumentNullException>(delegate
{
new ModLogger("blah", null);
});

Assert.Equal("logger", e.ParamName);
}

[Fact]
public void TestLog__Info()
{
Expand Down Expand Up @@ -46,9 +79,8 @@ public void TestException()
{
Exception e = new Exception();
logger.Exception("An exception was thrown", e);

innerLogger.Received().Log(LogType.Error, "[MyMod] An exception was thrown");
innerLogger.Received().LogException(e);

innerLogger.Received().Exception("[MyMod] An exception was thrown", e);
}
}
}
65 changes: 65 additions & 0 deletions ModuleManagerTests/Logging/UnityLoggerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using Xunit;
using NSubstitute;
using UnityEngine;
using ModuleManager.Logging;

namespace ModuleManagerTests.Logging
{
public class UnityLoggerTest
{
private ILogger innerLogger;
private UnityLogger logger;

public UnityLoggerTest()
{
innerLogger = Substitute.For<ILogger>();
logger = new UnityLogger(innerLogger);
}

[Fact]
public void TestConstructor__LoggerNull()
{
ArgumentNullException e = Assert.Throws<ArgumentNullException>(delegate
{
new UnityLogger(null);
});

Assert.Equal("logger", e.ParamName);
}

[Fact]
public void TestLog__Info()
{
logger.Log(LogType.Log, "well hi there");

innerLogger.Received().Log(LogType.Log, "well hi there");
}

[Fact]
public void TestLog__Warning()
{
logger.Log(LogType.Warning, "I'm warning you");

innerLogger.Received().Log(LogType.Warning, "I'm warning you");
}

[Fact]
public void TestLog__Error()
{
logger.Log(LogType.Error, "You have made a grave mistake");

innerLogger.Received().Log(LogType.Error, "You have made a grave mistake");
}

[Fact]
public void TestException()
{
Exception e = new Exception();
logger.Exception("An exception was thrown", e);

innerLogger.Received().Log(LogType.Error, "An exception was thrown");
innerLogger.Received().LogException(e);
}
}
}
1 change: 1 addition & 0 deletions ModuleManagerTests/ModuleManagerTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<Compile Include="Extensions\IBasicLoggerExtensionsTest.cs" />
<Compile Include="Extensions\StringExtensionsTest.cs" />
<Compile Include="Extensions\UrlConfigExtensionsTest.cs" />
<Compile Include="Logging\UnityLoggerTest.cs" />
<Compile Include="NeedsCheckerTest.cs" />
<Compile Include="Collections\MessageQueueTest.cs" />
<Compile Include="Logging\ExceptionMessageTest.cs" />
Expand Down

0 comments on commit e152f67

Please sign in to comment.