-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split up prefixing and translating logs for unity
Should be separate classes.
- Loading branch information
1 parent
30fd490
commit e152f67
Showing
7 changed files
with
134 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters