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