|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 using System.Collections.Generic; |
|
19 using System.IO; |
|
20 using System.Text; |
|
21 using SymbianUtils; |
|
22 using SymbianUtils.Threading; |
|
23 |
|
24 namespace SymbianStructuresLib.Debug.Trace |
|
25 { |
|
26 public class TraceLine : IFormattable |
|
27 { |
|
28 #region Enumerations |
|
29 public enum TType |
|
30 { |
|
31 /// <summary> |
|
32 /// Plain text trace data |
|
33 /// </summary> |
|
34 ETypeText = 0, |
|
35 |
|
36 /// <summary> |
|
37 /// Binary trace data that has been decoded back to text |
|
38 /// </summary> |
|
39 ETypeBinary, |
|
40 |
|
41 /// <summary> |
|
42 /// Raw trace data is (usually) binary data that could not be decoded |
|
43 /// </summary> |
|
44 ETypeRaw |
|
45 } |
|
46 #endregion |
|
47 |
|
48 #region Factory |
|
49 public static TraceLine NewText( TraceTimeStamp aTimeStamp, string aPayload ) |
|
50 { |
|
51 TraceLine ret = new TraceLine( aTimeStamp, aPayload, TType.ETypeText ); |
|
52 return ret; |
|
53 } |
|
54 |
|
55 public static TraceLine NewRaw( TraceTimeStamp aTimeStamp, byte[] aRawData ) |
|
56 { |
|
57 TraceLine ret = new TraceLine( aTimeStamp, aRawData ); |
|
58 return ret; |
|
59 } |
|
60 |
|
61 public static TraceLine NewBinary( TraceTimeStamp aTimeStamp, string aPayload ) |
|
62 { |
|
63 TraceLine ret = new TraceLine( aTimeStamp, aPayload, TType.ETypeBinary ); |
|
64 return ret; |
|
65 } |
|
66 #endregion |
|
67 |
|
68 #region Constructors |
|
69 static TraceLine() |
|
70 { |
|
71 TraceIdentifier temp = new TraceIdentifier( 0, 0, 0 ); |
|
72 iTraceIdentifierWidthInCharacters = temp.ToString().Length; |
|
73 } |
|
74 |
|
75 private TraceLine( TraceTimeStamp aTimeStamp, string aPayload, TType aType ) |
|
76 { |
|
77 iTimeStamp = aTimeStamp; |
|
78 iPayload = aPayload; |
|
79 iType = aType; |
|
80 } |
|
81 |
|
82 private TraceLine( TraceTimeStamp aTimeStamp, byte[] aRawData ) |
|
83 : this( aTimeStamp, string.Empty, TType.ETypeRaw ) |
|
84 { |
|
85 StringBuilder payload = new StringBuilder(); |
|
86 // |
|
87 foreach ( byte b in aRawData ) |
|
88 { |
|
89 payload.AppendFormat( "{0:x2} ", b ); |
|
90 } |
|
91 // |
|
92 iPayload = payload.ToString(); |
|
93 } |
|
94 #endregion |
|
95 |
|
96 #region API |
|
97 public void AddPrefix( int aOrder, string aValue ) |
|
98 { |
|
99 if ( string.IsNullOrEmpty( aValue ) == false ) |
|
100 { |
|
101 TXffix item = new TXffix( aOrder, aValue ); |
|
102 InsertInSortedOrder( ref iPrefixes, item ); |
|
103 } |
|
104 } |
|
105 |
|
106 public void AddSuffix( int aOrder, string aValue ) |
|
107 { |
|
108 if ( string.IsNullOrEmpty( aValue ) == false ) |
|
109 { |
|
110 TXffix item = new TXffix( aOrder, aValue ); |
|
111 InsertInSortedOrder( ref iSuffixes, item ); |
|
112 } |
|
113 } |
|
114 #endregion |
|
115 |
|
116 #region Properties |
|
117 public TType Type |
|
118 { |
|
119 get { return iType; } |
|
120 } |
|
121 |
|
122 public string Prefix |
|
123 { |
|
124 get |
|
125 { |
|
126 string ret = XffixListToString( iPrefixes ); |
|
127 return ret; |
|
128 } |
|
129 } |
|
130 |
|
131 public string Suffix |
|
132 { |
|
133 get |
|
134 { |
|
135 string ret = XffixListToString( iSuffixes ); |
|
136 return ret; |
|
137 } |
|
138 } |
|
139 |
|
140 public string Payload |
|
141 { |
|
142 get { return iPayload; } |
|
143 } |
|
144 |
|
145 public uint ContextId |
|
146 { |
|
147 get { return iContextId; } |
|
148 set { iContextId = value; } |
|
149 } |
|
150 |
|
151 public bool HasIdentifier |
|
152 { |
|
153 get { return iIdentifier != null; } |
|
154 } |
|
155 |
|
156 public TraceTimeStamp TimeStamp |
|
157 { |
|
158 get { return iTimeStamp; } |
|
159 } |
|
160 |
|
161 public TraceIdentifier Identifier |
|
162 { |
|
163 get { return iIdentifier; } |
|
164 set { iIdentifier = value; } |
|
165 } |
|
166 #endregion |
|
167 |
|
168 #region From System.Object |
|
169 public override string ToString() |
|
170 { |
|
171 StringBuilder ret = new StringBuilder(); |
|
172 |
|
173 // First comes time stamp |
|
174 ret.Append( iTimeStamp.ToString() + " " ); |
|
175 |
|
176 // Prefix with type |
|
177 switch ( iType ) |
|
178 { |
|
179 case TType.ETypeBinary: |
|
180 ret.Append( "[BIN]" ); |
|
181 break; |
|
182 case TType.ETypeRaw: |
|
183 ret.Append( "[RAW]" ); |
|
184 break; |
|
185 case TType.ETypeText: |
|
186 ret.Append( "[ASC]" ); |
|
187 break; |
|
188 default: |
|
189 ret.Append( "[???]" ); |
|
190 break; |
|
191 } |
|
192 ret.Append( " " ); |
|
193 |
|
194 // Context Id (if available) |
|
195 if ( iContextId != 0 ) |
|
196 { |
|
197 ret.AppendFormat( "THD: [{0:x8}]", iContextId ); |
|
198 } |
|
199 else |
|
200 { |
|
201 ret.Append( " " ); |
|
202 } |
|
203 ret.Append( " " ); |
|
204 |
|
205 // Add identifier if present |
|
206 if ( iIdentifier != null ) |
|
207 { |
|
208 ret.Append( "ID: " + iIdentifier.ToString() ); |
|
209 } |
|
210 else |
|
211 { |
|
212 ret.Append( " " ); |
|
213 ret.Append( "".PadRight( iTraceIdentifierWidthInCharacters, ' ' ) ); |
|
214 } |
|
215 |
|
216 // Text aspect |
|
217 ret.Append( this.GetText() ); |
|
218 |
|
219 // Done |
|
220 return ret.ToString(); |
|
221 } |
|
222 #endregion |
|
223 |
|
224 #region From IFormattable |
|
225 public string ToString( string aFormat, IFormatProvider formatProvider ) |
|
226 { |
|
227 string ret = string.Empty; |
|
228 // |
|
229 if ( aFormat.ToUpper() == "TEXT" ) |
|
230 { |
|
231 ret = this.GetText(); |
|
232 } |
|
233 else |
|
234 { |
|
235 ret = this.ToString(); |
|
236 } |
|
237 // |
|
238 return ret; |
|
239 } |
|
240 #endregion |
|
241 |
|
242 #region Internal constants |
|
243 #endregion |
|
244 |
|
245 #region Internal class |
|
246 private class TXffix : IComparer<TXffix> |
|
247 { |
|
248 #region Constructors |
|
249 public TXffix( int aOrder, string aValue ) |
|
250 { |
|
251 iOrder = aOrder; |
|
252 iValue = aValue; |
|
253 } |
|
254 #endregion |
|
255 |
|
256 #region From System.Object |
|
257 public override string ToString() |
|
258 { |
|
259 return iValue; |
|
260 } |
|
261 #endregion |
|
262 |
|
263 #region From IComparer<TXffix> |
|
264 public int Compare( TXffix aLeft, TXffix aRight ) |
|
265 { |
|
266 int ret = aLeft.iOrder.CompareTo( aRight.iOrder ); |
|
267 return ret; |
|
268 } |
|
269 #endregion |
|
270 |
|
271 #region Data members |
|
272 private readonly int iOrder; |
|
273 private readonly string iValue; |
|
274 #endregion |
|
275 } |
|
276 #endregion |
|
277 |
|
278 #region Internal methods |
|
279 private void InsertInSortedOrder( ref List<TXffix> aList, TXffix aValue ) |
|
280 { |
|
281 if ( aList == null ) |
|
282 { |
|
283 aList = new List<TXffix>(); |
|
284 aList.Add( aValue ); |
|
285 } |
|
286 else |
|
287 { |
|
288 int pos = aList.BinarySearch( aValue, aValue as IComparer<TXffix> ); |
|
289 if ( pos < 0 ) |
|
290 { |
|
291 pos = ~pos; |
|
292 } |
|
293 aList.Insert( pos, aValue ); |
|
294 } |
|
295 } |
|
296 |
|
297 private string XffixListToString( List<TXffix> aList ) |
|
298 { |
|
299 string ret = string.Empty; |
|
300 // |
|
301 if ( aList != null ) |
|
302 { |
|
303 StringBuilder sb = new StringBuilder(); |
|
304 // |
|
305 foreach ( TXffix item in aList ) |
|
306 { |
|
307 sb.Append( item ); |
|
308 sb.Append( " " ); |
|
309 } |
|
310 // |
|
311 ret = sb.ToString(); |
|
312 } |
|
313 // |
|
314 return ret; |
|
315 } |
|
316 |
|
317 private string GetText() |
|
318 { |
|
319 StringBuilder ret = new StringBuilder(); |
|
320 |
|
321 // Now prefix, payload, suffix |
|
322 string prefix = XffixListToString( iPrefixes ); |
|
323 string suffix = XffixListToString( iSuffixes ); |
|
324 |
|
325 ret.AppendFormat( " {0}{1}{2}", prefix, iPayload, suffix ); |
|
326 |
|
327 return ret.ToString(); |
|
328 } |
|
329 #endregion |
|
330 |
|
331 #region Data members |
|
332 private static int iTraceIdentifierWidthInCharacters; |
|
333 private readonly TraceTimeStamp iTimeStamp; |
|
334 private readonly string iPayload; |
|
335 private readonly TType iType; |
|
336 private uint iContextId = 0; |
|
337 private List<TXffix> iPrefixes = null; |
|
338 private List<TXffix> iSuffixes = null; |
|
339 private TraceIdentifier iIdentifier = null; |
|
340 #endregion |
|
341 } |
|
342 } |