-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding log handler that sets console color (#2001)
* adding log handler that sets console color * fixing stack trace log * removing empty cases * removing string.Format
- Loading branch information
1 parent
268d93f
commit 4623978
Showing
7 changed files
with
100 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using UnityEngine; | ||
|
||
namespace Mirror.Logging | ||
{ | ||
public class ConsoleColorLogHandler : ILogHandler | ||
{ | ||
readonly bool showExceptionStackTrace; | ||
|
||
public ConsoleColorLogHandler(bool showExceptionStackTrace) | ||
{ | ||
this.showExceptionStackTrace = showExceptionStackTrace; | ||
} | ||
|
||
public void LogException(Exception exception, UnityEngine.Object context) | ||
{ | ||
Console.ForegroundColor = ConsoleColor.Red; | ||
Console.WriteLine("Exception: {0}", exception.Message); | ||
if (showExceptionStackTrace) | ||
{ | ||
Console.WriteLine(" {0}", exception.StackTrace); | ||
} | ||
Console.ResetColor(); | ||
} | ||
|
||
public void LogFormat(LogType logType, UnityEngine.Object context, string format, params object[] args) | ||
{ | ||
switch (logType) | ||
{ | ||
case LogType.Exception: | ||
case LogType.Error: | ||
case LogType.Assert: | ||
Console.ForegroundColor = ConsoleColor.Red; | ||
break; | ||
case LogType.Warning: | ||
Console.ForegroundColor = ConsoleColor.Yellow; | ||
break; | ||
} | ||
|
||
Console.WriteLine(format, args); | ||
Console.ResetColor(); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Mirror/Runtime/Logging/ConsoleColorLogHandler.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,23 @@ | ||
using UnityEngine; | ||
|
||
namespace Mirror.Logging | ||
{ | ||
/// <summary> | ||
/// Used to replace log hanlder with Console Color LogHandler | ||
/// </summary> | ||
[DisallowMultipleComponent] | ||
[AddComponentMenu("Network/NetworkHeadlessLogger")] | ||
[HelpURL("https://mirror-networking.com/docs/Components/NetworkHeadlessLogger.html")] | ||
public class NetworkHeadlessLogger : MonoBehaviour | ||
{ | ||
[SerializeField] bool showExceptionStackTrace = false; | ||
|
||
void Awake() | ||
{ | ||
if (NetworkManager.isHeadless) | ||
{ | ||
LogFactory.ReplaceLogHandler(new ConsoleColorLogHandler(showExceptionStackTrace)); | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Mirror/Runtime/Logging/NetworkHeadlessLogger.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,9 @@ | ||
# Network Headless Logger | ||
|
||
Network Headless Logger replaces the the default log handler with one that set `Console.ForegroundColor`. | ||
|
||
Only replaces the handler when running in headless mode. | ||
|
||
![Inspector](NetworkHeadlessLogger.png) | ||
|
||
`showExceptionStackTrace` will log the stack trace of any exceptions sent to the handler. |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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