New test.
[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 implementation 
8 // can be found at: /mcs/docs/apidocs/xml/en/System.Diagnostics
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.Diagnostics;
36
37 namespace System.Diagnostics {
38
39         public abstract class TraceListener : MarshalByRefObject, IDisposable {
40
41                 [ThreadStatic]
42                 private int indentLevel = 0;
43
44                 [ThreadStatic]
45                 private int indentSize = 4;
46
47                 private string name = null;
48                 private bool needIndent = true;
49
50                 protected TraceListener () : this ("")
51                 {
52                 }
53
54                 protected TraceListener (string name)
55                 {
56                         Name = name;
57                 }
58
59                 public int IndentLevel {
60                         get {return indentLevel;}
61                         set {indentLevel = value;}
62                 }
63
64                 public int IndentSize {
65                         get {return indentSize;}
66                         set {indentSize = value;}
67                 }
68
69                 public virtual string Name {
70                         get {return name;}
71                         set {name = value;}
72                 }
73
74                 protected bool NeedIndent {
75                         get {return needIndent;}
76                         set {needIndent = value;}
77                 }
78
79                 public virtual void Close ()
80                 {
81                         Dispose ();
82                 }
83
84                 public void Dispose ()
85                 {
86                         Dispose (true);
87                         GC.SuppressFinalize (this);
88                 }
89
90                 protected virtual void Dispose (bool disposing)
91                 {
92                 }
93
94                 public virtual void Fail (string message)
95                 {
96                         Fail (message, "");
97                 }
98
99                 public virtual void Fail (string message, string detailMessage)
100                 {
101                         WriteLine ("---- DEBUG ASSERTION FAILED ----");
102                         WriteLine ("---- Assert Short Message ----");
103                         WriteLine (message);
104                         WriteLine ("---- Assert Long Message ----");
105                         WriteLine (detailMessage);
106                         WriteLine ("");
107                 }
108
109                 public virtual void Flush ()
110                 {
111                 }
112
113                 public virtual void Write (object o)
114                 {
115                         Write (o.ToString());
116                 }
117
118                 public abstract void Write (string message);
119
120                 public virtual void Write (object o, string category)
121                 {
122                         Write (o.ToString(), category);
123                 }
124
125                 public virtual void Write (string message, string category)
126                 {
127                         Write (category + ": " + message);
128                 }
129
130                 protected virtual void WriteIndent ()
131                 {
132                         // Must set NeedIndent to false before Write; otherwise, we get endless
133                         // recursion with Write->WriteIndent->Write->WriteIndent...*boom*
134                         NeedIndent = false;
135                         String indent = new String (' ', IndentLevel*IndentSize);
136                         Write (indent);
137                 }
138
139                 public virtual void WriteLine (object o)
140                 {
141                         WriteLine (o.ToString());
142                 }
143
144                 public abstract void WriteLine (string message);
145
146                 public virtual void WriteLine (object o, string category)
147                 {
148                         WriteLine (o.ToString(), category);
149                 }
150
151                 public virtual void WriteLine (string message, string category)
152                 {
153                         WriteLine (category + ": " + message);
154                 }
155         }
156 }
157