New test.
[mono.git] / mcs / class / System / System.Diagnostics / TextWriterTraceListener.cs
1 //
2 // System.Diagnostics.TextWriterTraceListener.cs
3 //
4 // Comments from John R. Hicks <angryjohn69@nc.rr.com> original implementation 
5 // can be found at: /mcs/docs/apidocs/xml/en/System.Diagnostics
6 //
7 // Authors:
8 //   Jonathan Pryor (jonpryor@vt.edu)
9 //
10 // (C) 2002 Jonathan Pryor
11 //
12
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System;
35 using System.IO;
36 using System.Diagnostics;
37
38 namespace System.Diagnostics {
39
40         public class TextWriterTraceListener : TraceListener {
41
42                 private TextWriter writer;
43
44                 public TextWriterTraceListener () : base ("TextWriter")
45                 {
46                 }
47
48                 public TextWriterTraceListener (Stream stream)
49                         : this (stream, "")
50                 {
51                 }
52
53                 public TextWriterTraceListener (string fileName)
54                         : this (fileName, "")
55                 {
56                 }
57
58                 public TextWriterTraceListener (TextWriter writer)
59                         : this (writer, "")
60                 {
61                 }
62
63                 public TextWriterTraceListener (Stream stream, string name) 
64                         : base (name != null ? name : "")
65                 {
66                         if (stream == null) 
67                                 throw new ArgumentNullException ("stream");
68                         writer = new StreamWriter (stream);
69                 }
70
71                 public TextWriterTraceListener (string fileName, string name) 
72                         : base (name != null ? name : "")
73                 {
74                         if (fileName == null)
75                                 throw new ArgumentNullException ("fileName");
76                         writer = File.AppendText (fileName);
77                 }
78
79                 public TextWriterTraceListener (TextWriter writer, string name) 
80                         : base (name != null ? name : "")
81                 {
82                         if (writer == null)
83                                 throw new ArgumentNullException ("writer");
84                         this.writer = writer;
85                 }
86
87                 public TextWriter Writer {
88                         get {return writer;}
89                         set {writer = value;}
90                 }
91
92                 public override void Close ()
93                 {
94                         if (writer != null) {
95                                 writer.Flush ();
96                                 writer.Close ();
97                                 writer = null;
98                         }
99                 }
100
101                 protected override void Dispose (bool disposing)
102                 {
103                         if (disposing)
104                                 Close ();
105
106                         base.Dispose (disposing);
107                 }
108
109                 public override void Flush ()
110                 {
111                         if (writer != null)
112                                 writer.Flush ();
113                 }
114
115                 public override void Write (string message)
116                 {
117                         if (writer != null) {
118                                 if (NeedIndent)
119                                         WriteIndent ();
120                                 writer.Write (message);
121                         }
122                 }
123
124                 public override void WriteLine (string message)
125                 {
126                         if (writer != null) {
127                                 if (NeedIndent)
128                                         WriteIndent ();
129                                 writer.WriteLine (message);
130                                 NeedIndent = true;
131                         }
132                 }
133         }
134 }
135