|
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.Text; |
|
19 using System.Xml; |
|
20 using System.Collections.Generic; |
|
21 using CrashItemLib.Crash.Container; |
|
22 |
|
23 namespace CAPCrashAnalysis.CommandLine.Progress |
|
24 { |
|
25 internal class CACmdLineProgressReporter |
|
26 { |
|
27 #region Constructors |
|
28 public CACmdLineProgressReporter() |
|
29 { |
|
30 } |
|
31 #endregion |
|
32 |
|
33 #region API |
|
34 public void StepBegin( string aMessage, string aKey ) |
|
35 { |
|
36 StepBegin( aMessage, aKey, KNotDefined ); |
|
37 } |
|
38 |
|
39 public void StepBegin( string aMessage, string aKey, int aSubSteps ) |
|
40 { |
|
41 // Check preconditions |
|
42 if ( string.IsNullOrEmpty( aMessage ) ) |
|
43 { |
|
44 throw new ArgumentException( "Step title cannot be undefined" ); |
|
45 } |
|
46 else if ( string.IsNullOrEmpty( aKey ) ) |
|
47 { |
|
48 throw new ArgumentException( "Step key cannot be undefined" ); |
|
49 } |
|
50 // |
|
51 lock ( iSyncRoot ) |
|
52 { |
|
53 Step step = FindStep( aKey ); |
|
54 if ( step != null ) |
|
55 { |
|
56 throw new ArgumentException( "The specified key already exists: " + aKey ); |
|
57 } |
|
58 |
|
59 step = new Step( this, aKey, ++iCurrentStep, aSubSteps ); |
|
60 step.StepBegin( aMessage ); |
|
61 iActiveSteps.Add( aKey, step ); |
|
62 } |
|
63 } |
|
64 |
|
65 public void StepProgress( string aMessage, int aValue, string aKey ) |
|
66 { |
|
67 if (iDetailed) |
|
68 { |
|
69 lock (iSyncRoot) |
|
70 { |
|
71 Step step = FindStep(aKey); |
|
72 if (step == null) |
|
73 { |
|
74 throw new ArgumentException("A step with the specified key was not found: " + aKey); |
|
75 } |
|
76 step.StepProgress(aMessage, aValue); |
|
77 } |
|
78 } |
|
79 } |
|
80 |
|
81 public void StepEnd( string aMessage, string aKey ) |
|
82 { |
|
83 if ( string.IsNullOrEmpty( aKey ) ) |
|
84 { |
|
85 throw new ArgumentException( "Step key cannot be undefined" ); |
|
86 } |
|
87 lock ( iSyncRoot ) |
|
88 { |
|
89 Step step = FindStep( aKey ); |
|
90 if ( step == null ) |
|
91 { |
|
92 throw new ArgumentException( "A step with the specified key was not found: " + aKey ); |
|
93 } |
|
94 step.StepEnd( aMessage ); |
|
95 iActiveSteps.Remove( aKey ); |
|
96 } |
|
97 } |
|
98 |
|
99 public void PrintProgress( string aText, int aStepNumber ) |
|
100 { |
|
101 StringBuilder line = new StringBuilder(); |
|
102 line.Append( "[CA PROGRESS]" ); |
|
103 |
|
104 lock ( iSyncRoot ) |
|
105 { |
|
106 line.Append( " " ); |
|
107 line.Append( "{" ); |
|
108 |
|
109 // Put step count if available |
|
110 if ( iTotalStepCount != KNotDefined ) |
|
111 { |
|
112 line.AppendFormat( "{0:d3}/{1:d3}", aStepNumber, iTotalStepCount ); |
|
113 } |
|
114 else |
|
115 { |
|
116 line.AppendFormat( "{0:d3}/???", aStepNumber, iTotalStepCount ); |
|
117 } |
|
118 |
|
119 line.Append( "}" ); |
|
120 line.AppendFormat( " {0}", aText ); |
|
121 } |
|
122 // |
|
123 string text = line.ToString(); |
|
124 PrintRaw( text ); |
|
125 } |
|
126 #endregion |
|
127 |
|
128 #region Properties |
|
129 public bool Enabled |
|
130 { |
|
131 get { return iEnabled; } |
|
132 set { iEnabled = value; } |
|
133 } |
|
134 |
|
135 public bool Detailed |
|
136 { |
|
137 get { return iDetailed; } |
|
138 set { iDetailed = value; iEnabled = value; } |
|
139 } |
|
140 |
|
141 public int TotalNumberOfSteps |
|
142 { |
|
143 get |
|
144 { |
|
145 lock ( iSyncRoot ) |
|
146 { |
|
147 return iTotalStepCount; |
|
148 } |
|
149 } |
|
150 set |
|
151 { |
|
152 lock ( iSyncRoot ) |
|
153 { |
|
154 iTotalStepCount = value; |
|
155 } |
|
156 } |
|
157 } |
|
158 #endregion |
|
159 |
|
160 #region Internal methods |
|
161 private Step FindStep( string aKey ) |
|
162 { |
|
163 Step ret = null; |
|
164 iActiveSteps.TryGetValue( aKey, out ret ); |
|
165 return ret; |
|
166 } |
|
167 |
|
168 private void PrintRaw( string aText ) |
|
169 { |
|
170 System.Console.WriteLine( aText ); |
|
171 } |
|
172 #endregion |
|
173 |
|
174 #region Internal constants |
|
175 internal const int KNotDefined = -1; |
|
176 #endregion |
|
177 |
|
178 #region Internal classes |
|
179 private class Step |
|
180 { |
|
181 #region Constructors |
|
182 public Step( CACmdLineProgressReporter aParent, string aKey, int aStepNumber, int aSubStepCount ) |
|
183 { |
|
184 iKey = aKey; |
|
185 iParent = aParent; |
|
186 iStepNumber = aStepNumber; |
|
187 iSubStepCount = aSubStepCount; |
|
188 } |
|
189 #endregion |
|
190 |
|
191 #region API |
|
192 public void StepBegin( string aMessage ) |
|
193 { |
|
194 StringBuilder message = new StringBuilder(); |
|
195 message.Append( KPrefixStepBegin ); |
|
196 // |
|
197 if ( string.IsNullOrEmpty( aMessage ) == false ) |
|
198 { |
|
199 message.AppendFormat( " - {0}", aMessage ); |
|
200 } |
|
201 // |
|
202 PrintProgress( message.ToString() ); |
|
203 } |
|
204 |
|
205 public void StepProgress( string aMessage, int aValue ) |
|
206 { |
|
207 float newProgress = ( ( (float) aValue ) / (float) iSubStepCount ) * 100.0f; |
|
208 if ( (int) newProgress != iLastProgressPercentage || aValue == iSubStepCount ) |
|
209 { |
|
210 iLastProgressPercentage = (int) newProgress; |
|
211 iLastProgressMessage = aMessage; |
|
212 ++iNumberOfProgressReportPackets; |
|
213 |
|
214 StringBuilder text = new StringBuilder(); |
|
215 text.AppendFormat( "{0:d3}%", iLastProgressPercentage ); |
|
216 |
|
217 if ( string.IsNullOrEmpty( aMessage ) == false ) |
|
218 { |
|
219 text.Append( " " + aMessage ); |
|
220 } |
|
221 |
|
222 PrintProgress( text.ToString() ); |
|
223 } |
|
224 } |
|
225 |
|
226 public void StepEnd( string aMessage ) |
|
227 { |
|
228 // If we didn't hit 100% completion, then emit a dummy |
|
229 // progress event. |
|
230 if ( iNumberOfProgressReportPackets > 0 && iSubStepCount != KNotDefined && iLastProgressPercentage < 100 ) |
|
231 { |
|
232 StepProgress( iLastProgressMessage, iSubStepCount ); |
|
233 } |
|
234 |
|
235 // Now output end marker |
|
236 StringBuilder message = new StringBuilder(); |
|
237 message.Append( KPrefixStepEnd ); |
|
238 // |
|
239 if ( string.IsNullOrEmpty( aMessage ) == false ) |
|
240 { |
|
241 message.AppendFormat( " - {0}", aMessage ); |
|
242 } |
|
243 // |
|
244 PrintProgress( message.ToString() ); |
|
245 iParent.PrintRaw( string.Empty ); |
|
246 } |
|
247 #endregion |
|
248 |
|
249 #region Internal constants |
|
250 private const string KPrefixStepBegin = "START"; |
|
251 private const string KPrefixStepEnd = "END"; |
|
252 #endregion |
|
253 |
|
254 #region Internal methods |
|
255 private void PrintProgress( string aText ) |
|
256 { |
|
257 iParent.PrintProgress( aText, iStepNumber ); |
|
258 } |
|
259 #endregion |
|
260 |
|
261 #region Data members |
|
262 private readonly string iKey; |
|
263 private readonly int iStepNumber; |
|
264 private readonly CACmdLineProgressReporter iParent; |
|
265 private readonly int iSubStepCount; |
|
266 private string iLastProgressMessage = string.Empty; |
|
267 private int iLastProgressPercentage = KNotDefined; |
|
268 private int iNumberOfProgressReportPackets = 0; |
|
269 #endregion |
|
270 } |
|
271 #endregion |
|
272 |
|
273 #region Data members |
|
274 private bool iEnabled = false; |
|
275 private bool iDetailed = false; |
|
276 private object iSyncRoot = new object(); |
|
277 // |
|
278 private int iTotalStepCount = KNotDefined; |
|
279 private int iCurrentStep = 0; |
|
280 // |
|
281 private Dictionary<string, Step> iActiveSteps = new Dictionary<string, Step>(); |
|
282 #endregion |
|
283 } |
|
284 } |