2009-05-21 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / RabbitMQ.Client / src / util / DebugUtil.cs
1 // This source code is dual-licensed under the Apache License, version
2 // 2.0, and the Mozilla Public License, version 1.1.
3 //
4 // The APL v2.0:
5 //
6 //---------------------------------------------------------------------------
7 //   Copyright (C) 2007-2009 LShift Ltd., Cohesive Financial
8 //   Technologies LLC., and Rabbit Technologies Ltd.
9 //
10 //   Licensed under the Apache License, Version 2.0 (the "License");
11 //   you may not use this file except in compliance with the License.
12 //   You may obtain a copy of the License at
13 //
14 //       http://www.apache.org/licenses/LICENSE-2.0
15 //
16 //   Unless required by applicable law or agreed to in writing, software
17 //   distributed under the License is distributed on an "AS IS" BASIS,
18 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 //   See the License for the specific language governing permissions and
20 //   limitations under the License.
21 //---------------------------------------------------------------------------
22 //
23 // The MPL v1.1:
24 //
25 //---------------------------------------------------------------------------
26 //   The contents of this file are subject to the Mozilla Public License
27 //   Version 1.1 (the "License"); you may not use this file except in
28 //   compliance with the License. You may obtain a copy of the License at
29 //   http://www.rabbitmq.com/mpl.html
30 //
31 //   Software distributed under the License is distributed on an "AS IS"
32 //   basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
33 //   License for the specific language governing rights and limitations
34 //   under the License.
35 //
36 //   The Original Code is The RabbitMQ .NET Client.
37 //
38 //   The Initial Developers of the Original Code are LShift Ltd,
39 //   Cohesive Financial Technologies LLC, and Rabbit Technologies Ltd.
40 //
41 //   Portions created before 22-Nov-2008 00:00:00 GMT by LShift Ltd,
42 //   Cohesive Financial Technologies LLC, or Rabbit Technologies Ltd
43 //   are Copyright (C) 2007-2008 LShift Ltd, Cohesive Financial
44 //   Technologies LLC, and Rabbit Technologies Ltd.
45 //
46 //   Portions created by LShift Ltd are Copyright (C) 2007-2009 LShift
47 //   Ltd. Portions created by Cohesive Financial Technologies LLC are
48 //   Copyright (C) 2007-2009 Cohesive Financial Technologies
49 //   LLC. Portions created by Rabbit Technologies Ltd are Copyright
50 //   (C) 2007-2009 Rabbit Technologies Ltd.
51 //
52 //   All Rights Reserved.
53 //
54 //   Contributor(s): ______________________________________.
55 //
56 //---------------------------------------------------------------------------
57 using System;
58 using System.IO;
59 using System.Text;
60 using System.Reflection;
61 using System.Collections;
62
63 namespace RabbitMQ.Util {
64     ///<summary>Miscellaneous debugging and development utilities.</summary>
65     ///<remarks>
66     ///Not part of the public API.
67     ///</remarks>
68     public class DebugUtil {
69         ///<summary>Private constructor - this class has no instances</summary>
70         private DebugUtil() {}
71
72         ///<summary>Print a hex dump of the supplied bytes to stdout.</summary>
73         public static void Dump(byte[] bytes) {
74             Dump(bytes, Console.Out);
75         }
76
77         ///<summary>Print a hex dump of the supplied bytes to the supplied TextWriter.</summary>
78         public static void Dump(byte[] bytes, TextWriter writer) {
79             int rowlen = 16;
80
81             for (int count = 0; count < bytes.Length; count += rowlen) {
82                 int thisRow = Math.Min(bytes.Length - count, rowlen);
83
84                 writer.Write(String.Format("{0:X8}: ", count));
85                 for (int i = 0; i < thisRow; i++) {
86                     writer.Write(String.Format("{0:X2}", bytes[count + i]));
87                 }
88                 for (int i = 0; i < (rowlen - thisRow); i++) {
89                     writer.Write("  ");
90                 }
91                 writer.Write("  ");
92                 for (int i = 0; i < thisRow; i++) {
93                     if (bytes[count + i] >= 32 &&
94                         bytes[count + i] < 128)
95                         {
96                             writer.Write((char) bytes[count + i]);
97                         } else {
98                             writer.Write('.');
99                         }
100                 }
101                 writer.WriteLine();
102             }
103             if (bytes.Length % 16 != 0) {
104                 writer.WriteLine(String.Format("{0:X8}: ", bytes.Length));
105             }
106         }
107
108         ///<summary>Prints an indented key/value pair; used by DumpProperties()</summary>
109         ///<remarks>Recurses into the value using DumpProperties().</remarks>
110         public static void DumpKeyValue(string key, object value, TextWriter writer, int indent) {
111             string prefix = new String(' ', indent + 2) + key + ": ";
112             writer.Write(prefix);
113             DumpProperties(value, writer, indent + 2);
114         }
115
116         ///<summary>Dump properties of objects to the supplied writer.</summary>
117         public static void DumpProperties(object value, TextWriter writer, int indent) {
118             if (value == null) {
119                 writer.WriteLine("(null)");
120             } else if (value is string) {
121                 writer.WriteLine("\"" + ((string) value).Replace("\"", "\\\"") + "\"");
122             } else if (value is byte[]) {
123                 writer.WriteLine("byte[]");
124                 Dump((byte[]) value);
125             } else if (value is ValueType) {
126                 writer.WriteLine(value);
127             } else if (value is IDictionary) {
128                 Type t = value.GetType();
129                 writer.WriteLine(t.FullName);
130                 foreach (DictionaryEntry entry in ((IDictionary) value)) {
131                     DumpKeyValue((string) entry.Key, entry.Value, writer, indent);
132                 }
133             } else if (value is IEnumerable) {
134                 writer.WriteLine("IEnumerable");
135                 int index = 0;
136                 foreach (object v in ((IEnumerable) value)) {
137                     DumpKeyValue(index.ToString(), v, writer, indent);
138                     index++;
139                 }
140             } else {
141                 Type t = value.GetType();
142                 writer.WriteLine(t.FullName);
143                 foreach (PropertyInfo pi in t.GetProperties(BindingFlags.Instance
144                                                             | BindingFlags.Public
145                                                             | BindingFlags.DeclaredOnly))
146                 {
147                     if (pi.GetIndexParameters().Length == 0) {
148                         DumpKeyValue(pi.Name, pi.GetValue(value, new object[0]), writer, indent);
149                     }
150                 }
151             }
152         }
153     }
154 }