[runtime] Switch getenv to use heap memory
[mono.git] / mcs / class / System.Web / System.Web / TraceContext.cs
1 // 
2 // System.Web.TraceContext
3 //
4 // Author:
5 //   Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 //   Jackson Harper (jackson@ximian.com)
7 //
8 // (C) 2002 2003, Patrik Torstensson
9 // Copyright (C) 2003-2009 Novell, Inc (http://www.novell.com)
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.ComponentModel;
32 using System.Collections;
33 using System.Security.Permissions;
34 using System.Web.UI;
35
36 namespace System.Web
37 {
38         // CAS - no InheritanceDemand here as the class is sealed
39         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         public sealed class TraceContext
41         {
42                 static readonly object traceFinishedEvent = new object ();
43                 
44                 HttpContext _Context;
45                 TraceManager _traceManager;
46                 bool _Enabled;
47                 TraceMode _Mode = TraceMode.Default;
48                 TraceData data;
49                 bool data_saved;
50                 bool _haveTrace;
51                 Hashtable view_states;
52                 Hashtable control_states;
53                 Hashtable sizes;                
54                 EventHandlerList events = new EventHandlerList ();
55                 
56                 public event TraceContextEventHandler TraceFinished {
57                         add { events.AddHandler (traceFinishedEvent, value); }
58                         remove { events.AddHandler (traceFinishedEvent, value); }
59                 }
60
61                 public TraceContext (HttpContext Context)
62                 {
63                         _Context = Context;
64                 }
65
66                 internal bool HaveTrace {
67                         get {
68                                 return _haveTrace;
69                         }
70                 }
71
72                 public bool IsEnabled {
73                         get {
74                                 if (!_haveTrace)
75                                         return TraceManager.Enabled;
76                                 return _Enabled;
77                         }
78
79                         set {
80                                 if (value && data == null)
81                                         data = new TraceData ();
82                                 _haveTrace = true;
83                                 _Enabled = value;
84                         }
85                 }
86
87                 TraceManager TraceManager
88                 {
89                         get
90                         {
91                                 if (_traceManager == null)
92                                         _traceManager = HttpRuntime.TraceManager;
93
94                                 return _traceManager;
95                         }
96                 }
97
98                 public TraceMode TraceMode {
99                         get {
100                                 return (_Mode == TraceMode.Default) ? TraceManager.TraceMode : _Mode;
101                         }
102                         set {
103                                 _Mode = value;
104                         }
105                 }
106
107                 public void Warn(string msg)
108                 {
109                         Write (String.Empty, msg, null, true);
110                 }
111
112                 public void Warn(string category, string msg)
113                 {
114                         Write (category, msg, null, true);
115                 }
116
117                 public void Warn (string category, string msg, Exception error)
118                 {
119                         Write (category, msg, error, true);
120                 }
121
122                 public void Write (string msg)
123                 {
124                         Write (String.Empty, msg, null, false);
125                 }
126
127                 public void Write (string category, string msg)
128                 {
129                         Write (category, msg, null, false);
130                 }
131
132                 public void Write (string category, string msg, Exception error)
133                 {
134                         Write (category, msg, error, false);
135                 }
136
137                 void Write (string category, string msg, Exception error, bool Warning)
138                 {
139                         if (!IsEnabled)
140                                 return;
141                         if (data == null)
142                                 data = new TraceData ();
143                         data.Write (category, msg, error, Warning);
144                 }
145
146                 internal void SaveData ()
147                 {
148                         if (data == null)
149                                 data = new TraceData ();
150
151                         data.TraceMode = _Context.Trace.TraceMode;
152
153                         SetRequestDetails ();
154                         if (_Context.Handler is Page)
155                                 data.AddControlTree ((Page) _Context.Handler, view_states, control_states, sizes);
156
157                         AddCookies ();
158                         AddHeaders ();
159                         AddServerVars ();
160                         TraceManager.AddTraceData (data);
161                         data_saved = true;
162                 }
163
164                 internal void SaveViewState (Control ctrl, object vs)
165                 {
166                         if (view_states == null)
167                                 view_states = new Hashtable ();
168
169                         view_states [ctrl] = vs;
170                 }
171
172                 internal void SaveControlState (Control ctrl, object vs) {
173                         if (control_states == null)
174                                 control_states = new Hashtable ();
175
176                         control_states [ctrl] = vs;
177                 }
178
179                 internal void SaveSize (Control ctrl, int size)
180                 {
181                         if (sizes == null)
182                                 sizes = new Hashtable ();
183
184                         sizes [ctrl] = size;
185                 }
186
187                 internal void Render (HtmlTextWriter output)
188                 {
189                         if (!data_saved)
190                                 SaveData ();
191                         data.Render (output);
192                 }
193
194                 void SetRequestDetails ()
195                 {
196                         data.RequestPath = _Context.Request.FilePath;
197                         data.SessionID = (_Context.Session != null ? _Context.Session.SessionID : String.Empty);
198                         data.RequestType = _Context.Request.RequestType;
199                         data.RequestTime = _Context.Timestamp;
200                         data.StatusCode = _Context.Response.StatusCode;
201                         data.RequestEncoding = _Context.Request.ContentEncoding;
202                         data.ResponseEncoding = _Context.Response.ContentEncoding;
203                 }
204
205                 void AddCookies ()
206                 {
207                         foreach (string key in _Context.Request.Cookies.Keys)
208                                 data.AddCookie (key, _Context.Request.Cookies [key].Value);
209                 }
210
211                 void AddHeaders ()
212                 {
213                         foreach (string key in _Context.Request.Headers.Keys)
214                                 data.AddHeader (key, _Context.Request.Headers [key]);
215                 }
216
217                 void AddServerVars ()
218                 {
219                         foreach (string key in _Context.Request.ServerVariables)
220                                 data.AddServerVar (key, _Context.Request.ServerVariables [key]);
221                 }
222         }
223 }
224