Skip to content

Commit

Permalink
feat: adding log handler that sets console color (#2001)
Browse files Browse the repository at this point in the history
* adding log handler that sets console color

* fixing stack trace log

* removing empty cases

* removing string.Format
  • Loading branch information
James-Frowen committed Jun 15, 2020
1 parent 268d93f commit 4623978
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Assets/Mirror/Runtime/Logging/ConsoleColorLogHandler.cs
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 Assets/Mirror/Runtime/Logging/ConsoleColorLogHandler.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions Assets/Mirror/Runtime/Logging/NetworkHeadlessLogger.cs
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 Assets/Mirror/Runtime/Logging/NetworkHeadlessLogger.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions doc/Components/NetworkHeadlessLogger.md
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.
Binary file added doc/Components/NetworkHeadlessLogger.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions doc/Components/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ These core components are included in Mirror:
The Network Identity component is at the heart of the Mirror networking high-level API. It controls a game object’s unique identity on the network, and it uses that identity to make the networking system aware of the game object. It offers two different options for configuration and they are mutually exclusive, which means either one of the options or none can be checked.
- [Network Log Settings](NetworkLogSettings.md)
The Network Log Settings component allows you to configure logging levels and load the settings in a build.
- [Network Headless Logger](NetworkHeadlessLogger.md)
Network Headless Logger adds color to log when running in headless mode
- [Network Manager](NetworkManager.md)
The Network Manager is a component for managing the networking aspects of a multiplayer game.
- [Network Manager HUD](NetworkManagerHUD.md)
Expand Down

0 comments on commit 4623978

Please sign in to comment.