System.Drawing: added email to icon and test file headers
[mono.git] / mcs / class / System / System.Diagnostics / DelimitedListTraceListener.cs
1 //
2 // DelimitedListTraceFilter.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // (C) 2007 Novell, Inc.
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 #if NET_2_0
31
32 using System;
33 using System.IO;
34 using System.Collections;
35 using System.Diagnostics;
36 using System.Runtime.CompilerServices;
37 using System.Runtime.InteropServices;
38 using System.Text;
39
40 namespace System.Diagnostics
41 {
42         public class DelimitedListTraceListener : TextWriterTraceListener
43         {
44                 public DelimitedListTraceListener (string fileName)
45                         : base (fileName)
46                 {
47                 }
48
49                 public DelimitedListTraceListener (string fileName, string name)
50                         : base (fileName, name)
51                 {
52                 }
53
54                 public DelimitedListTraceListener (Stream stream)
55                         : base (stream)
56                 {
57                 }
58
59                 public DelimitedListTraceListener (Stream stream, string name)
60                         : base (stream, name)
61                 {
62                 }
63
64                 public DelimitedListTraceListener (TextWriter writer)
65                         : base (writer)
66                 {
67                 }
68
69                 public DelimitedListTraceListener (TextWriter writer, string name)
70                         : base (writer, name)
71                 {
72                 }
73
74                 static readonly string [] attributes = new string [] {"delimiter"};
75                 string delimiter = ";";
76
77                 public string Delimiter {
78                         get { return delimiter; }
79                         set {
80                                 if (value == null)
81                                         throw new ArgumentNullException ("value");
82                                 delimiter = value;
83                         }
84                 }
85
86                 protected internal override string [] GetSupportedAttributes ()
87                 {
88                         return attributes;
89                 }
90
91                 public override void TraceData (TraceEventCache eventCache,
92                                                 string source, TraceEventType eventType,
93                                                 int id, object data)
94                 {
95                         TraceCore (eventCache, source, eventType, id, null, data);
96                 }
97
98                 public override void TraceData (TraceEventCache eventCache,
99                                                 string source, TraceEventType eventType,
100                                                 int id, params object [] data)
101                 {
102                         TraceCore (eventCache, source, eventType, id, null, data);
103                 }
104
105                 public override void TraceEvent (TraceEventCache eventCache,
106                                                  string source, TraceEventType eventType,
107                                                  int id, string message)
108                 {
109                         TraceCore (eventCache, source, eventType, id, message);
110                 }
111
112                 public override void TraceEvent (TraceEventCache eventCache,
113                                                  string source, TraceEventType eventType,
114                                                  int id, string format, params object [] args)
115                 {
116                         TraceCore (eventCache, source, eventType, id, String.Format (format, args));
117                 }
118
119                 void TraceCore (TraceEventCache c, string source, TraceEventType eventType, int id, string message, params object [] data)
120                 {
121                         // source, eventType, id, message?, data-comma-separated
122                         Write (String.Format ("{1}{0}{2}{0}{3}{0}{4}{0}{5}{0}{6}{0}{7}{0}{8}{0}{9}{0}{10}{0}{11}{12}",
123                                delimiter,
124                                source != null ? "\"" + source.Replace ("\"", "\"\"") + "\"": null,
125                                eventType,
126                                id,
127                                message != null ? "\"" + message.Replace ("\"", "\"\"") + "\"" : null,
128                                FormatData (data),
129                                IsTarget (c, TraceOptions.ProcessId) ? c.ProcessId.ToString () : null,
130                                IsTarget (c, TraceOptions.LogicalOperationStack) ? FormatArray (c.LogicalOperationStack, ", ") : null,
131                                IsTarget (c, TraceOptions.ThreadId) ? c.ThreadId : null,
132                                IsTarget (c, TraceOptions.DateTime) ? c.DateTime.ToString ("o") : null,
133                                IsTarget (c, TraceOptions.Timestamp) ? c.Timestamp.ToString () : null,
134                                IsTarget (c, TraceOptions.Callstack) ? c.Callstack : null,
135                                Environment.NewLine));
136                 }
137
138                 bool IsTarget (TraceEventCache c, TraceOptions opt)
139                 {
140                         return c != null && (TraceOutputOptions & opt) != 0;
141                 }
142
143                 string FormatData (object [] data)
144                 {
145                         if (data == null || data.Length == 0)
146                                 return null;
147                         StringBuilder sb = new StringBuilder ();
148                         for (int i = 0; i < data.Length; i++) {
149                                 if (data [i] != null)
150                                         sb.Append ('"').Append (data [i].ToString ().Replace ("\"", "\"\"")).Append ('"');
151                                 if (i + 1 < data.Length)
152                                         sb.Append (',');
153                         }
154                         return sb.ToString ();
155                 }
156         }
157 }
158 #endif