Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
DebugProvider.cs
Go to the documentation of this file.
2
3namespace System.Diagnostics;
4
5public class DebugProvider
6{
7 private sealed class DebugAssertException : Exception
8 {
9 internal DebugAssertException(string message, string detailMessage, string stackTrace)
10 : base(Terminate(message) + Terminate(detailMessage) + stackTrace)
11 {
12 }
13
14 private static string Terminate(string s)
15 {
16 if (s == null)
17 {
18 return s;
19 }
20 s = s.Trim();
21 if (s.Length > 0)
22 {
23 s += "\r\n";
24 }
25 return s;
26 }
27 }
28
29 private static readonly object s_lock = new object();
30
31 private bool _needIndent = true;
32
33 private string _indentString;
34
35 internal static Action<string, string, string, string> s_FailCore;
36
37 internal static Action<string> s_WriteCore;
38
39 private static readonly object s_ForLock = new object();
40
41 [DoesNotReturn]
42 public virtual void Fail(string? message, string? detailMessage)
43 {
44 string stackTrace;
45 try
46 {
47 stackTrace = new StackTrace(0, fNeedFileInfo: true).ToString(StackTrace.TraceFormat.Normal);
48 }
49 catch
50 {
51 stackTrace = "";
52 }
53 WriteAssert(stackTrace, message, detailMessage);
54 FailCore(stackTrace, message, detailMessage, "Assertion failed.");
55 }
56
57 internal void WriteAssert(string stackTrace, string message, string detailMessage)
58 {
59 WriteLine(SR.DebugAssertBanner + "\r\n" + SR.DebugAssertShortMessage + "\r\n" + message + "\r\n" + SR.DebugAssertLongMessage + "\r\n" + detailMessage + "\r\n" + stackTrace);
60 }
61
62 public virtual void Write(string? message)
63 {
64 lock (s_lock)
65 {
66 if (message == null)
67 {
68 WriteCore(string.Empty);
69 return;
70 }
71 if (_needIndent)
72 {
73 message = GetIndentString() + message;
74 _needIndent = false;
75 }
76 WriteCore(message);
77 if (message.EndsWith("\r\n", StringComparison.Ordinal))
78 {
79 _needIndent = true;
80 }
81 }
82 }
83
84 public virtual void WriteLine(string? message)
85 {
86 Write(message + "\r\n");
87 }
88
89 public virtual void OnIndentLevelChanged(int indentLevel)
90 {
91 }
92
93 public virtual void OnIndentSizeChanged(int indentSize)
94 {
95 }
96
97 private string GetIndentString()
98 {
99 int num = Debug.IndentSize * Debug.IndentLevel;
100 string indentString = _indentString;
101 if (indentString != null && indentString.Length == num)
102 {
103 return _indentString;
104 }
105 return _indentString = new string(' ', num);
106 }
107
108 public static void FailCore(string stackTrace, string? message, string? detailMessage, string errorSource)
109 {
110 if (s_FailCore != null)
111 {
112 s_FailCore(stackTrace, message, detailMessage, errorSource);
113 return;
114 }
116 {
117 Debugger.Break();
118 return;
119 }
120 DebugAssertException ex = new DebugAssertException(message, detailMessage, stackTrace);
121 Environment.FailFast(ex.Message, ex, errorSource);
122 }
123
124 public static void WriteCore(string message)
125 {
126 if (s_WriteCore != null)
127 {
128 s_WriteCore(message);
129 return;
130 }
131 lock (s_ForLock)
132 {
133 if (message.Length <= 4091)
134 {
135 WriteToDebugger(message);
136 return;
137 }
138 int i;
139 for (i = 0; i < message.Length - 4091; i += 4091)
140 {
141 WriteToDebugger(message.Substring(i, 4091));
142 }
143 WriteToDebugger(message.Substring(i));
144 }
145 }
146
147 private static void WriteToDebugger(string message)
148 {
149 if (Debugger.IsLogging())
150 {
151 Debugger.Log(0, null, message);
152 }
153 else
154 {
155 Interop.Kernel32.OutputDebugString(message ?? string.Empty);
156 }
157 }
158}
static void OutputDebugString(string message)
DebugAssertException(string message, string detailMessage, string stackTrace)
static readonly object s_lock
static Action< string > s_WriteCore
virtual void OnIndentLevelChanged(int indentLevel)
virtual void Fail(string? message, string? detailMessage)
virtual void Write(string? message)
static void WriteToDebugger(string message)
virtual void OnIndentSizeChanged(int indentSize)
static void FailCore(string stackTrace, string? message, string? detailMessage, string errorSource)
static void WriteCore(string message)
void WriteAssert(string stackTrace, string message, string detailMessage)
static readonly object s_ForLock
static Action< string, string, string, string > s_FailCore
virtual void WriteLine(string? message)
static int IndentLevel
Definition Debug.cs:189
static void Log(int level, string? category, string? message)
Definition Debugger.cs:55
static void FailFast(string? message)
virtual string Message
Definition Exception.cs:100
static string DebugAssertLongMessage
Definition SR.cs:26
static string DebugAssertBanner
Definition SR.cs:22
static string DebugAssertShortMessage
Definition SR.cs:24
Definition SR.cs:7