DGtal  0.9.2
Trace.ih
1 /**
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  **/
16 
17 /**
18  * @file Trace.ih
19  * @author David Coeurjolly (\c david.coeurjolly@liris.cnrs.fr )
20  * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
21  *
22  * @date 2009/12/19
23  *
24  * Implementation of inline methods defined in Trace.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 ///////////////////////////////////////////////////////////////////////////////
30 // IMPLEMENTATION of inline methods.
31 ///////////////////////////////////////////////////////////////////////////////
32 
33 //////////////////////////////////////////////////////////////////////////////
34 #include <cstdlib>
35 #include <string>
36 #include <ostream>
37 #include <iostream>
38 #include <stack>
39 //////////////////////////////////////////////////////////////////////////////
40 
41 
42 ///////////////////////////////////////////////////////////////////////////////
43 // Implementation of inline methods //
44 
45 
46 /**
47  * Constructor.
48  *
49  * @param outputStream the output stream that will receive the traces.
50  */
51 inline
52 DGtal::Trace::Trace(DGtal::TraceWriter &writer):
53  myCurrentLevel(0), myCurrentPrefix (""), myWriter(writer), myProgressBarCurrent(-1), myProgressBarRotation(0), myStyle(false)
54 {
55 }
56 
57 /**
58  * Destructor.
59  *
60  * We send a last postfixReset to prevent bugs in some TraceWriterTerm
61  * terminals.
62  */
63 inline
64 DGtal::Trace::~Trace()
65 {
66  if (myStyle)
67  myWriter.outputStream() << myWriter.postfixReset();
68 }
69 
70 
71 /**
72  * Writes/Displays the object on an output stream.
73  * @param out the output stream where the object is written.
74  */
75 inline
76 void
77 DGtal::Trace::selfDisplay( std::ostream & out ) const
78 {
79  out << "[Trace]";
80 }
81 
82 /**
83  * Checks the validity/consistency of the object.
84  * @return 'true' if the object is valid, 'false' otherwise.
85  */
86 inline
87 bool
88 DGtal::Trace::isValid() const
89 {
90  return true;
91 }
92 
93 /**
94  * Reset all the variables of the Trace object (indentation level and
95  * keyword stack).
96  *
97  */
98 inline
99 void
100 DGtal::Trace::reset()
101 {
102  myProgressBarCurrent = -1;
103  myProgressBarRotation = 0;
104  myCurrentLevel = 0;
105  myCurrentPrefix = "";
106  //FIXME may leak??
107  while( !myKeywordStack.empty() )
108  myKeywordStack.pop();
109  while( !myClockStack.empty() )
110  myClockStack.pop();
111 
112 }
113 
114 /**
115  * Enter a new block and increase the indentation level
116  * @param keyword contains a label to the new block
117  *
118  */
119 inline
120 void
121 DGtal::Trace::beginBlock(const std::string &keyword)
122 {
123  myWriter.outputStream()<< myCurrentPrefix
124  << myWriter.prefixEmphase()
125  << "New Block ["<<keyword << "]"
126  << myWriter.postfixReset()
127  <<std::endl;
128  myCurrentLevel++;
129  myCurrentPrefix += TRACE_PATTERN;
130  myKeywordStack.push(keyword);
131  myProgressBarCurrent = -1;
132  myProgressBarRotation = 0;
133 
134  //Block timer start
135  Clock *c = new(Clock);
136  c->startClock();
137  myClockStack.push(c);
138 }
139 
140 /**
141  * Leave a current block, decrease the indentation level and display
142  * the associate keyword with ellapsed time in ms.
143  *
144  * @return the ellapsed time in the block in milliseconds (Class Clock).
145  *
146  */
147 inline double
148 DGtal::Trace::endBlock()
149 {
150  double tick;
151  Clock *localClock;
152 
153  ASSERT (myCurrentLevel >0);
154 
155  localClock = myClockStack.top();
156  tick = localClock->stopClock();
157 
158  myCurrentLevel--;
159  myCurrentPrefix = "";
160  for(unsigned int i = 0; i < myCurrentLevel; i++)
161  myCurrentPrefix += TRACE_PATTERN;
162 
163  myWriter.outputStream() << myCurrentPrefix
164  << myWriter.prefixEmphase()
165  << "EndBlock [" << myKeywordStack.top()
166  << "] (" << tick<<" ms)"
167  << myWriter.postfixReset()<< std::endl;
168  myKeywordStack.pop();
169  myClockStack.pop();
170  delete localClock;
171  return tick;
172 }
173 
174 /**
175  * Create a string with an indentation prefix for a warning trace.
176  * The string is postfixed by the keyword "[WRNG]"
177  * @return the output stream with the prefix
178  */
179 inline std::ostream &
180 DGtal::Trace::warning()
181 {
182  myWriter.outputStream() << myCurrentPrefix << myWriter.prefixWarning();
183  myStyle = true;
184  return myWriter.outputStream();
185 }
186 
187 
188 /**
189  * Create an output message with an indentation prefix for an emphased (bold) trace.
190  * The string is postfixed by the keyword "[ERR]"
191  * @return the output stream with the prefix
192  */
193 inline std::ostream &
194 DGtal::Trace::error()
195 {
196  myWriter.outputStream() << myCurrentPrefix << myWriter.prefixError();
197  myStyle = true;
198  return myWriter.outputStream();
199 }
200 
201 
202 /**
203  * Create a string with an indentation prefix for an emphased trace.
204  *
205  * @return the output stream with the prefix
206  */
207 inline std::ostream &
208 DGtal::Trace::emphase()
209 {
210  myWriter.outputStream() << myCurrentPrefix << myWriter.prefixEmphase();
211  myStyle = true;
212  return myWriter.outputStream();
213 }
214 
215 /**
216  * Create a string with an indentation prefix for a normal trace.
217  * @return the cerr output stream with the prefix
218  */
219 inline std::ostream &
220 DGtal::Trace::info()
221 {
222  myWriter.outputStream() << myCurrentPrefix;
223 
224  if (myStyle)
225  myWriter.outputStream() << myWriter.prefixInfo();
226 
227  myStyle = false;
228  return myWriter.outputStream();
229 }
230 
231 inline void
232 DGtal::Trace::progressBar(const double currentValue, const double maximumValue)
233 {
234  // how wide you want the progress meter to be
235  double fraction = currentValue /maximumValue;
236 
237  // part of the progressmeter that's already "full"
238  int dotz = static_cast<int>(floor(fraction * PROGRESSBARWIDTH));
239  if (dotz > PROGRESSBARWIDTH) dotz = PROGRESSBARWIDTH;
240 
241  // if the fullness hasn't changed skip display
242  if (dotz == myProgressBarCurrent) return;
243 
244  myProgressBarCurrent = dotz;
245  myProgressBarRotation++;
246 
247  // create the "meter"
248  int ii=0;
249  myWriter.outputStream() << myCurrentPrefix<<"[";
250  // part that's full already
251  for ( ; ii < dotz;ii++)
252  {
253  myWriter.outputStream()<< "#";
254  }
255  // remaining part (spaces)
256  for ( ; ii < PROGRESSBARWIDTH;ii++)
257  {
258  myWriter.outputStream()<< " ";
259  }
260  // and back to line begin - do not forget the fflush to avoid output buffering problems!
261 
262  BOOST_STATIC_CONSTANT(std::string, rotation_string = "|\\-/");
263  myProgressBarRotation %= 4;
264 
265  myWriter.outputStream()<< "] " << rotation_string[myProgressBarRotation] << " " << (int)(fraction*100)<<"/100\r";
266  myWriter.outputStream().flush();
267 }
268 
269 
270 
271 ///////////////////////////////////////////////////////////////////////////////
272 // Implementation of inline functions and external operators //
273 
274 /**
275  * Overloads 'operator<<' for displaying objects of class 'Trace'.
276  * @param out the output stream where the object is written.
277  * @param object the object of class 'Trace' to write.
278  * @return the output stream after the writing.
279  */
280 inline
281 std::ostream&
282 DGtal::operator<<( std::ostream & out,
283  const Trace & object )
284 {
285  object.selfDisplay( out );
286  return out;
287 }
288 
289 // //
290 ///////////////////////////////////////////////////////////////////////////////