UnityOutput.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. namespace Best.HTTP.Shared.Logger
  3. {
  4. /// <summary>
  5. /// Provides an implementation of <see cref="ILogOutput"/> that writes log messages to the Unity Debug Console.
  6. /// </summary>
  7. public sealed class UnityOutput : ILogOutput
  8. {
  9. /// <summary>
  10. /// Gets a value indicating whether this log output accepts color codes.
  11. /// </summary>
  12. /// <remarks>
  13. /// This property returns <c>true</c> when running in the Unity Editor and <c>false</c> otherwise.
  14. /// </remarks>
  15. public bool AcceptColor { get; } = UnityEngine.Application.isEditor;
  16. /// <summary>
  17. /// Writes a log message to the Unity Debug Console based on the specified log level.
  18. /// </summary>
  19. /// <param name="level">The log level of the message.</param>
  20. /// <param name="logEntry">The log message to write.</param>
  21. public void Write(Loglevels level, string logEntry)
  22. {
  23. switch (level)
  24. {
  25. case Loglevels.All:
  26. case Loglevels.Information:
  27. UnityEngine.Debug.Log(logEntry);
  28. break;
  29. case Loglevels.Warning:
  30. UnityEngine.Debug.LogWarning(logEntry);
  31. break;
  32. case Loglevels.Error:
  33. case Loglevels.Exception:
  34. UnityEngine.Debug.LogError(logEntry);
  35. break;
  36. }
  37. }
  38. /// <summary>
  39. /// This implementation does nothing.
  40. /// </summary>
  41. void ILogOutput.Flush() {}
  42. void IDisposable.Dispose() => GC.SuppressFinalize(this);
  43. }
  44. }