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