forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInspect.cs
239 lines (213 loc) · 9.38 KB
/
Inspect.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using ServiceStack.Logging;
using ServiceStack.Script;
using ServiceStack.Text;
namespace ServiceStack
{
/// <summary>
/// Helper utility for inspecting variables
/// </summary>
public static class Inspect
{
public static class Config
{
public const string VarsName = "vars.json";
public static Action<object> VarsFilter { get; set; } = DefaultVarsFilter;
public static Func<object,string> DumpTableFilter { get; set; }
public static void DefaultVarsFilter(object anonArgs)
{
try
{
var inspectVarsPath = Environment.GetEnvironmentVariable("INSPECT_VARS");
if (string.IsNullOrEmpty(inspectVarsPath)) // Disable
return;
var varsPath = Path.DirectorySeparatorChar == '\\'
? inspectVarsPath.Replace('/','\\')
: inspectVarsPath.Replace('\\','/');
if (varsPath.IndexOf(Path.DirectorySeparatorChar) >= 0)
Path.GetDirectoryName(varsPath).AssertDir();
File.WriteAllText(varsPath, anonArgs.ToSafeJson());
}
catch (Exception ex)
{
Tracer.Instance.WriteError("Inspect.vars() Error: " + ex);
}
}
}
/// <summary>
/// Dump serialized values to 'vars.json'
/// </summary>
/// <param name="anonArgs">Anonymous object with named value</param>
// ReSharper disable once InconsistentNaming
public static void vars(object anonArgs) => Config.VarsFilter?.Invoke(anonArgs);
/// <summary>
/// Recursively prints the contents of any POCO object in a human-friendly, readable format
/// </summary>
public static string dump<T>(T instance)
{
try
{
// convert into common common collection type for generic dump routine
var use = UseType(instance);
return dumpInternal(use);
}
catch (Exception e)
{
var log = LogManager.GetLogger(typeof(Inspect));
if (log.IsDebugEnabled)
log.Debug($"Could not pretty print {typeof(T).Name}", e);
}
return instance.Dump();
}
private static object UseType<T>(T instance)
{
if (typeof(T).IsValueType || typeof(T) == typeof(string))
return instance;
if (instance is IEnumerable e)
{
var elType = EnumerableUtils.FirstOrDefault(e);
if (elType?.GetType().GetTypeWithGenericTypeDefinitionOf(typeof(KeyValuePair<,>)) != null)
return instance.ToObjectDictionary();
return new List<object>(e.Cast<object>());
}
return instance.ToObjectDictionary();
}
/// <summary>
/// Print Dump to Console.WriteLine
/// </summary>
public static void printDump<T>(T instance) => PclExport.Instance.WriteLine(dump(instance));
/// <summary>
/// Dump object in Ascii Markdown table
/// </summary>
public static string dumpTable(object instance) => DefaultScripts.TextDump(instance, null);
/// <summary>
/// Dump object in Ascii Markdown table
/// </summary>
public static string dumpTable(object instance, TextDumpOptions options) => DefaultScripts.TextDump(instance, options);
/// <summary>
/// Dump object in Ascii Markdown table using specified column headers
/// </summary>
public static string dumpTable(object instance, string[] headers) =>
DefaultScripts.TextDump(instance, new TextDumpOptions { Headers = headers, IncludeRowNumbers = false });
/// <summary>
/// Print Dump object in Ascii Markdown table
/// </summary>
public static void printDumpTable(object instance) => PclExport.Instance.WriteLine(dumpTable(instance));
/// <summary>
/// Print Dump object in Ascii Markdown table using specified column headers
/// </summary>
public static void printDumpTable(object instance, string[] headers) =>
PclExport.Instance.WriteLine(dumpTable(instance, new TextDumpOptions { Headers = headers, IncludeRowNumbers = false }));
/// <summary>
/// Recursively prints the contents of any POCO object to HTML
/// </summary>
public static string htmlDump(object target) => HtmlScripts.HtmlDump(target, null);
/// <summary>
/// Recursively prints the contents of any POCO object to HTML
/// </summary>
public static string htmlDump(object target, HtmlDumpOptions options) => HtmlScripts.HtmlDump(target, options);
/// <summary>
/// Recursively prints the contents of any POCO object to HTML with specified columns
/// </summary>
public static string htmlDump(object target, string[] headers) =>
HtmlScripts.HtmlDump(target, new HtmlDumpOptions { Headers = headers });
/// <summary>
/// Print htmlDump object
/// </summary>
public static void printHtmlDump(object instance) => PclExport.Instance.WriteLine(htmlDump(instance));
/// <summary>
/// Print htmlDump object with specified columns
/// </summary>
public static void printHtmlDump(object instance, string[] headers) =>
PclExport.Instance.WriteLine(htmlDump(instance, headers));
private static string dumpInternal(object instance)
{
if (instance is Dictionary<string, object> obj)
{
var sb = StringBuilderCache.Allocate();
var keyLen = obj.Keys.Map(x => x.Length).Max() + 2;
foreach (var entry in obj)
{
var k = entry.Key;
var value = entry.Value;
var key = k + ":";
if (value is Dictionary<string, object> nestedObj)
{
if (nestedObj.Count > 0)
{
sb.AppendLine($"{key}");
sb.AppendLine(nestedObj.Dump());
}
}
else if (value is List<object> nestedList)
{
if (nestedList.Count > 0)
{
sb.AppendOneNewLine();
if (nestedList.Count == 1)
{
sb.AppendLine($"[{k}]");
sb.AppendLine(dumpInternal(nestedList[0]));
}
else
{
var showNakedList = nestedList
.All(x => x is not Dictionary<string, object> and not List<object>);
var showTable = nestedList
.All(x => x is Dictionary<string, object> nestedItem && nestedItem.Values
.All(y => y is not Dictionary<string, object> and not List<object>));
sb.AppendLine(key);
if (showNakedList)
{
foreach (var item in nestedList)
{
sb.AppendLine($" {item}");
}
}
else if (showTable)
{
sb.AppendLine(nestedList.DumpTable());
}
else
{
sb.AppendLine(nestedList.Dump());
}
}
sb.AppendOneNewLine();
}
}
else
{
sb.AppendLine($"{key.PadRight(keyLen, ' ')} {value}");
}
}
return StringBuilderCache.ReturnAndFree(sb).TrimStart();
}
if (instance is List<object> list)
return list.DumpTable();
return instance.Dump();
}
private static StringBuilder AppendOneNewLine(this StringBuilder sb)
{
if (sb.Length <= 2)
{
sb.AppendLine();
}
else
{
var c1 = sb[sb.Length - 1];
var c2 = sb[sb.Length - 2];
var hasNewLine = c1 == '\n' && c2 == '\n'
|| (sb.Length > 4 && c1 == '\n' && c2 == '\r' && sb[sb.Length - 3] == '\n' && sb[sb.Length - 4] == '\r');
if (!hasNewLine)
sb.AppendLine();
}
return sb;
}
}
}