Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
TextWriterTraceListener.cs
Go to the documentation of this file.
1using System.IO;
2using System.Text;
3
4namespace System.Diagnostics;
5
7{
9
10 private string _fileName;
11
12 public TextWriter? Writer
13 {
14 get
15 {
17 return _writer;
18 }
19 set
20 {
21 _writer = value;
22 }
23 }
24
26 {
27 }
28
30 : this(stream, string.Empty)
31 {
32 }
33
35 : base(name)
36 {
37 if (stream == null)
38 {
39 throw new ArgumentNullException("stream");
40 }
42 }
43
48
50 : base(name)
51 {
52 if (writer == null)
53 {
54 throw new ArgumentNullException("writer");
55 }
57 }
58
59 public TextWriterTraceListener(string? fileName)
60 {
61 _fileName = fileName;
62 }
63
64 public TextWriterTraceListener(string? fileName, string? name)
65 : base(name)
66 {
67 _fileName = fileName;
68 }
69
70 public override void Close()
71 {
72 if (_writer != null)
73 {
74 try
75 {
76 _writer.Close();
77 }
79 {
80 }
81 _writer = null;
82 }
83 _fileName = null;
84 }
85
86 protected override void Dispose(bool disposing)
87 {
88 try
89 {
90 if (disposing && _writer != null)
91 {
93 }
94 }
95 finally
96 {
97 base.Dispose(disposing);
98 }
99 }
100
101 public override void Flush()
102 {
103 EnsureWriter();
104 try
105 {
106 _writer?.Flush();
107 }
109 {
110 }
111 }
112
113 public override void Write(string? message)
114 {
115 EnsureWriter();
116 if (_writer != null)
117 {
118 if (base.NeedIndent)
119 {
120 WriteIndent();
121 }
122 try
123 {
124 _writer.Write(message);
125 }
127 {
128 }
129 }
130 }
131
132 public override void WriteLine(string? message)
133 {
134 EnsureWriter();
135 if (_writer != null)
136 {
137 if (base.NeedIndent)
138 {
139 WriteIndent();
140 }
141 try
142 {
143 _writer.WriteLine(message);
144 base.NeedIndent = true;
145 }
147 {
148 }
149 }
150 }
151
153 {
154 Encoding encoding2 = (Encoding)encoding.Clone();
155 encoding2.EncoderFallback = EncoderFallback.ReplacementFallback;
156 encoding2.DecoderFallback = DecoderFallback.ReplacementFallback;
157 return encoding2;
158 }
159
160 internal void EnsureWriter()
161 {
162 if (_writer != null)
163 {
164 return;
165 }
166 bool flag = false;
167 if (_fileName == null)
168 {
169 return;
170 }
171 Encoding encodingWithFallback = GetEncodingWithFallback(new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
172 string path = Path.GetFullPath(_fileName);
173 string directoryName = Path.GetDirectoryName(path);
174 string text = Path.GetFileName(path);
175 for (int i = 0; i < 2; i++)
176 {
177 try
178 {
179 _writer = new StreamWriter(path, append: true, encodingWithFallback, 4096);
180 flag = true;
181 }
182 catch (IOException)
183 {
184 text = $"{Guid.NewGuid()}{text}";
185 path = Path.Combine(directoryName, text);
186 continue;
187 }
189 {
190 }
191 catch (Exception)
192 {
193 }
194 break;
195 }
196 if (!flag)
197 {
198 _fileName = null;
199 }
200 }
201
202 internal bool IsEnabled(TraceOptions opts)
203 {
204 return (opts & base.TraceOutputOptions) != 0;
205 }
206}
TextWriterTraceListener(TextWriter writer, string? name)
static Encoding GetEncodingWithFallback(Encoding encoding)
TextWriterTraceListener(string? fileName, string? name)
static string Combine(string path1, string path2)
Definition Path.cs:304
static ? string GetFileName(string? path)
Definition Path.cs:200
static string GetFullPath(string path)
Definition Path.cs:881
static ? string GetDirectoryName(string? path)
Definition Path.cs:121
virtual void Dispose(bool disposing)
virtual void Close()
virtual void Write(char value)
virtual void Flush()
virtual void WriteLine()
static DecoderFallback ReplacementFallback
static EncoderFallback ReplacementFallback
virtual object Clone()
Definition Encoding.cs:706