The 'Debugger' class (1) already exists (in the corlib assembly), and
[mono.git] / mcs / class / System / System.Diagnostics / TextWriterTraceListener.cs
1 //
2 // System.Diagnostics.TextWriterTraceListener.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.IO;
15 using System.Diagnostics;
16
17 namespace System.Diagnostics {
18
19         /// <summary>
20         /// Directs tracing or debugging output to a <see cref="System.IO.TextWriter">
21         /// TextWriter</see> or to a <see cref="System.IO.Stream">Stream</see>,
22         /// such as <see cref="System.Console.Out">Console.Out</see> or
23         /// <see cref="System.IO.FileStream">FileStream</see>.
24         /// </summary>
25         public class TextWriterTraceListener : TraceListener {
26
27                 private TextWriter writer;
28
29                 /// <summary>
30                 /// Initializes a new instance of the <see cref="TextWriterTraceListener">
31                 /// TextWriterTraceListener</see> class with 
32                 /// <see cref="System.IO.TextWriter">TextWriter</see> 
33                 /// as the output recipient.
34                 /// </summary>
35                 public TextWriterTraceListener () : base ("TextWriter")
36                 {
37                 }
38
39                 /// <summary>
40                 /// Initializes a new instance of the <see cref="TextWriterTraceListener">
41                 /// TextWriterTraceListener</see> class, using the stream as the output
42                 /// recipient of the debugging and tracing output.
43                 /// </summary>
44                 /// <param name="stream">
45                 /// A <see cref="System.IO.Stream">Stream</see> that represents the stream 
46                 /// the <see cref="TextWriterTraceListener">TextWriterTraceListener</see> 
47                 /// writes to.
48                 /// </param>
49                 /// <exception cref="System.ArgumentNullException">
50                 /// The stream is a null reference.
51                 /// </exception>
52                 public TextWriterTraceListener (Stream stream)
53                         : this (stream, "")
54                 {
55                 }
56
57                 /// <summary>
58                 /// Initializes a new instance of the <see cref="TextWriterTraceListener">
59                 /// TextWriterTraceListener</see> class, using the file as the recipient
60                 /// of the debugging and tracing output.
61                 /// </summary>
62                 /// <param name="fileName">
63                 /// The name of the file the <see cref="TextWriterTraceListener">
64                 /// TextWriterTraceListener</see> writes to.
65                 /// </param>
66                 /// <exception cref="System.ArgumentNullException">
67                 /// The fileName is null.
68                 /// </exception>
69                 public TextWriterTraceListener (string fileName)
70                         : this (fileName, "")
71                 {
72                 }
73
74                 /// <summary>
75                 /// Initializes a new instance of the <see cref="TextWriterTraceListener">
76                 /// TextWriterTraceListener</see> class using the specified writer as the
77                 /// recipient of the tracing or debugging output.
78                 /// </summary>
79                 /// <param name="writer">
80                 /// A <see cref="System.IO.TextWriter">TextWriter</see> that receives 
81                 /// output from the 
82                 /// <see cref="TextWriterTraceListener">TextWriterTraceListener</see>.
83                 /// </param>
84                 /// <exception cref="System.ArgumentNullException">
85                 /// The writer is a null reference
86                 /// </exception>
87                 public TextWriterTraceListener (TextWriter writer)
88                         : this (writer, "")
89                 {
90                 }
91
92                 /// <summary>
93                 /// Initializes a new instance of the <see cref="TextWriterTraceListener">
94                 /// TextWriterTraceListener</see> class with the specified name, using the
95                 /// stream as the recipient of the tracing or debugging output.
96                 /// </summary>
97                 /// <param name="stream">
98                 /// A <see cref="System.IO.Stream">Stream</see> that represents the stream 
99                 /// the <see cref="TextWriterTraceListener">TextWriterTraceListener</see>
100                 /// writes to.
101                 /// </param>
102                 /// <param name="name">
103                 /// The name of the new instance
104                 /// </param>
105                 /// <exception cref="System.ArgumentNullException">
106                 /// The stream is a null reference
107                 /// </exception>
108                 public TextWriterTraceListener (Stream stream, string name) 
109                         : base (name != null ? name : "")
110                 {
111                         if (stream == null) 
112                                 throw new ArgumentNullException ("stream");
113                         writer = new StreamWriter (stream);
114                 }
115
116                 /// <summary>
117                 /// Initializes a new instance of the <see cref="TextWriterTraceListener">
118                 /// TextWriterTraceListener</see> class with the specified name, using the
119                 /// file as the recipient of the tracing or debugging output.
120                 /// </summary>
121                 /// <param name="fileName">
122                 /// The name of the file the <see cref="TextWriterTraceListener">
123                 /// TextWriterTraceListener</see> writes to.
124                 /// </param>
125                 /// <param name="name">
126                 /// The name of the new instance
127                 /// </param>
128                 /// <exception cref="System.ArgumentNullException">
129                 /// The file is a null reference.
130                 /// </exception>
131                 public TextWriterTraceListener (string fileName, string name) 
132                         : base (name != null ? name : "")
133                 {
134                         if (fileName == null)
135                                 throw new ArgumentNullException ("fileName");
136                         writer = new StreamWriter (File.OpenWrite (fileName));
137                 }
138
139                 /// <summary>
140                 /// Initializes a new instance of the <see cref="TextWriterTraceListener">
141                 /// TextWriterTraceListener</see> class with the specified name, using
142                 /// the specified writer as the recipient of the tracing or 
143                 /// debugging output.
144                 /// </summary>
145                 /// <param name="writer">
146                 /// A <see cref="System.IO.TextWriter">TextWriter</see> that receives 
147                 /// the output from the 
148                 /// <see cref="TextWriterTraceListener">TextWriterTraceListener</see>.
149                 /// </param>
150                 /// <param name="name">
151                 /// The name of the new instance.
152                 /// </param>
153                 /// <exception cref="System.ArgumentNullException">
154                 /// The writer is a null reference.
155                 /// </exception>
156                 public TextWriterTraceListener (TextWriter writer, string name) 
157                         : base (name != null ? name : "")
158                 {
159                         if (writer == null)
160                                 throw new ArgumentNullException ("writer");
161                         this.writer = writer;
162                 }
163
164                 /// <summary>
165                 /// Gets or sets the writer that receives the debugging or tracing output.
166                 /// </summary>
167                 /// <value>
168                 /// A <see cref="System.IO.TextWriter">TextWriter</see> that represents 
169                 /// the writer that receives the tracing or debugging output.
170                 /// </value>
171                 public TextWriter Writer {
172                         get {return writer;}
173                         set {writer = value;}
174                 }
175
176                 /// <summary>
177                 /// Closes the <see cref="System.IO.Writer">Writer</see> so that it no 
178                 /// longer receives tracing or debugging output.
179                 /// </summary>
180                 public override void Close ()
181                 {
182                         if (writer != null) {
183                                 writer.Flush ();
184                                 writer.Close ();
185                                 writer = null;
186                         }
187                 }
188
189                 protected override void Dispose (bool disposing)
190                 {
191                         if (disposing)
192                                 Close ();
193                 }
194
195                 /// <summary>
196                 /// Flushes the output buffer for the 
197                 /// <see cref="System.IO.Writer">Writer</see>.
198                 /// </summary>
199                 public override void Flush ()
200                 {
201                         writer.Flush ();
202                 }
203
204                 /// <summary>
205                 /// Writes a message to this instance's 
206                 /// <see cref="System.IO.Writer">Writer</see>.
207                 /// </summary>
208                 /// <param name="message">
209                 /// A message to write.
210                 /// </param>
211                 public override void Write (string message)
212                 {
213                         if (NeedIndent)
214                                 WriteIndent ();
215                         writer.Write (message);
216                 }
217
218                 /// <summary>
219                 /// Writes a message to this instance's 
220                 /// <see cref="System.IO.Writer">Writer</see>
221                 /// followed by a line terminator.
222                 /// </summary>
223                 /// <param name="message">
224                 /// A message to write.
225                 /// </param>
226                 public override void WriteLine (string message)
227                 {
228                         if (NeedIndent)
229                                 WriteIndent ();
230                         writer.WriteLine (message);
231                         NeedIndent = true;
232                 }
233         }
234 }
235