暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RenderGraphLogger.cs 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. namespace UnityEngine.Rendering.RenderGraphModule
  5. {
  6. struct RenderGraphLogIndent : IDisposable
  7. {
  8. int m_Indentation;
  9. RenderGraphLogger m_Logger;
  10. bool m_Disposed;
  11. public RenderGraphLogIndent(RenderGraphLogger logger, int indentation = 1)
  12. {
  13. m_Disposed = false;
  14. m_Indentation = indentation;
  15. m_Logger = logger;
  16. m_Logger.IncrementIndentation(m_Indentation);
  17. }
  18. public void Dispose()
  19. {
  20. Dispose(true);
  21. }
  22. void Dispose(bool disposing)
  23. {
  24. Debug.Assert(m_Logger != null, "RenderGraphLogIndent: logger parameter should not be null.");
  25. if (m_Disposed)
  26. return;
  27. if (disposing && m_Logger != null)
  28. {
  29. m_Logger.DecrementIndentation(m_Indentation);
  30. }
  31. m_Disposed = true;
  32. }
  33. }
  34. class RenderGraphLogger
  35. {
  36. Dictionary<string, StringBuilder> m_LogMap = new Dictionary<string, StringBuilder>(); // Can log multiple instances before flush everything.
  37. StringBuilder m_CurrentBuilder;
  38. int m_CurrentIndentation;
  39. public void Initialize(string logName)
  40. {
  41. if (!m_LogMap.TryGetValue(logName, out var stringBuilder))
  42. {
  43. stringBuilder = new StringBuilder();
  44. m_LogMap.Add(logName, stringBuilder);
  45. }
  46. m_CurrentBuilder = stringBuilder;
  47. m_CurrentBuilder.Clear();
  48. m_CurrentIndentation = 0;
  49. }
  50. public void IncrementIndentation(int value)
  51. {
  52. m_CurrentIndentation += Math.Abs(value);
  53. }
  54. public void DecrementIndentation(int value)
  55. {
  56. m_CurrentIndentation = Math.Max(0, m_CurrentIndentation - Math.Abs(value));
  57. }
  58. public void LogLine(string format, params object[] args)
  59. {
  60. for (int i = 0; i < m_CurrentIndentation; ++i)
  61. m_CurrentBuilder.Append('\t');
  62. m_CurrentBuilder.AppendFormat(format, args);
  63. m_CurrentBuilder.AppendLine();
  64. }
  65. public string GetLog(string logName)
  66. {
  67. if (m_LogMap.TryGetValue(logName, out var builder))
  68. {
  69. return builder.ToString();
  70. }
  71. return "";
  72. }
  73. public string GetAllLogs()
  74. {
  75. string result = "";
  76. foreach (var kvp in m_LogMap)
  77. {
  78. var builder = kvp.Value;
  79. builder.AppendLine();
  80. result += builder.ToString();
  81. }
  82. m_LogMap.Clear();
  83. return result;
  84. }
  85. }
  86. }