1 //
2 // System.Web.Util.WebTrace
3 //
4 // Authors:
5 //	Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002 Ximian, Inc (http://www.ximian.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 using System.Collections;
32 using System.Diagnostics;
33 
34 namespace System.Web.Util
35 {
36 	internal class WebTrace
37 	{
38 		static Stack ctxStack;
39 		static bool trace;
40 
WebTrace()41 		static WebTrace ()
42 		{
43 			ctxStack = new Stack ();
44 		}
45 
46 		[Conditional("WEBTRACE")]
PushContext(string context)47 		static public void PushContext (string context)
48 		{
49 			ctxStack.Push (context);
50 			Trace.Indent ();
51 		}
52 
53 		[Conditional("WEBTRACE")]
PopContext()54 		static public void PopContext ()
55 		{
56 			if (ctxStack.Count == 0)
57 				return;
58 
59 			Trace.Unindent ();
60 			ctxStack.Pop ();
61 		}
62 
63 		static public string Context
64 		{
65 			get {
66 				if (ctxStack.Count == 0)
67 					return "No context";
68 
69 				return (string) ctxStack.Peek ();
70 			}
71 		}
72 
73 		static public bool StackTrace
74 		{
75 			get { return trace; }
76 
77 			set { trace = value; }
78 		}
79 
80 		[Conditional("WEBTRACE")]
WriteLine(string msg)81 		static public void WriteLine (string msg)
82 		{
83 			Trace.WriteLine (Format (msg));
84 		}
85 
86 		[Conditional("WEBTRACE")]
WriteLine(string msg, object arg)87 		static public void WriteLine (string msg, object arg)
88 		{
89 			Trace.WriteLine (Format (String.Format (msg, arg)));
90 		}
91 
92 		[Conditional("WEBTRACE")]
WriteLine(string msg, object arg1, object arg2)93 		static public void WriteLine (string msg, object arg1, object arg2)
94 		{
95 			Trace.WriteLine (Format (String.Format (msg, arg1, arg2)));
96 		}
97 
98 		[Conditional("WEBTRACE")]
WriteLine(string msg, object arg1, object arg2, object arg3)99 		static public void WriteLine (string msg, object arg1, object arg2, object arg3)
100 		{
101 			Trace.WriteLine (Format (String.Format (msg, arg1, arg2, arg3)));
102 		}
103 
104 		[Conditional("WEBTRACE")]
WriteLine(string msg, params object [] args)105 		static public void WriteLine (string msg, params object [] args)
106 		{
107 			Trace.WriteLine (Format (String.Format (msg, args)));
108 		}
109 
Format(string msg)110 		static string Format (string msg)
111 		{
112 			if (trace)
113 				return String.Format ("{0}: {1}\n{2}", Context, msg, Environment.StackTrace);
114 			else
115 				return String.Format ("{0}: {1}", Context, msg);
116 		}
117 	}
118 }
119 
120