Same thing as in HEAD.
[mono.git] / mcs / class / System.Web / System.Web / TraceData.cs
1 //
2 // System.Web.TraceData
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.Text;
34 using System.Data;
35 using System.Web.UI;
36 using System.Web.UI.WebControls;
37
38 namespace System.Web {
39
40         internal class TraceData {
41
42                 private bool is_first_time;
43                 private DateTime first_time;
44                 private double prev_time;
45                 
46                 private DataTable info;
47                 private DataTable control_data;
48                 private DataTable cookie_data;
49                 private DataTable header_data;
50                 private DataTable servervar_data;
51
52                 private string request_path;
53                 private string session_id;
54                 private DateTime request_time;
55                 private Encoding request_encoding;
56                 private Encoding response_encoding;
57                 private string request_type;
58                 private int status_code;
59
60                 public TraceData ()
61                 {
62                         info = new DataTable ();
63                         info.Columns.Add (new DataColumn ("Category", typeof (string)));
64                         info.Columns.Add (new DataColumn ("Message", typeof (string)));
65                         info.Columns.Add (new DataColumn ("Exception", typeof (string)));
66                         info.Columns.Add (new DataColumn ("TimeSinceFirst", typeof (double)));
67                         info.Columns.Add (new DataColumn ("IsWarning", typeof (bool)));
68
69                         control_data = new DataTable ();
70                         control_data.Columns.Add (new DataColumn ("ControlId", typeof (string)));
71                         control_data.Columns.Add (new DataColumn ("Type", typeof (System.Type)));
72                         control_data.Columns.Add (new DataColumn ("RenderSize", typeof (int)));
73                         control_data.Columns.Add (new DataColumn ("ViewstateSize", typeof (int)));
74                         control_data.Columns.Add (new DataColumn ("Depth", typeof (int)));
75
76                         cookie_data = new DataTable ();
77                         cookie_data.Columns.Add (new DataColumn ("Name", typeof (string)));
78                         cookie_data.Columns.Add (new DataColumn ("Value", typeof (string)));
79
80                         header_data = new DataTable ();
81                         header_data.Columns.Add (new DataColumn ("Name", typeof (string)));
82                         header_data.Columns.Add (new DataColumn ("Value", typeof (string)));
83
84                         servervar_data = new DataTable ();
85                         servervar_data.Columns.Add (new DataColumn ("Name", typeof (string)));
86                         servervar_data.Columns.Add (new DataColumn ("Value", typeof (string)));
87
88                         is_first_time = true;
89                 }
90
91                 public string RequestPath {
92                         get { return request_path; }
93                         set { request_path = value; }
94                 }
95                 
96                 public string SessionID {
97                         get { return session_id; }
98                         set { session_id = value; }
99                 }
100
101                 public DateTime RequestTime {
102                         get { return request_time; }
103                         set { request_time = value; }
104                 }
105
106                 public Encoding RequestEncoding {
107                         get { return request_encoding; }
108                         set { request_encoding = value; }
109                 }
110
111                 public Encoding ResponseEncoding {
112                         get { return response_encoding; }
113                         set { response_encoding = value; }
114                 }
115
116                 public string RequestType {
117                         get { return request_type; }
118                         set { request_type = value; }
119                 }
120
121                 public int StatusCode {
122                         get { return status_code; }
123                         set { status_code = value; }
124                 }
125
126                 public void Write (string category, string msg, Exception error, bool Warning)
127                 {
128                         double time;
129                         if (is_first_time) {
130                                 time = 0;
131                                 is_first_time = false;
132                                 first_time = DateTime.Now;
133                         } else
134                                 time = (DateTime.Now - first_time).TotalSeconds;
135
136                         DataRow r = info.NewRow ();
137                         r ["Category"] = category;
138                         r ["Message"] = HtmlEncode (msg);
139                         r ["Exception"] = (error != null ? error.ToString () : null);
140                         r ["TimeSinceFirst"] = time;
141                         r ["IsWarning"] = Warning;
142
143                         info.Rows.Add (r);
144                 }
145
146                 static string HtmlEncode (string s)
147                 {
148                         if (s == null)
149                                 return "";
150
151                         string res = HttpUtility.HtmlEncode (s);
152                         res = res.Replace ("\n", "<br>");
153                         return res.Replace (" ", "&nbsp;");
154                 }
155                 
156                 public void AddControlTree (Page page)
157                 {
158                         AddControl (page, 0);
159                 }
160
161                 private void AddControl (Control c, int control_pos)
162                 {
163                         DataRow r = control_data.NewRow ();
164                         r ["ControlId"] = c.UniqueID;
165                         r ["Type"] = c.GetType ();
166                         r ["Depth"] = control_pos;
167
168                         control_data.Rows.Add (r);
169                         
170                         foreach (Control child in c.Controls)
171                                 AddControl (child, control_pos + 1);
172                 }
173
174                 public void AddCookie (string name, string value)
175                 {
176                         DataRow r = cookie_data.NewRow ();
177
178                         r ["Name"] = name;
179                         r ["Value"] = value;
180
181                         cookie_data.Rows.Add (r);
182                 }
183
184                 public void AddHeader (string name, string value)
185                 {
186                         DataRow r = header_data.NewRow ();
187
188                         r ["Name"] = name;
189                         r ["Value"] = value;
190
191                         header_data.Rows.Add (r);
192                 }
193
194                 public void AddServerVar (string name, string value)
195                 {
196                         DataRow r = servervar_data.NewRow ();
197
198                         r ["Name"] = name;
199                         r ["Value"] = value;
200
201                         servervar_data.Rows.Add (r);
202                 }
203                 
204                 public void Render (HtmlTextWriter output)
205                 {
206                         output.AddAttribute ("id", "__asptrace");
207                         output.RenderBeginTag (HtmlTextWriterTag.Div);
208                         
209                         RenderStyleSheet (output);
210                         
211                         output.AddAttribute ("class", "tracecontent");
212                         output.RenderBeginTag (HtmlTextWriterTag.Span);
213                         
214                         RenderRequestDetails (output);
215                         RenderTraceInfo (output);
216                         RenderControlTree (output);
217                         RenderCookies (output);
218                         RenderHeaders (output);
219                         RenderServerVars (output);
220                         
221                         output.RenderEndTag ();
222                         output.RenderEndTag ();
223                 }
224                 
225                 private void RenderRequestDetails (HtmlTextWriter output)
226                 {
227                         Table table = CreateTable ();
228                         
229                         table.Rows.Add (AltRow ("Request Details:"));
230                         table.Rows.Add (InfoRow2 ("Session Id:", session_id,
231                                                         "Request Type", request_type));
232                         table.Rows.Add (InfoRow2 ("Time of Request:", request_time.ToString (),
233                                                         "State Code:", status_code.ToString ()));
234                         table.Rows.Add (InfoRow2 ("Request Encoding:", request_encoding.EncodingName,
235                                                    "Response Encoding:", response_encoding.EncodingName));           
236                         table.RenderControl (output);
237                 }
238                 
239                 private void RenderTraceInfo (HtmlTextWriter output)
240                 {
241                         Table table = CreateTable ();
242                         
243                         table.Rows.Add (AltRow ("Trace Information"));
244                         table.Rows.Add (SubHeadRow ("Category", "Message", "From First(s)", "From Lasts(s)"));
245                         
246                         int pos = 0;
247                         foreach (DataRow r in info.Rows)
248                                 RenderTraceInfoRow (table, r, pos++);
249                         
250                         table.RenderControl (output);
251                 }
252                 
253                 private void RenderControlTree (HtmlTextWriter output)
254                 {
255                         Table table = CreateTable ();
256                         
257                         table.Rows.Add (AltRow ("Control Tree"));
258                         table.Rows.Add (SubHeadRow ("Control Id", "Type",
259                                                         "Render Size Bytes (including children)",
260                                                         "View state Size Bytes (excluding children)"));
261                         
262                         int pos = 0;
263                         foreach (DataRow r in control_data.Rows) {
264                                 int depth = (int) r ["Depth"];
265                                 string prefix = String.Empty;
266                                 for (int i=0; i<depth; i++)
267                                         prefix += "&nbsp;&nbsp;&nbsp;&nbsp;";
268                                 RenderAltRow (table, pos++, prefix + r ["ControlId"],
269                                                 r ["Type"].ToString (), "&nbsp;", "&nbsp;");
270                         }
271                         
272                         table.RenderControl (output);
273                 }
274                    
275                 private void RenderCookies (HtmlTextWriter output)
276                 {
277                         Table table = CreateTable ();
278                         
279                         table.Rows.Add (AltRow ("Cookies Collection"));
280                         table.Rows.Add (SubHeadRow ("Name", "Value", "Size"));
281                         
282                         int pos = 0;
283                         foreach (DataRow r in cookie_data.Rows) {
284                                 string name = r ["Name"].ToString ();
285                                 string value = r ["Value"].ToString ();
286                                 int length = name.Length + (value == null ? 0 : value.Length);
287                                 RenderAltRow (table, pos++, name, value, length.ToString ());
288                         }
289                         
290                         table.RenderControl (output);
291                 }
292                 
293                 private void RenderHeaders (HtmlTextWriter output)
294                 {
295                         Table table = CreateTable ();
296                         
297                         table.Rows.Add (AltRow ("Headers Collection"));
298                         table.Rows.Add (SubHeadRow ("Name", "Value"));
299                         
300                         int pos = 0;
301                         foreach (DataRow r in header_data.Rows)
302                                 RenderAltRow (table, pos++, r ["Name"].ToString (), r ["Value"].ToString ());
303                         
304                         table.RenderControl (output);
305                 }
306                 
307                 private void RenderServerVars (HtmlTextWriter output)
308                 {
309                         Table table = CreateTable ();
310                         
311                         table.Rows.Add (AltRow ("Server Variables"));
312                         table.Rows.Add (SubHeadRow ("Name", "Value"));
313                         
314                         int pos = 0;
315                         foreach (DataRow r in servervar_data.Rows)
316                                 RenderAltRow (table, pos++, r ["Name"].ToString (), r ["Value"].ToString ());
317                         
318                         table.RenderControl (output);
319                 }
320                 
321                 internal static TableRow AltRow (string title)
322                 {
323                         TableRow row = new TableRow ();
324                         TableHeaderCell header = new TableHeaderCell ();
325                         header.CssClass = "alt";
326                         header.HorizontalAlign = HorizontalAlign.Left;
327                         header.Attributes [" colspan"] = "10";
328                         header.Text = "<h3><b>" + title + "</b></h3>";
329
330                         row.Cells.Add (header);
331                         return row;
332                 }
333                 
334                 private TableRow RenderTraceInfoRow (Table table, DataRow r, int pos)
335                 {
336                         string open, close;
337                         open = close = String.Empty;
338                         if ((bool) r ["IsWarning"]) {
339                                 open = "<font color=\"Red\">";
340                                 close = "</font>";
341                         }
342                         
343                         double t = (double) r ["TimeSinceFirst"];
344                         string t1, t2;
345                         if (t == 0) {
346                                 t1 = t2 = String.Empty;
347                                 prev_time = 0;
348                         } else {
349                                 t1 = t.ToString ("0.000000");
350                                 t2 = (t - prev_time).ToString ("0.000000");
351                                 prev_time = t;
352                         }
353                         
354                         return RenderAltRow (table, pos, open + (string) r ["Category"] + close,
355                                         open + (string) r ["Message"] + close, t1, t2);
356                 }
357            
358                 internal static TableRow SubHeadRow (params string[] cells)
359                 {
360                         TableRow row = new TableRow ();
361                         foreach (string s in cells) {
362                                 TableHeaderCell cell = new TableHeaderCell ();
363                                 cell.Text = s;
364                                 row.Cells.Add (cell);
365                         }
366                         
367                         row.CssClass = "subhead";
368                         row.HorizontalAlign = HorizontalAlign.Left;
369                         
370                         return row;
371                 }
372                 
373                 internal static TableRow RenderAltRow (Table table, int pos, params string[] cells)
374                 {
375                         TableRow row = new TableRow ();
376                         foreach (string s in cells) {
377                                 TableCell cell = new TableCell ();
378                                 cell.Text = s;
379                                 row.Cells.Add (cell);
380                    }
381                         
382                         if ((pos % 2) != 0)
383                                 row.CssClass = "alt";
384                         
385                         table.Rows.Add (row);
386                         return row;
387                 }
388            
389                 private TableRow InfoRow2 (string title1, string info1, string title2, string info2)
390                 {
391                         TableRow row = new TableRow ();
392                         TableHeaderCell header1 = new TableHeaderCell ();
393                         TableHeaderCell header2 = new TableHeaderCell ();
394                         TableCell cell1 = new TableCell ();
395                         TableCell cell2 = new TableCell ();
396                         
397                         header1.Text = title1;
398                         header2.Text = title2;
399                         cell1.Text = info1;
400                         cell2.Text = info2;
401                         
402                         row.Cells.Add (header1);
403                         row.Cells.Add (cell1);
404                         row.Cells.Add (header2);
405                         row.Cells.Add (cell2);
406
407                         row.HorizontalAlign = HorizontalAlign.Left;
408                         
409                         return row;
410                 }
411                 
412                 internal static Table CreateTable ()
413                 {
414                         Table table = new Table ();
415                         
416                         table.Width = Unit.Percentage (100);
417                         table.CellSpacing = 0;
418                         table.CellPadding = 0;
419                         
420                         return table;
421                 }
422                 
423                 internal static void RenderStyleSheet (HtmlTextWriter o)
424                 {
425                         o.WriteLine ("<style type=\"text/css\">");
426                         o.Write ("span.tracecontent { background-color:white; ");
427                         o.WriteLine ("color:black;font: 10pt verdana, arial; }");
428                         o.Write ("span.tracecontent table { font: 10pt verdana, ");
429                         o.WriteLine ("arial; cellspacing:0; cellpadding:0; margin-bottom:25}");
430                         o.WriteLine ("span.tracecontent tr.subhead { background-color:cccccc;}");
431                         o.WriteLine ("span.tracecontent th { padding:0,3,0,3 }");
432                         o.WriteLine ("span.tracecontent th.alt { background-color:black; color:white; padding:3,3,2,3; }");
433                         o.WriteLine ("span.tracecontent td { padding:0,3,0,3 }");
434                         o.WriteLine ("span.tracecontent tr.alt { background-color:eeeeee }");
435                         o.WriteLine ("span.tracecontent h1 { font: 24pt verdana, arial; margin:0,0,0,0}");
436                         o.WriteLine ("span.tracecontent h2 { font: 18pt verdana, arial; margin:0,0,0,0}");
437                         o.WriteLine ("span.tracecontent h3 { font: 12pt verdana, arial; margin:0,0,0,0}");
438                         o.WriteLine ("span.tracecontent th a { color:darkblue; font: 8pt verdana, arial; }");
439                         o.WriteLine ("span.tracecontent a { color:darkblue;text-decoration:none }");
440                         o.WriteLine ("span.tracecontent a:hover { color:darkblue;text-decoration:underline; }");
441                         o.WriteLine ("span.tracecontent div.outer { width:90%; margin:15,15,15,15}");
442                         o.Write ("span.tracecontent table.viewmenu td { background-color:006699; ");
443                         o.WriteLine ("color:white; padding:0,5,0,5; }");
444                         o.WriteLine ("span.tracecontent table.viewmenu td.end { padding:0,0,0,0; }");
445                         o.WriteLine ("span.tracecontent table.viewmenu a {color:white; font: 8pt verdana, arial; }");
446                         o.WriteLine ("span.tracecontent table.viewmenu a:hover {color:white; font: 8pt verdana, arial; }");
447                         o.WriteLine ("span.tracecontent a.tinylink {color:darkblue; font: 8pt verdana, ");
448                         o.WriteLine ("arial;text-decoration:underline;}");
449                         o.WriteLine ("span.tracecontent a.link {color:darkblue; text-decoration:underline;}");
450                         o.WriteLine ("span.tracecontent div.buffer {padding-top:7; padding-bottom:17;}");
451                         o.WriteLine ("span.tracecontent .small { font: 8pt verdana, arial }");
452                         o.WriteLine ("span.tracecontent table td { padding-right:20 }");
453                         o.WriteLine ("span.tracecontent table td.nopad { padding-right:5 }");
454                         o.WriteLine ("</style>");
455                 }
456         }
457 }
458