New test.
[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                         r ["RenderSize"] = GetRenderSize (c);
183                         r ["ViewstateSize"] = GetViewStateSize (c, (ctrl_vs != null) ? ctrl_vs [c] : null);
184
185                         control_data.Rows.Add (r);
186
187                         if (c.HasControls ()) {
188                                 foreach (Control child in c.Controls)
189                                         AddControl (child, control_pos + 1);
190                         }
191                 }
192
193                 int GetRenderSize (Control ctrl)
194                 {
195                         if (sizes == null)
196                                 return 0;
197
198                         object s = sizes [ctrl];
199                         return s == null ? 0 : (int) s;
200                 }
201
202                 static int GetViewStateSize (Control ctrl, object vs)
203                 {
204                         if (vs == null)
205                                 return 0;
206
207                         StringWriter sr = new StringWriter ();
208                         LosFormatter fmt = new LosFormatter ();
209                         fmt.Serialize (sr, vs);
210                         return sr.GetStringBuilder ().Length;
211                 }
212
213                 public void AddCookie (string name, string value)
214                 {
215                         DataRow r = cookie_data.NewRow ();
216
217                         r ["Name"] = name;
218                         r ["Value"] = value;
219
220                         cookie_data.Rows.Add (r);
221                 }
222
223                 public void AddHeader (string name, string value)
224                 {
225                         DataRow r = header_data.NewRow ();
226
227                         r ["Name"] = name;
228                         r ["Value"] = value;
229
230                         header_data.Rows.Add (r);
231                 }
232
233                 public void AddServerVar (string name, string value)
234                 {
235                         DataRow r = servervar_data.NewRow ();
236
237                         r ["Name"] = name;
238                         r ["Value"] = value;
239
240                         servervar_data.Rows.Add (r);
241                 }
242                 
243                 public void Render (HtmlTextWriter output)
244                 {
245                         output.AddAttribute ("id", "__asptrace");
246                         output.RenderBeginTag (HtmlTextWriterTag.Div);
247                         
248                         RenderStyleSheet (output);
249                         
250                         output.AddAttribute ("class", "tracecontent");
251                         output.RenderBeginTag (HtmlTextWriterTag.Span);
252                         
253                         RenderRequestDetails (output);
254                         RenderTraceInfo (output);
255                         RenderControlTree (output);
256                         RenderCookies (output);
257                         RenderHeaders (output);
258                         RenderServerVars (output);
259                         
260                         output.RenderEndTag ();
261                         output.RenderEndTag ();
262                 }
263                 
264                 private void RenderRequestDetails (HtmlTextWriter output)
265                 {
266                         Table table = CreateTable ();
267                         
268                         table.Rows.Add (AltRow ("Request Details:"));
269                         table.Rows.Add (InfoRow2 ("Session Id:", session_id,
270                                                         "Request Type", request_type));
271                         table.Rows.Add (InfoRow2 ("Time of Request:", request_time.ToString (),
272                                                         "State Code:", status_code.ToString ()));
273                         table.Rows.Add (InfoRow2 ("Request Encoding:", request_encoding.EncodingName,
274                                                    "Response Encoding:", response_encoding.EncodingName));           
275                         table.RenderControl (output);
276                 }
277                 
278                 private void RenderTraceInfo (HtmlTextWriter output)
279                 {
280                         Table table = CreateTable ();
281                         
282                         table.Rows.Add (AltRow ("Trace Information"));
283                         table.Rows.Add (SubHeadRow ("Category", "Message", "From First(s)", "From Lasts(s)"));
284                         
285                         int pos = 0;
286                         foreach (DataRow r in info.Rows)
287                                 RenderTraceInfoRow (table, r, pos++);
288                         
289                         table.RenderControl (output);
290                 }
291                 
292                 private void RenderControlTree (HtmlTextWriter output)
293                 {
294                         Table table = CreateTable ();
295                         
296                         int page_vs_size = GetViewStateSize (page, page.GetSavedViewState ());
297                         table.Rows.Add (AltRow ("Control Tree"));
298                         table.Rows.Add (SubHeadRow ("Control Id", "Type",
299                                                 "Render Size Bytes (including children)",
300                                                 String.Format ("View state Size (total: {0} bytes)(excluding children)",
301                                                                 page_vs_size)));
302                         
303                         int pos = 0;
304                         foreach (DataRow r in control_data.Rows) {
305                                 int depth = (int) r ["Depth"];
306                                 string prefix = String.Empty;
307                                 for (int i=0; i<depth; i++)
308                                         prefix += "&nbsp;&nbsp;&nbsp;&nbsp;";
309                                 RenderAltRow (table, pos++, prefix + r ["ControlId"],
310                                                 r ["Type"].ToString (), r ["RenderSize"].ToString (),
311                                                 r ["ViewstateSize"].ToString ());
312                         }
313                         
314                         table.RenderControl (output);
315                 }
316
317                 private void RenderCookies (HtmlTextWriter output)
318                 {
319                         Table table = CreateTable ();
320                         
321                         table.Rows.Add (AltRow ("Cookies Collection"));
322                         table.Rows.Add (SubHeadRow ("Name", "Value", "Size"));
323                         
324                         int pos = 0;
325                         foreach (DataRow r in cookie_data.Rows) {
326                                 string name = r ["Name"].ToString ();
327                                 string value = r ["Value"].ToString ();
328                                 int length = name.Length + (value == null ? 0 : value.Length);
329                                 RenderAltRow (table, pos++, name, value, length.ToString ());
330                         }
331                         
332                         table.RenderControl (output);
333                 }
334                 
335                 private void RenderHeaders (HtmlTextWriter output)
336                 {
337                         Table table = CreateTable ();
338                         
339                         table.Rows.Add (AltRow ("Headers Collection"));
340                         table.Rows.Add (SubHeadRow ("Name", "Value"));
341                         
342                         int pos = 0;
343                         foreach (DataRow r in header_data.Rows)
344                                 RenderAltRow (table, pos++, r ["Name"].ToString (), r ["Value"].ToString ());
345                         
346                         table.RenderControl (output);
347                 }
348                 
349                 private void RenderServerVars (HtmlTextWriter output)
350                 {
351                         Table table = CreateTable ();
352                         
353                         table.Rows.Add (AltRow ("Server Variables"));
354                         table.Rows.Add (SubHeadRow ("Name", "Value"));
355                         
356                         int pos = 0;
357                         foreach (DataRow r in servervar_data.Rows)
358                                 RenderAltRow (table, pos++, r ["Name"].ToString (), r ["Value"].ToString ());
359                         
360                         table.RenderControl (output);
361                 }
362                 
363                 internal static TableRow AltRow (string title)
364                 {
365                         TableRow row = new TableRow ();
366                         TableHeaderCell header = new TableHeaderCell ();
367                         header.CssClass = "alt";
368                         header.HorizontalAlign = HorizontalAlign.Left;
369                         header.Attributes [" colspan"] = "10";
370                         header.Text = "<h3><b>" + title + "</b></h3>";
371
372                         row.Cells.Add (header);
373                         return row;
374                 }
375                 
376                 private TableRow RenderTraceInfoRow (Table table, DataRow r, int pos)
377                 {
378                         string open, close;
379                         open = close = String.Empty;
380                         if ((bool) r ["IsWarning"]) {
381                                 open = "<font color=\"Red\">";
382                                 close = "</font>";
383                         }
384                         
385                         double t = (double) r ["TimeSinceFirst"];
386                         string t1, t2;
387                         if (t == 0) {
388                                 t1 = t2 = String.Empty;
389                                 prev_time = 0;
390                         } else {
391                                 t1 = t.ToString ("0.000000");
392                                 t2 = (t - prev_time).ToString ("0.000000");
393                                 prev_time = t;
394                         }
395                         
396                         return RenderAltRow (table, pos, open + (string) r ["Category"] + close,
397                                         open + (string) r ["Message"] + close, t1, t2);
398                 }
399            
400                 internal static TableRow SubHeadRow (params string[] cells)
401                 {
402                         TableRow row = new TableRow ();
403                         foreach (string s in cells) {
404                                 TableHeaderCell cell = new TableHeaderCell ();
405                                 cell.Text = s;
406                                 row.Cells.Add (cell);
407                         }
408                         
409                         row.CssClass = "subhead";
410                         row.HorizontalAlign = HorizontalAlign.Left;
411                         
412                         return row;
413                 }
414                 
415                 internal static TableRow RenderAltRow (Table table, int pos, params string[] cells)
416                 {
417                         TableRow row = new TableRow ();
418                         foreach (string s in cells) {
419                                 TableCell cell = new TableCell ();
420                                 cell.Text = s;
421                                 row.Cells.Add (cell);
422                    }
423                         
424                         if ((pos % 2) != 0)
425                                 row.CssClass = "alt";
426                         
427                         table.Rows.Add (row);
428                         return row;
429                 }
430            
431                 private TableRow InfoRow2 (string title1, string info1, string title2, string info2)
432                 {
433                         TableRow row = new TableRow ();
434                         TableHeaderCell header1 = new TableHeaderCell ();
435                         TableHeaderCell header2 = new TableHeaderCell ();
436                         TableCell cell1 = new TableCell ();
437                         TableCell cell2 = new TableCell ();
438                         
439                         header1.Text = title1;
440                         header2.Text = title2;
441                         cell1.Text = info1;
442                         cell2.Text = info2;
443                         
444                         row.Cells.Add (header1);
445                         row.Cells.Add (cell1);
446                         row.Cells.Add (header2);
447                         row.Cells.Add (cell2);
448
449                         row.HorizontalAlign = HorizontalAlign.Left;
450                         
451                         return row;
452                 }
453                 
454                 internal static Table CreateTable ()
455                 {
456                         Table table = new Table ();
457                         
458                         table.Width = Unit.Percentage (100);
459                         table.CellSpacing = 0;
460                         table.CellPadding = 0;
461                         
462                         return table;
463                 }
464                 
465                 internal static void RenderStyleSheet (HtmlTextWriter o)
466                 {
467                         o.WriteLine ("<style type=\"text/css\">");
468                         o.Write ("span.tracecontent { background-color:white; ");
469                         o.WriteLine ("color:black;font: 10pt verdana, arial; }");
470                         o.Write ("span.tracecontent table { font: 10pt verdana, ");
471                         o.WriteLine ("arial; cellspacing:0; cellpadding:0; margin-bottom:25}");
472                         o.WriteLine ("span.tracecontent tr.subhead { background-color:cccccc;}");
473                         o.WriteLine ("span.tracecontent th { padding:0,3,0,3 }");
474                         o.WriteLine ("span.tracecontent th.alt { background-color:black; color:white; padding:3,3,2,3; }");
475                         o.WriteLine ("span.tracecontent td { padding:0,3,0,3 }");
476                         o.WriteLine ("span.tracecontent tr.alt { background-color:eeeeee }");
477                         o.WriteLine ("span.tracecontent h1 { font: 24pt verdana, arial; margin:0,0,0,0}");
478                         o.WriteLine ("span.tracecontent h2 { font: 18pt verdana, arial; margin:0,0,0,0}");
479                         o.WriteLine ("span.tracecontent h3 { font: 12pt verdana, arial; margin:0,0,0,0}");
480                         o.WriteLine ("span.tracecontent th a { color:darkblue; font: 8pt verdana, arial; }");
481                         o.WriteLine ("span.tracecontent a { color:darkblue;text-decoration:none }");
482                         o.WriteLine ("span.tracecontent a:hover { color:darkblue;text-decoration:underline; }");
483                         o.WriteLine ("span.tracecontent div.outer { width:90%; margin:15,15,15,15}");
484                         o.Write ("span.tracecontent table.viewmenu td { background-color:006699; ");
485                         o.WriteLine ("color:white; padding:0,5,0,5; }");
486                         o.WriteLine ("span.tracecontent table.viewmenu td.end { padding:0,0,0,0; }");
487                         o.WriteLine ("span.tracecontent table.viewmenu a {color:white; font: 8pt verdana, arial; }");
488                         o.WriteLine ("span.tracecontent table.viewmenu a:hover {color:white; font: 8pt verdana, arial; }");
489                         o.WriteLine ("span.tracecontent a.tinylink {color:darkblue; font: 8pt verdana, ");
490                         o.WriteLine ("arial;text-decoration:underline;}");
491                         o.WriteLine ("span.tracecontent a.link {color:darkblue; text-decoration:underline;}");
492                         o.WriteLine ("span.tracecontent div.buffer {padding-top:7; padding-bottom:17;}");
493                         o.WriteLine ("span.tracecontent .small { font: 8pt verdana, arial }");
494                         o.WriteLine ("span.tracecontent table td { padding-right:20 }");
495                         o.WriteLine ("span.tracecontent table td.nopad { padding-right:5 }");
496                         o.WriteLine ("</style>");
497                 }
498         }
499 }
500