Skip to content

Instantly share code, notes, and snippets.

@neuecc
Created December 22, 2016 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neuecc/d97812411e2f38d8530c5f76c337408f to your computer and use it in GitHub Desktop.
Save neuecc/d97812411e2f38d8530c5f76c337408f to your computer and use it in GitHub Desktop.
using Grpc.Core.Logging;
using System;
namespace MagicOnion
{
public class UnityDebugLogger : ILogger
{
readonly Type forType;
readonly string forTypeString;
public UnityDebugLogger()
: this(null)
{
}
protected UnityDebugLogger(Type forType)
{
this.forType = forType;
if (forType != null)
{
var namespaceStr = forType.Namespace ?? "";
if (namespaceStr.Length > 0)
{
namespaceStr += ".";
}
this.forTypeString = namespaceStr + forType.Name + " ";
}
else
{
this.forTypeString = "";
}
}
/// <summary>
/// Returns a logger associated with the specified type.
/// </summary>
public virtual ILogger ForType<T>()
{
if (typeof(T) == forType)
{
return this;
}
return new UnityDebugLogger(typeof(T));
}
/// <summary>Logs a message with severity Debug.</summary>
public void Debug(string message)
{
UnityEngine.Debug.Log(BuildMessage(message));
}
/// <summary>Logs a formatted message with severity Debug.</summary>
public void Debug(string format, params object[] formatArgs)
{
UnityEngine.Debug.Log(BuildMessage(format, formatArgs));
}
/// <summary>Logs a message with severity Info.</summary>
public void Info(string message)
{
UnityEngine.Debug.Log(BuildMessage(message));
}
/// <summary>Logs a formatted message with severity Info.</summary>
public void Info(string format, params object[] formatArgs)
{
UnityEngine.Debug.Log(BuildMessage(format, formatArgs));
}
/// <summary>Logs a message with severity Warning.</summary>
public void Warning(string message)
{
UnityEngine.Debug.LogWarning(BuildMessage(message));
}
/// <summary>Logs a formatted message with severity Warning.</summary>
public void Warning(string format, params object[] formatArgs)
{
UnityEngine.Debug.LogWarning(BuildMessage(format, formatArgs));
}
/// <summary>Logs a message and an associated exception with severity Warning.</summary>
public void Warning(Exception exception, string message)
{
Warning(message + " " + exception);
}
/// <summary>Logs a message with severity Error.</summary>
public void Error(string message)
{
UnityEngine.Debug.LogError(BuildMessage(message));
}
/// <summary>Logs a formatted message with severity Error.</summary>
public void Error(string format, params object[] formatArgs)
{
UnityEngine.Debug.LogError(BuildMessage(format, formatArgs));
}
/// <summary>Logs a message and an associated exception with severity Error.</summary>
public void Error(Exception exception, string message)
{
Error(message + " " + exception);
}
/// <summary>Gets the type associated with this logger.</summary>
protected Type AssociatedType
{
get { return forType; }
}
string BuildMessage(string message)
{
if (forType != null)
{
return forTypeString + message;
}
else
{
return message;
}
}
string BuildMessage(string format, object[] args)
{
if (forType != null)
{
return forTypeString + string.Format(format, args);
}
else
{
return string.Format(format, args);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment