1 // **********************************************************************
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
8 // **********************************************************************
13 using System
.Collections
.Generic
;
14 using System
.Diagnostics
;
15 using System
.Globalization
;
17 sealed class TraceUtil
19 internal static void traceSend(BasicStream str
, Ice
.Logger logger
, TraceLevels tl
)
26 using(System
.IO
.StringWriter s
= new System
.IO
.StringWriter(CultureInfo
.CurrentCulture
))
28 byte type
= printMessage(s
, str
);
30 logger
.trace(tl
.protocolCat
, "sending " + getMessageTypeAsString(type
) + " " + s
.ToString());
36 internal static void traceRecv(BasicStream str
, Ice
.Logger logger
, TraceLevels tl
)
43 using(System
.IO
.StringWriter s
= new System
.IO
.StringWriter(CultureInfo
.CurrentCulture
))
45 byte type
= printMessage(s
, str
);
47 logger
.trace(tl
.protocolCat
, "received " + getMessageTypeAsString(type
) + " " + s
.ToString());
53 internal static void trace(string heading
, BasicStream str
, Ice
.Logger logger
, TraceLevels tl
)
60 using(System
.IO
.StringWriter s
= new System
.IO
.StringWriter(CultureInfo
.CurrentCulture
))
65 logger
.trace(tl
.protocolCat
, s
.ToString());
71 private static HashSet
<string> slicingIds
= new HashSet
<string>();
73 internal static void traceSlicing(string kind
, string typeId
, string slicingCat
, Ice
.Logger logger
)
75 lock(typeof(IceInternal
.TraceUtil
))
77 if(slicingIds
.Add(typeId
))
79 using(System
.IO
.StringWriter s
= new System
.IO
.StringWriter(CultureInfo
.CurrentCulture
))
81 s
.Write("unknown " + kind
+ " type `" + typeId
+ "'");
82 logger
.trace(slicingCat
, s
.ToString());
88 public static void dumpStream(BasicStream stream
)
90 int pos
= stream
.pos();
93 byte[] data
= new byte[stream
.size()];
94 stream
.readBlob(data
);
100 public static void dumpOctets(byte[] data
)
104 for(int i
= 0; i
< data
.Length
; i
+= inc
)
106 for(int j
= i
; j
- i
< inc
; j
++)
110 int n
= (int)data
[j
];
128 System
.Console
.Out
.Write(s
+ " ");
132 System
.Console
.Out
.Write(" ");
136 System
.Console
.Out
.Write('"');
138 for(int j
= i
; j
< data
.Length
&& j
- i
< inc
; j
++)
140 // TODO: this needs fixing
141 if(data
[j
] >= (byte)32 && data
[j
] < (byte)127)
143 System
.Console
.Out
.Write((char) data
[j
]);
147 System
.Console
.Out
.Write('.');
151 System
.Console
.Out
.WriteLine('"');
155 private static void printIdentityFacetOperation(System
.IO
.StringWriter s
, BasicStream str
)
159 Ice
.Identity identity
= new Ice
.Identity();
160 identity
.read__(str
);
161 s
.Write("\nidentity = " + str
.instance().identityToString(identity
));
163 string[] facet
= str
.readStringSeq();
164 s
.Write("\nfacet = ");
167 s
.Write(IceUtilInternal
.StringUtil
.escapeString(facet
[0], ""));
170 string operation
= str
.readString();
171 s
.Write("\noperation = " + operation
);
173 catch(System
.IO
.IOException
)
179 private static void printRequest(System
.IO
.StringWriter s
, BasicStream str
)
181 int requestId
= str
.readInt();
182 s
.Write("\nrequest id = " + requestId
);
185 s
.Write(" (oneway)");
188 printRequestHeader(s
, str
);
191 private static void printBatchRequest(System
.IO
.StringWriter s
, BasicStream str
)
193 int batchRequestNum
= str
.readInt();
194 s
.Write("\nnumber of requests = " + batchRequestNum
);
196 for(int i
= 0; i
< batchRequestNum
; ++i
)
198 s
.Write("\nrequest #" + i
+ ':');
199 printRequestHeader(s
, str
);
204 private static void printReply(System
.IO
.StringWriter s
, BasicStream str
)
206 int requestId
= str
.readInt();
207 s
.Write("\nrequest id = " + requestId
);
209 byte replyStatus
= str
.readByte();
210 s
.Write("\nreply status = " + (int)replyStatus
+ ' ');
214 case ReplyStatus
.replyOK
:
220 case ReplyStatus
.replyUserException
:
222 s
.Write("(user exception)");
226 case ReplyStatus
.replyObjectNotExist
:
227 case ReplyStatus
.replyFacetNotExist
:
228 case ReplyStatus
.replyOperationNotExist
:
232 case ReplyStatus
.replyObjectNotExist
:
234 s
.Write("(object not exist)");
238 case ReplyStatus
.replyFacetNotExist
:
240 s
.Write("(facet not exist)");
244 case ReplyStatus
.replyOperationNotExist
:
246 s
.Write("(operation not exist)");
257 printIdentityFacetOperation(s
, str
);
261 case ReplyStatus
.replyUnknownException
:
262 case ReplyStatus
.replyUnknownLocalException
:
263 case ReplyStatus
.replyUnknownUserException
:
267 case ReplyStatus
.replyUnknownException
:
269 s
.Write("(unknown exception)");
273 case ReplyStatus
.replyUnknownLocalException
:
275 s
.Write("(unknown local exception)");
279 case ReplyStatus
.replyUnknownUserException
:
281 s
.Write("(unknown user exception)");
292 string unknown
= str
.readString();
293 s
.Write("\nunknown = " + unknown
);
299 s
.Write("(unknown)");
305 private static void printRequestHeader(System
.IO
.StringWriter s
, BasicStream str
)
307 printIdentityFacetOperation(s
, str
);
311 byte mode
= str
.readByte();
312 s
.Write("\nmode = " + (int)mode
+ ' ');
313 switch((Ice
.OperationMode
)mode
)
315 case Ice
.OperationMode
.Normal
:
321 case Ice
.OperationMode
.Nonmutating
:
323 s
.Write("(nonmutating)");
327 case Ice
.OperationMode
.Idempotent
:
329 s
.Write("(idempotent)");
335 s
.Write("(unknown)");
340 int sz
= str
.readSize();
341 s
.Write("\ncontext = ");
344 string key
= str
.readString();
345 string val
= str
.readString();
346 s
.Write(key
+ '/' + val
);
353 catch(System
.IO
.IOException
)
359 private static byte printHeader(System
.IO
.StringWriter s
, BasicStream str
)
363 str
.readByte(); // Don't bother printing the magic number
368 /* byte pMajor = */ str
.readByte();
369 /* byte pMinor = */ str
.readByte();
370 //s.Write("\nprotocol version = " + (int)pMajor + "." + (int)pMinor);
372 /* byte eMajor = */ str
.readByte();
373 /* byte eMinor = */ str
.readByte();
374 //s.Write("\nencoding version = " + (int)eMajor + "." + (int)eMinor);
376 byte type
= str
.readByte();
377 s
.Write("\nmessage type = " + (int)type
+ " (" + getMessageTypeAsString(type
) + ')');
379 byte compress
= str
.readByte();
380 s
.Write("\ncompression status = " + (int)compress
+ ' ');
385 s
.Write("(not compressed; do not compress response, if any)");
391 s
.Write("(not compressed; compress response, if any)");
397 s
.Write("(compressed; compress response, if any)");
403 s
.Write("(unknown)");
408 int size
= str
.readInt();
409 s
.Write("\nmessage size = " + size
);
412 catch(System
.IO
.IOException
)
419 private static byte printMessage(System
.IO
.StringWriter s
, BasicStream str
)
421 byte type
= printHeader(s
, str
);
425 case Protocol
.closeConnectionMsg
:
426 case Protocol
.validateConnectionMsg
:
432 case Protocol
.requestMsg
:
434 printRequest(s
, str
);
438 case Protocol
.requestBatchMsg
:
440 printBatchRequest(s
, str
);
444 case Protocol
.replyMsg
:
452 s
.Write("(unknown)");
460 internal static void traceHeader(string heading
, BasicStream str
, Ice
.Logger logger
, TraceLevels tl
)
467 using(System
.IO
.StringWriter s
= new System
.IO
.StringWriter(CultureInfo
.CurrentCulture
))
472 logger
.trace(tl
.protocolCat
, s
.ToString());
478 private static string getMessageTypeAsString(byte type
)
482 case Protocol
.requestMsg
:
484 case Protocol
.requestBatchMsg
:
485 return "batch request";
486 case Protocol
.replyMsg
:
488 case Protocol
.closeConnectionMsg
:
489 return "close connection";
490 case Protocol
.validateConnectionMsg
:
491 return "validate connection";