This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Tls / DebugHelper.cs
1
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
10 // 
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
13 // 
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 //
22 /* Transport Security Layer (TLS)
23  * Copyright (c) 2003-2004 Carlos Guzman Alvarez
24  * 
25  * Permission is hereby granted, free of charge, to any person 
26  * obtaining a copy of this software and associated documentation 
27  * files (the "Software"), to deal in the Software without restriction, 
28  * including without limitation the rights to use, copy, modify, merge, 
29  * publish, distribute, sublicense, and/or sell copies of the Software, 
30  * and to permit persons to whom the Software is furnished to do so, 
31  * subject to the following conditions:
32  * 
33  * The above copyright notice and this permission notice shall be included 
34  * in all copies or substantial portions of the Software.
35  * 
36  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
37  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
38  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
39  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
40  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
41  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
42  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
43  * DEALINGS IN THE SOFTWARE.
44  */
45
46 using System;
47 using System.Diagnostics;
48
49 namespace Mono.Security.Protocol.Tls
50 {
51         internal class DebugHelper
52         {
53                 private static bool isInitialized;
54
55                 [Conditional("DEBUG")]
56                 public static void Initialize()
57                 {
58                         if (!isInitialized)
59                         {
60                                 Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
61                                 // Debug.Listeners.Add(new TextWriterTraceListener(@"c:\ssl.log"));
62                                 Debug.AutoFlush = true;
63                                 Debug.Indent();
64
65                                 isInitialized = true;
66                         }
67                 }
68
69                 [Conditional("DEBUG")]
70                 public static void WriteLine(string format, params object[] args)
71                 {
72                         Initialize();
73                         Debug.WriteLine(String.Format(format, args));
74                 }
75
76                 [Conditional("DEBUG")]
77                 public static void WriteLine(string message)
78                 {
79                         Initialize();
80                         Debug.WriteLine(message);
81                 }
82
83                 [Conditional("DEBUG")]
84                 public static void WriteLine(string message, byte[] buffer)
85                 {
86                         Initialize();
87                         DebugHelper.WriteLine(String.Format("{0} ({1} bytes))", message, buffer.Length));
88                         DebugHelper.WriteBuffer(buffer);
89                 }
90
91                 [Conditional("DEBUG")]
92                 public static void WriteBuffer(byte[] buffer)
93                 {
94                         Initialize();
95                         DebugHelper.WriteBuffer(buffer, 0, buffer.Length);
96                 }
97
98                 [Conditional("DEBUG")]
99                 public static void WriteBuffer(byte[] buffer, int index, int length)
100                 {
101                         Initialize();
102                         for (int i = index; i < length; i += 16)
103                         {
104                                 int count = (length - i) >= 16 ? 16 : (length - i);
105                                 for (int j = 0; j < count; j++)
106                                 {
107                                         Debug.Write(buffer[i + j].ToString("x2") + " ");
108                                 }
109                                 Debug.WriteLine("");
110                         }
111                 }
112         }
113 }