Fixed stupid stack overflow bug. My bad.
[mono.git] / mcs / class / System / System.Diagnostics / TraceListener.cs
1 //
2 // System.Diagnostics.TraceListener.cs
3 //
4 // Authors:
5 //   Jonathan Pryor (jonpryor@vt.edu)
6 //
7 // Comments from John R. Hicks <angryjohn69@nc.rr.com> original
8 // implementation.
9 //
10 // (C) 2002 Jonathan Pryor
11 //
12
13 using System;
14 using System.Diagnostics;
15
16 namespace System.Diagnostics {
17
18         /// <summary>
19         /// Provides the abstract base class for the listeners who monitor
20         /// trace and debug output
21         /// </summary>
22         public abstract class TraceListener : MarshalByRefObject, IDisposable {
23
24                 private int indentLevel = 0;
25                 private int indentSize = 4;
26                 private string name = null;
27                 private bool needIndent = false;
28
29                 /// <summary>
30                 /// Initializes a new instance of the <see cref="TraceListener">
31                 /// TraceListener</see> class.
32                 /// </summary>
33                 protected TraceListener () : this ("")
34                 {
35                 }
36
37                 /// <summary>
38                 /// Initializes a new instance of the <see cref="TraceListener">
39                 /// TraceListener</see> class using the specified name as the listener.
40                 /// </summary>
41                 protected TraceListener (string name)
42                 {
43                         Name = name;
44                 }
45
46                 /// <summary>
47                 /// Gets or sets the indent level.
48                 /// </summary>
49                 /// <value>
50                 /// The indent level.  The default is zero.
51                 /// </value>
52                 public int IndentLevel {
53                         get {return indentLevel;}
54                         set {indentLevel = value;}
55                 }
56
57                 /// <summary>
58                 /// Gets or sets the number of spaces in an indent.
59                 /// </summary>
60                 /// <value>
61                 /// The number of spaces in an indent.  The default is four spaces.
62                 /// </value>
63                 public int IndentSize {
64                         get {return indentSize;}
65                         set {indentSize = value;}
66                 }
67
68                 /// <summary>
69                 /// Gets or sets a name for this 
70                 /// <see cref="TraceListener">TraceListener</see>.
71                 /// </summary>
72                 /// <value>
73                 /// A name for this <see cref="TraceListener">TraceListener</see>.
74                 /// The default is the empty string ("")
75                 /// </value>
76                 public virtual string Name {
77                         get {return name;}
78                         set {name = value;}
79                 }
80
81                 /// <summary>
82                 /// Gets or sets a value indicating whether to indent the output.
83                 /// </summary>
84                 /// <value>
85                 /// <b>true</b> if the output should be indented; otherwise <b>false</b>.
86                 /// </value>
87                 protected bool NeedIndent {
88                         get {return needIndent;}
89                         set {needIndent = value;}
90                 }
91
92                 /// <summary>
93                 /// When overridden in a derived class, closes the output stream so it 
94                 /// no longer receives tracing or debugging output.
95                 /// </summary>
96                 public virtual void Close ()
97                 {
98                         Dispose ();
99                 }
100
101                 /// <summary>
102                 /// Releases all resources used by the i
103                 /// <see cref="TraceListener">TraceListener</see>.
104                 /// </summary>
105                 public void Dispose ()
106                 {
107                         Dispose (true);
108                         GC.SuppressFinalize (this);
109                 }
110
111                 /// <summary>
112                 /// Releases the unmanaged resources used by the 
113                 /// <see cref="TraceListener">TraceListener</see> and optionally 
114                 /// releases the managed resources.
115                 /// </summary>
116                 /// <param name="disposing">
117                 /// <b>true</b> to release both managed and unmanaged resources;
118                 /// <b>false</b> to release only unmanaged resources.
119                 /// </param>
120                 protected virtual void Dispose (bool disposing)
121                 {
122                 }
123
124                 /// <summary>
125                 /// Emits an error message to the listener you create when you 
126                 /// implement the <see cref="TraceListener">TraceListener</see> class.
127                 /// </summary>
128                 /// <param name="message">
129                 /// A message to emit.
130                 /// </param>
131                 public virtual void Fail (string message)
132                 {
133                         Fail (message, "");
134                 }
135
136                 /// <summary>
137                 /// Emits an error message, and a detailed error message to the listener
138                 /// you create when you implement the 
139                 /// <see cref="TraceListener">TraceListener</see> class.
140                 /// </summary>
141                 /// <param name="message">
142                 /// A message to emit.
143                 /// </param>
144                 /// <param name="detailMessage">
145                 /// A detailed message to emit.
146                 /// </param>
147                 public virtual void Fail (string message, string detailMessage)
148                 {
149                         WriteLine ("---- DEBUG ASSERTION FAILED ----");
150                         WriteLine ("---- Assert Short Message ----");
151                         WriteLine (message);
152                         WriteLine ("---- Assert Long Message ----");
153                         WriteLine (detailMessage);
154                         WriteLine ("");
155                 }
156
157                 /// <summary>
158                 /// When overridden in a derived class, flushes the output buffer.
159                 /// </summary>
160                 public virtual void Flush ()
161                 {
162                 }
163
164                 /// <summary>
165                 /// Writes the value of the object's 
166                 /// <see cref="System.Object.ToString">ToString</see>
167                 /// method to the listener you create when you implement the
168                 /// <see cref="TraceListener">TraceListener</see> class.
169                 /// </summary>
170                 /// <param name="o">
171                 /// An <see cref="System.Object">Object</see> whose fully qualified
172                 /// class name you want to write.
173                 /// </param>
174                 public virtual void Write (object o)
175                 {
176                         Write (o.ToString());
177                 }
178
179                 /// <summary>
180                 /// When overridden in a derived class, writes the specified message to 
181                 /// the listener you create in the derived class.
182                 /// </summary>
183                 /// <param name="message">
184                 /// A message to write.
185                 /// </param>
186                 public abstract void Write (string message);
187
188                 /// <summary>
189                 /// Writes a category name and the value of the object's 
190                 /// <see cref="System.Object.ToString">ToString</see>
191                 /// method to the listener you create when you implement the
192                 /// <see cref="TraceListener">TraceListener</see> class.
193                 /// </summary>
194                 /// <param name="o">
195                 /// An <see cref="System.Object">Object</see> whose fully qualified
196                 /// class name you wish to write.
197                 /// </param>
198                 /// <param name="category">
199                 /// A category name used to organize the output.
200                 /// </param>
201                 public virtual void Write (object o, string category)
202                 {
203                         Write (o.ToString(), category);
204                 }
205
206                 /// <summary>
207                 /// Writes a category name and a message to the listener you create when 
208                 /// you implement the <see cref="TraceListener">TraceListener</see> class.
209                 /// </summary>
210                 /// <param name="message">
211                 /// A message to write.
212                 /// </param>
213                 /// <param name="category">
214                 /// A category name used to organize the output.
215                 /// </param>
216                 public virtual void Write (string message, string category)
217                 {
218                         Write (category + ": " + message);
219                 }
220
221                 /// <summary>
222                 /// Writes the indent to the listener you create when you implement 
223                 /// this class, and resets the <see cref="NeedIndent">NeedIndent</see> 
224                 /// Property to <b>false</b>.
225                 /// </summary>
226                 protected virtual void WriteIndent ()
227                 {
228                         // Must set NeedIndent to false before Write; otherwise, we get endless
229                         // recursion with Write->WriteIndent->Write->WriteIndent...*boom*
230                         NeedIndent = false;
231                         String indent = new String (' ', IndentLevel*IndentSize);
232                         Write (indent);
233                 }
234
235                 /// <summary>
236                 /// Writes the value of the object's 
237                 /// <see cref="System.Object.ToString">ToString</see>
238                 /// method to the listener you create when you implement the
239                 /// <see cref="TraceListener">TraceListener</see> class, followed 
240                 /// by a line terminator.
241                 /// </summary>
242                 /// <param name="o">
243                 /// An <see cref="System.Object">Object</see> whose fully qualified
244                 /// class name you want to write.
245                 /// </param>
246                 public virtual void WriteLine (object o)
247                 {
248                         WriteLine (o.ToString());
249                 }
250
251                 /// <summary>
252                 /// When overridden in a derived class, writes the specified message to 
253                 /// the listener you create in the derived class, followed by a 
254                 /// line terminator.
255                 /// </summary>
256                 /// <param name="message">
257                 /// A message to write.
258                 /// </param>
259                 public abstract void WriteLine (string message);
260
261                 /// <summary>
262                 /// Writes a category name and the value of the object's 
263                 /// <see cref="System.Object.ToString">ToString</see>
264                 /// method to the listener you create when you implement the
265                 /// <see cref="TraceListener">TraceListener</see> class, followed by a
266                 /// line terminator.
267                 /// </summary>
268                 /// <param name="o">
269                 /// An <see cref="System.Object">Object</see> whose fully qualified
270                 /// class name you wish to write.
271                 /// </param>
272                 /// <param name="category">
273                 /// A category name used to organize the output.
274                 /// </param>
275                 public virtual void WriteLine (object o, string category)
276                 {
277                         WriteLine (o.ToString(), category);
278                 }
279
280                 /// <summary>
281                 /// Writes a category name and a message to the listener you create when 
282                 /// you implement the <see cref="TraceListener">TraceListener</see> class,
283                 /// followed by a line terminator.
284                 /// </summary>
285                 /// <param name="message">
286                 /// A message to write.
287                 /// </param>
288                 /// <param name="category">
289                 /// A category name used to organize the output.
290                 /// </param>
291                 public virtual void WriteLine (string message, string category)
292                 {
293                         WriteLine (category + ": " + message);
294                 }
295         }
296 }
297