DefaultLogFormatter.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. sb.AppendFormat("{0:d4}", args.ThreadId);
  18. sb.Append(' ');
  19. sb.Append(args.Message);
  20. if (args.Data != null)
  21. {
  22. PropertyInfo[] props = args.Data.GetType().GetProperties();
  23. sb.Append(' ');
  24. foreach (var prop in props)
  25. {
  26. sb.Append(prop.Name);
  27. sb.Append(": ");
  28. if (typeof(Array).IsAssignableFrom(prop.PropertyType))
  29. {
  30. Array enumerable = (Array)prop.GetValue(args.Data);
  31. foreach (var item in enumerable)
  32. {
  33. sb.Append(item.ToString());
  34. sb.Append(", ");
  35. }
  36. sb.Remove(sb.Length - 2, 2);
  37. }
  38. else
  39. {
  40. sb.Append(prop.GetValue(args.Data));
  41. }
  42. sb.Append(" - ");
  43. }
  44. // We remove the final '-' from the string
  45. if (props.Length > 0)
  46. {
  47. sb.Remove(sb.Length - 3, 3);
  48. }
  49. }
  50. return sb.ToString();
  51. }
  52. finally
  53. {
  54. _stringBuilderPool.Release(sb);
  55. }
  56. }
  57. }
  58. }