Loading [MathJax]/extensions/TeX/AMSsymbols.js
DGtal 2.0.0
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#include <cmath>
40#include "DGtal/base/Assert.h" // for ASSERT
41//////////////////////////////////////////////////////////////////////////////
42
43
44///////////////////////////////////////////////////////////////////////////////
45// Implementation of inline methods //
46
47
48/**
49 * Constructor.
50 *
51 * @param writer the output stream that will receive the traces.
52 */
53inline
54DGtal::Trace::Trace(DGtal::TraceWriter &writer):
55 myCurrentLevel(0), myCurrentPrefix (""), myWriter(writer), myProgressBarCurrent(-1), myProgressBarRotation(0), myStyle(false)
56{
57}
58
59/**
60 * Destructor.
61 *
62 * We send a last postfixReset to prevent bugs in some TraceWriterTerm
63 * terminals.
64 */
65inline
66DGtal::Trace::~Trace()
67{
68 if (myStyle)
69 myWriter.outputStream() << myWriter.postfixReset();
70}
71
72
73/**
74 * Writes/Displays the object on an output stream.
75 * @param out the output stream where the object is written.
76 */
77inline
78void
79DGtal::Trace::selfDisplay( std::ostream & out ) const
80{
81 out << "[Trace]";
82}
83
84/**
85 * Checks the validity/consistency of the object.
86 * @return 'true' if the object is valid, 'false' otherwise.
87 */
88inline
89bool
90DGtal::Trace::isValid() const
91{
92 return true;
93}
94
95/**
96 * Reset all the variables of the Trace object (indentation level and
97 * keyword stack).
98 *
99 */
100inline
101void
102DGtal::Trace::reset()
103{
104 myProgressBarCurrent = -1;
105 myProgressBarRotation = 0;
106 myCurrentLevel = 0;
107 myCurrentPrefix = "";
108 //FIXME may leak??
109 while( !myKeywordStack.empty() )
110 myKeywordStack.pop();
111 while( !myClockStack.empty() )
112 myClockStack.pop();
113
114}
115
116/**
117 * Enter a new block and increase the indentation level
118 * @param keyword contains a label to the new block
119 *
120 */
121inline
122void
123DGtal::Trace::beginBlock(const std::string &keyword)
124{
125 myWriter.outputStream()<< myCurrentPrefix
126 << myWriter.prefixEmphase()
127 << "New Block ["<<keyword << "]"
128 << myWriter.postfixReset()
129 <<std::endl;
130 myCurrentLevel++;
131 myCurrentPrefix += TRACE_PATTERN;
132 myKeywordStack.push(keyword);
133 myProgressBarCurrent = -1;
134 myProgressBarRotation = 0;
135
136 //Block timer start
137 Clock *c = new(Clock);
138 c->startClock();
139 myClockStack.push(c);
140}
141
142/**
143 * Leave a current block, decrease the indentation level and display
144 * the associate keyword with ellapsed time in ms.
145 *
146 * @return the ellapsed time in the block in milliseconds (Class Clock).
147 *
148 */
149inline double
150DGtal::Trace::endBlock()
151{
152 double tick;
153 Clock *localClock;
154
155 ASSERT(myCurrentLevel >0);
156
157 localClock = myClockStack.top();
158 tick = localClock->stopClock();
159
160 myCurrentLevel--;
161 myCurrentPrefix = "";
162 for(unsigned int i = 0; i < myCurrentLevel; i++)
163 myCurrentPrefix += TRACE_PATTERN;
164
165 myWriter.outputStream() << myCurrentPrefix
166 << myWriter.prefixEmphase()
167 << "EndBlock [" << myKeywordStack.top()
168 << "] (" << tick<<" ms)"
169 << myWriter.postfixReset()<< std::endl;
170 myKeywordStack.pop();
171 myClockStack.pop();
172 delete localClock;
173 return tick;
174}
175
176/**
177 * Create a string with an indentation prefix for a warning trace.
178 * The string is postfixed by the keyword "[WRNG]"
179 * @return the output stream with the prefix
180 */
181inline std::ostream &
182DGtal::Trace::warning()
183{
184 myWriter.outputStream() << myCurrentPrefix << myWriter.prefixWarning();
185 myStyle = true;
186 return myWriter.outputStream();
187}
188
189
190/**
191 * Create an output message with an indentation prefix for an emphased (bold) trace.
192 * The string is postfixed by the keyword "[ERR]"
193 * @return the output stream with the prefix
194 */
195inline std::ostream &
196DGtal::Trace::error()
197{
198 myWriter.outputStream() << myCurrentPrefix << myWriter.prefixError();
199 myStyle = true;
200 return myWriter.outputStream();
201}
202
203
204/**
205 * Create a string with an indentation prefix for an emphased trace.
206 *
207 * @return the output stream with the prefix
208 */
209inline std::ostream &
210DGtal::Trace::emphase()
211{
212 myWriter.outputStream() << myCurrentPrefix << myWriter.prefixEmphase();
213 myStyle = true;
214 return myWriter.outputStream();
215}
216
217/**
218 * Create a string with an indentation prefix for a normal trace.
219 * @return the cerr output stream with the prefix
220 */
221inline std::ostream &
222DGtal::Trace::info()
223{
224 myWriter.outputStream() << myCurrentPrefix;
225
226 if (myStyle)
227 myWriter.outputStream() << myWriter.prefixInfo();
228
229 myStyle = false;
230 return myWriter.outputStream();
231}
232
233inline void
234DGtal::Trace::progressBar(const double currentValue, const double maximumValue)
235{
236 // how wide you want the progress meter to be
237 double fraction = currentValue /maximumValue;
238 // part of the progressmeter that's already "full"
239 int dotz = static_cast<int>(floor(fraction * PROGRESSBARWIDTH));
240 if (dotz > PROGRESSBARWIDTH) dotz = PROGRESSBARWIDTH;
241
242 // if the fullness hasn't changed skip display
243 if (dotz == myProgressBarCurrent) return;
244
245 myProgressBarCurrent = dotz;
246 myProgressBarRotation++;
247
248 // create the "meter"
249 int ii=0;
250 myWriter.outputStream() << myCurrentPrefix<<"[";
251 // part that's full already
252 for ( ; ii < dotz;ii++)
253 {
254 myWriter.outputStream()<< "#";
255 }
256 // remaining part (spaces)
257 for ( ; ii < PROGRESSBARWIDTH;ii++)
258 {
259 myWriter.outputStream()<< " ";
260 }
261 // and back to line begin - do not forget the fflush to avoid output buffering problems!
262
263 const std::string rotation_string = "|\\-/";
264 myProgressBarRotation %= 4;
265
266 myWriter.outputStream()<< "] " << rotation_string[myProgressBarRotation] << " " << (int)(fraction*100)<<"/100\r";
267 myWriter.outputStream().flush();
268}
269
270
271
272///////////////////////////////////////////////////////////////////////////////
273// Implementation of inline functions and external operators //
274
275/**
276 * Overloads 'operator<<' for displaying objects of class 'Trace'.
277 * @param out the output stream where the object is written.
278 * @param object the object of class 'Trace' to write.
279 * @return the output stream after the writing.
280 */
281inline
282std::ostream&
283DGtal::operator<<( std::ostream & out,
284 const Trace & object )
285{
286 object.selfDisplay( out );
287 return out;
288}
289
290// //
291///////////////////////////////////////////////////////////////////////////////