UnityDebugSink.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine;
  5. namespace UniRx.Diagnostics
  6. {
  7. public class UnityDebugSink : IObserver<LogEntry>
  8. {
  9. public void OnCompleted()
  10. {
  11. // do nothing
  12. }
  13. public void OnError(Exception error)
  14. {
  15. // do nothing
  16. }
  17. public void OnNext(LogEntry value)
  18. {
  19. // avoid multithread exception.
  20. // (value.Context == null) can only be called from the main thread.
  21. var ctx = (System.Object)value.Context;
  22. switch (value.LogType)
  23. {
  24. case LogType.Error:
  25. if (ctx == null)
  26. {
  27. Debug.LogError(value.Message);
  28. }
  29. else
  30. {
  31. Debug.LogError(value.Message, value.Context);
  32. }
  33. break;
  34. case LogType.Exception:
  35. if (ctx == null)
  36. {
  37. Debug.LogException(value.Exception);
  38. }
  39. else
  40. {
  41. Debug.LogException(value.Exception, value.Context);
  42. }
  43. break;
  44. case LogType.Log:
  45. if (ctx == null)
  46. {
  47. Debug.Log(value.Message);
  48. }
  49. else
  50. {
  51. Debug.Log(value.Message, value.Context);
  52. }
  53. break;
  54. case LogType.Warning:
  55. if (ctx == null)
  56. {
  57. Debug.LogWarning(value.Message);
  58. }
  59. else
  60. {
  61. Debug.LogWarning(value.Message, value.Context);
  62. }
  63. break;
  64. default:
  65. break;
  66. }
  67. }
  68. }
  69. }