Merge pull request #268 from pcc/menudeactivate
[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
31 using System;
32 using System.IO;
33 using System.Collections;
34 using System.Diagnostics;
35 using System.Runtime.CompilerServices;
36 using System.Runtime.InteropServices;
37 using System.Text;
38
39 namespace System.Diagnostics
40 {
41         public class DelimitedListTraceListener : TextWriterTraceListener
42         {
43                 public DelimitedListTraceListener (string fileName)
44                         : base (fileName)
45                 {
46                 }
47
48                 public DelimitedListTraceListener (string fileName, string name)
49                         : base (fileName, name)
50                 {
51                 }
52
53                 public DelimitedListTraceListener (Stream stream)
54                         : base (stream)
55                 {
56                 }
57
58                 public DelimitedListTraceListener (Stream stream, string name)
59                         : base (stream, name)
60                 {
61                 }
62
63                 public DelimitedListTraceListener (TextWriter writer)
64                         : base (writer)
65                 {
66                 }
67
68                 public DelimitedListTraceListener (TextWriter writer, string name)
69                         : base (writer, name)
70                 {
71                 }
72
73                 static readonly string [] attributes = new string [] {"delimiter"};
74                 string delimiter = ";";
75
76                 public string Delimiter {
77                         get { return delimiter; }
78                         set {
79                                 if (value == null)
80                                         throw new ArgumentNullException ("value");
81                                 delimiter = value;
82                         }
83                 }
84
85                 protected internal override string [] GetSupportedAttributes ()
86                 {
87                         return attributes;
88                 }
89
90                 public override void TraceData (TraceEventCache eventCache,
91                                                 string source, TraceEventType eventType,
92                                                 int id, object data)
93                 {
94                         TraceCore (eventCache, source, eventType, id, null, data);
95                 }
96
97                 public override void TraceData (TraceEventCache eventCache,
98                                                 string source, TraceEventType eventType,
99                                                 int id, params object [] data)
100                 {
101                         TraceCore (eventCache, source, eventType, id, null, data);
102                 }
103
104                 public override void TraceEvent (TraceEventCache eventCache,
105                                                  string source, TraceEventType eventType,
106                                                  int id, string message)
107                 {
108                         TraceCore (eventCache, source, eventType, id, message);
109                 }
110
111                 public override void TraceEvent (TraceEventCache eventCache,
112                                                  string source, TraceEventType eventType,
113                                                  int id, string format, params object [] args)
114                 {
115                         TraceCore (eventCache, source, eventType, id, String.Format (format, args));
116                 }
117
118                 void TraceCore (TraceEventCache c, string source, TraceEventType eventType, int id, string message, params object [] data)
119                 {
120                         // source, eventType, id, message?, data-comma-separated
121                         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}",
122                                delimiter,
123                                source != null ? "\"" + source.Replace ("\"", "\"\"") + "\"": null,
124                                eventType,
125                                id,
126                                message != null ? "\"" + message.Replace ("\"", "\"\"") + "\"" : null,
127                                FormatData (data),
128                                IsTarget (c, TraceOptions.ProcessId) ? c.ProcessId.ToString () : null,
129                                IsTarget (c, TraceOptions.LogicalOperationStack) ? FormatArray (c.LogicalOperationStack, ", ") : null,
130                                IsTarget (c, TraceOptions.ThreadId) ? c.ThreadId : null,
131                                IsTarget (c, TraceOptions.DateTime) ? c.DateTime.ToString ("o") : null,
132                                IsTarget (c, TraceOptions.Timestamp) ? c.Timestamp.ToString () : null,
133                                IsTarget (c, TraceOptions.Callstack) ? c.Callstack : null,
134                                Environment.NewLine));
135                 }
136
137                 bool IsTarget (TraceEventCache c, TraceOptions opt)
138                 {
139                         return c != null && (TraceOutputOptions & opt) != 0;
140                 }
141
142                 string FormatData (object [] data)
143                 {
144                         if (data == null || data.Length == 0)
145                                 return null;
146                         StringBuilder sb = new StringBuilder ();
147                         for (int i = 0; i < data.Length; i++) {
148                                 if (data [i] != null)
149                                         sb.Append ('"').Append (data [i].ToString ().Replace ("\"", "\"\"")).Append ('"');
150                                 if (i + 1 < data.Length)
151                                         sb.Append (',');
152                         }
153                         return sb.ToString ();
154                 }
155         }
156 }