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