2009-02-28 Gonzalo Paniagua Javier <gonzalo@novell.com>
[mono.git] / mcs / class / System.Web / System.Web / TraceManager.cs
1 //
2 // System.Web.TraceManager
3 //
4 // Author(s):
5 //  Jackson Harper (jackson@ximian.com)
6 //
7 // (C) 2004 Novell, Inc (http://www.novell.com)
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
32 using System;
33 using System.Collections;
34 using System.Web.Configuration;
35
36 namespace System.Web {
37
38         internal class TraceManager {
39 #if !NET_2_0
40                 static string traceConfigPath = "system.web/trace";
41 #endif
42                 bool enabled = false;
43                 bool local_only = true;
44                 bool page_output = false;
45                 TraceMode mode;
46                 int request_limit = 10;
47
48                 int cur_item;
49                 TraceData[] data;
50
51                 Exception initialException;
52                 
53                 public TraceManager ()
54                 {
55                         try {
56 #if NET_2_0
57                                 mode = TraceMode.SortByTime;
58                                 TraceSection config = WebConfigurationManager.GetWebApplicationSection ("system.web/trace") as TraceSection;
59                                 if (config == null)
60                                         config = new TraceSection ();
61 #else
62                                 TraceConfig config = (TraceConfig) HttpContext.GetAppConfig (traceConfigPath);
63 #endif
64
65                                 if (config == null)
66                                         return;
67                         
68                                 enabled = config.Enabled;
69                                 local_only = config.LocalOnly;
70                                 page_output = config.PageOutput;
71 #if NET_2_0
72                                 if (config.TraceMode == TraceDisplayMode.SortByTime)
73                                         mode = TraceMode.SortByTime;
74                                 else
75                                         mode = TraceMode.SortByCategory;
76 #else
77                                 mode = config.TraceMode;
78 #endif
79                                 request_limit = config.RequestLimit;
80                         } catch (Exception ex) {
81                                 initialException = ex;
82                         }
83                 }
84
85                 public bool HasException {
86                         get { return initialException != null; }
87                 }
88
89                 public Exception InitialException {
90                         get { return initialException; }
91                 }
92                 
93                 public bool Enabled {
94                         get { return enabled; }
95                         set { enabled = value; }
96                 }
97
98                 public bool LocalOnly {
99                         get { return local_only; }
100                         set { local_only = value; }
101                 }
102
103                 public bool PageOutput {
104                         get { return page_output; }
105                         set { page_output = value; }
106                 }
107
108                 public int RequestLimit {
109                         get { return request_limit; }
110                         set {
111                                 if (request_limit == value)
112                                         return;
113                                 TraceData[] swap = new TraceData [value];
114                                 Array.Copy (data, swap, (cur_item > value ? value : cur_item));
115                                 if (cur_item > value)
116                                         cur_item = value;
117                                 request_limit = value;
118                         }
119                 }
120
121                 public TraceMode TraceMode {
122                         get { return mode; }
123                         set { mode = value; }
124                 }
125
126                 public TraceData[] TraceData {
127                         get { return data; }
128                 }
129                 
130                 public void AddTraceData (TraceData item)
131                 {
132                         if (data == null)
133                                 data = new TraceData [request_limit];
134                         if (cur_item == request_limit)
135                                 return;
136                         data [cur_item++] = item;
137                 }
138
139                 public void Clear ()
140                 {
141                         if (data == null)
142                                 return;
143                         
144                         Array.Clear (data, 0, data.Length);
145                         cur_item = 0;
146                 }
147                 
148                 public int ItemCount {
149                         get { return cur_item; }
150                 }
151         }
152 }
153