DefaultLogFormatter.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Reflection;
  3. using System.Text;
  4. namespace Ryujinx.Common.Logging
  5. {
  6. internal class DefaultLogFormatter : ILogFormatter
  7. {
  8. private static readonly ObjectPool<StringBuilder> _stringBuilderPool = SharedPools.Default<StringBuilder>();
  9. public string Format(LogEventArgs args)
  10. {
  11. StringBuilder sb = _stringBuilderPool.Allocate();
  12. try
  13. {
  14. sb.Clear();
  15. sb.AppendFormat(@"{0:hh\:mm\:ss\.fff}", args.Time);
  16. sb.Append(" | ");
  17. if (args.ThreadName != null)
  18. {
  19. sb.Append(args.ThreadName);
  20. sb.Append(' ');
  21. }
  22. sb.Append(args.Message);
  23. if (args.Data != null)
  24. {
  25. PropertyInfo[] props = args.Data.GetType().GetProperties();
  26. sb.Append(' ');
  27. foreach (var prop in props)
  28. {
  29. sb.Append(prop.Name);
  30. sb.Append(": ");
  31. if (typeof(Array).IsAssignableFrom(prop.PropertyType))
  32. {
  33. Array enumerable = (Array)prop.GetValue(args.Data);
  34. foreach (var item in enumerable)
  35. {
  36. sb.Append(item.ToString());
  37. sb.Append(", ");
  38. }
  39. sb.Remove(sb.Length - 2, 2);
  40. }
  41. else
  42. {
  43. sb.Append(prop.GetValue(args.Data));
  44. }
  45. sb.Append(" - ");
  46. }
  47. // We remove the final '-' from the string
  48. if (props.Length > 0)
  49. {
  50. sb.Remove(sb.Length - 3, 3);
  51. }
  52. }
  53. return sb.ToString();
  54. }
  55. finally
  56. {
  57. _stringBuilderPool.Release(sb);
  58. }
  59. }
  60. }
  61. }