Merge pull request #2530 from lambdageek/monoerror-mono_string_new
[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-2009 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         sealed class TraceManager
39         {
40                 bool enabled = false;
41                 bool local_only = true;
42                 bool page_output = false;
43                 TraceMode mode;
44                 int request_limit = 10;
45
46                 int cur_item;
47                 TraceData[] data;
48
49                 Exception initialException;
50                 
51                 public TraceManager ()
52                 {
53                         try {
54                                 mode = TraceMode.SortByTime;
55                                 TraceSection config = WebConfigurationManager.GetWebApplicationSection ("system.web/trace") as TraceSection;
56                                 if (config == null)
57                                         config = new TraceSection ();
58
59                                 if (config == null)
60                                         return;
61                         
62                                 enabled = config.Enabled;
63                                 local_only = config.LocalOnly;
64                                 page_output = config.PageOutput;
65                                 if (config.TraceMode == TraceDisplayMode.SortByTime)
66                                         mode = TraceMode.SortByTime;
67                                 else
68                                         mode = TraceMode.SortByCategory;
69                                 request_limit = config.RequestLimit;
70                         } catch (Exception ex) {
71                                 initialException = ex;
72                         }
73                 }
74
75                 public bool HasException {
76                         get { return initialException != null; }
77                 }
78
79                 public Exception InitialException {
80                         get { return initialException; }
81                 }
82                 
83                 public bool Enabled {
84                         get { return enabled; }
85                         set { enabled = value; }
86                 }
87
88                 public bool LocalOnly {
89                         get { return local_only; }
90                         set { local_only = value; }
91                 }
92
93                 public bool PageOutput {
94                         get { return page_output; }
95                         set { page_output = value; }
96                 }
97
98                 public int RequestLimit {
99                         get { return request_limit; }
100                         set {
101                                 if (request_limit == value)
102                                         return;
103                                 TraceData[] swap = new TraceData [value];
104                                 Array.Copy (data, swap, (cur_item > value ? value : cur_item));
105                                 if (cur_item > value)
106                                         cur_item = value;
107                                 request_limit = value;
108                         }
109                 }
110
111                 public TraceMode TraceMode {
112                         get { return mode; }
113                         set { mode = value; }
114                 }
115
116                 public TraceData[] TraceData {
117                         get { return data; }
118                 }
119                 
120                 public void AddTraceData (TraceData item)
121                 {
122                         if (data == null)
123                                 data = new TraceData [request_limit];
124                         if (cur_item == request_limit)
125                                 return;
126                         data [cur_item++] = item;
127                 }
128
129                 public void Clear ()
130                 {
131                         if (data == null)
132                                 return;
133                         
134                         Array.Clear (data, 0, data.Length);
135                         cur_item = 0;
136                 }
137                 
138                 public int ItemCount {
139                         get { return cur_item; }
140                 }
141         }
142 }
143