LoggerAdapter.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using Avalonia.Utilities;
  2. using System;
  3. using System.Text;
  4. namespace Ryujinx.Ava.UI.Helper
  5. {
  6. using AvaLogger = Avalonia.Logging.Logger;
  7. using AvaLogLevel = Avalonia.Logging.LogEventLevel;
  8. using RyuLogger = Ryujinx.Common.Logging.Logger;
  9. using RyuLogClass = Ryujinx.Common.Logging.LogClass;
  10. internal class LoggerAdapter : Avalonia.Logging.ILogSink
  11. {
  12. public static void Register()
  13. {
  14. AvaLogger.Sink = new LoggerAdapter();
  15. }
  16. private static RyuLogger.Log? GetLog(AvaLogLevel level)
  17. {
  18. return level switch
  19. {
  20. AvaLogLevel.Verbose => RyuLogger.Trace,
  21. AvaLogLevel.Debug => RyuLogger.Debug,
  22. AvaLogLevel.Information => RyuLogger.Info,
  23. AvaLogLevel.Warning => RyuLogger.Warning,
  24. AvaLogLevel.Error => RyuLogger.Error,
  25. AvaLogLevel.Fatal => RyuLogger.Notice,
  26. _ => throw new ArgumentOutOfRangeException(nameof(level), level, null)
  27. };
  28. }
  29. public bool IsEnabled(AvaLogLevel level, string area)
  30. {
  31. return GetLog(level) != null;
  32. }
  33. public void Log(AvaLogLevel level, string area, object source, string messageTemplate)
  34. {
  35. GetLog(level)?.PrintMsg(RyuLogClass.Ui, Format(area, messageTemplate, source, null));
  36. }
  37. public void Log<T0>(AvaLogLevel level, string area, object source, string messageTemplate, T0 propertyValue0)
  38. {
  39. GetLog(level)?.PrintMsg(RyuLogClass.Ui, Format(area, messageTemplate, source, new object[] { propertyValue0 }));
  40. }
  41. public void Log<T0, T1>(AvaLogLevel level, string area, object source, string messageTemplate, T0 propertyValue0, T1 propertyValue1)
  42. {
  43. GetLog(level)?.PrintMsg(RyuLogClass.Ui, Format(area, messageTemplate, source, new object[] { propertyValue0, propertyValue1 }));
  44. }
  45. public void Log<T0, T1, T2>(AvaLogLevel level, string area, object source, string messageTemplate, T0 propertyValue0, T1 propertyValue1, T2 propertyValue2)
  46. {
  47. GetLog(level)?.PrintMsg(RyuLogClass.Ui, Format(area, messageTemplate, source, new object[] { propertyValue0, propertyValue1, propertyValue2 }));
  48. }
  49. public void Log(AvaLogLevel level, string area, object source, string messageTemplate, params object[] propertyValues)
  50. {
  51. GetLog(level)?.PrintMsg(RyuLogClass.Ui, Format(area, messageTemplate, source, propertyValues));
  52. }
  53. private static string Format(string area, string template, object source, object[] v)
  54. {
  55. var result = new StringBuilder();
  56. var r = new CharacterReader(template.AsSpan());
  57. var i = 0;
  58. result.Append('[');
  59. result.Append(area);
  60. result.Append("] ");
  61. while (!r.End)
  62. {
  63. var c = r.Take();
  64. if (c != '{')
  65. {
  66. result.Append(c);
  67. }
  68. else
  69. {
  70. if (r.Peek != '{')
  71. {
  72. result.Append('\'');
  73. result.Append(i < v.Length ? v[i++] : null);
  74. result.Append('\'');
  75. r.TakeUntil('}');
  76. r.Take();
  77. }
  78. else
  79. {
  80. result.Append('{');
  81. r.Take();
  82. }
  83. }
  84. }
  85. if (source != null)
  86. {
  87. result.Append(" (");
  88. result.Append(source.GetType().Name);
  89. result.Append(" #");
  90. result.Append(source.GetHashCode());
  91. result.Append(')');
  92. }
  93. return result.ToString();
  94. }
  95. }
  96. }