Skip to content

Commit 893e5b0

Browse files
committed
Add handy DumpField method to Logger
1 parent 70bbe1a commit 893e5b0

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

Diff for: beats2/Assets/Scripts/Tools/Logger.cs

+31-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
*/
77
using System;
88
using System.Collections.Generic;
9+
using System.Reflection;
910
using System.Text;
1011
using UnityEngine;
12+
using System.Collections;
1113

1214
namespace Beats2
1315
{
@@ -84,7 +86,35 @@ public static string GetLogHistoryString()
8486
}
8587
return logHistory.ToString();
8688
}
87-
89+
90+
public static string DumpFields(object obj, int indentCount = 0)
91+
{
92+
string indent = new string('\t', indentCount);
93+
string indentInner = new string('\t', indentCount + 1);
94+
StringBuilder properties = new StringBuilder();
95+
96+
properties.AppendLine(indent + "{");
97+
98+
// Use reflection to get list of properties and their values
99+
Type type = obj.GetType();
100+
foreach (FieldInfo field in type.GetFields()) {
101+
string name = field.Name;
102+
object value = field.GetValue(obj);
103+
if (value is ICollection) {
104+
// Recursively print inner objects
105+
properties.AppendLine(string.Format("{0}{1} =", indentInner, name));
106+
foreach (ICollection item in ((ICollection)value)) {
107+
properties.AppendLine(DumpFields(item, indentCount + 1));
108+
}
109+
} else {
110+
properties.AppendLine(string.Format("{0}{1} = {2}", indentInner, name, value));
111+
}
112+
}
113+
properties.AppendLine(indent + "}");
114+
115+
return properties.ToString();
116+
}
117+
88118
private static string FormatLogString(string type, string tag, string format, params object[] args)
89119
{
90120
return String.Format("{0}|{1}: {2}", type, tag, String.Format(format, args));

0 commit comments

Comments
 (0)