DGtal 1.4.0
Loading...
Searching...
No Matches
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//////////////////////////////////////////////////////////////////////////////
41
42
43///////////////////////////////////////////////////////////////////////////////
44// Implementation of inline methods //
45
46
47/**
48 * Constructor.
49 *
50 * @param writer the output stream that will receive the traces.
51 */
52inline
53DGtal::Trace::Trace(DGtal::TraceWriter &writer):
54 myCurrentLevel(0), myCurrentPrefix (""), myWriter(writer), myProgressBarCurrent(-1), myProgressBarRotation(0), myStyle(false)
55{
56}
57
58/**
59 * Destructor.
60 *
61 * We send a last postfixReset to prevent bugs in some TraceWriterTerm
62 * terminals.
63 */
64inline
65DGtal::Trace::~Trace()
66{
67 if (myStyle)
68 myWriter.outputStream() << myWriter.postfixReset();
69}
70
71
72/**
73 * Writes/Displays the object on an output stream.
74 * @param out the output stream where the object is written.
75 */
76inline
77void
78DGtal::Trace::selfDisplay( std::ostream & out ) const
79{
80 out << "[Trace]";
81}
82
83/**
84 * Checks the validity/consistency of the object.
85 * @return 'true' if the object is valid, 'false' otherwise.
86 */
87inline
88bool
89DGtal::Trace::isValid() const
90{
91 return true;
92}
93
94/**
95 * Reset all the variables of the Trace object (indentation level and
96 * keyword stack).
97 *
98 */
99inline
100void
101DGtal::Trace::reset()
102{
103 myProgressBarCurrent = -1;
104 myProgressBarRotation = 0;
105 myCurrentLevel = 0;
106 myCurrentPrefix = "";
107 //FIXME may leak??
108 while( !myKeywordStack.empty() )
109 myKeywordStack.pop();
110 while( !myClockStack.empty() )
111 myClockStack.pop();
112
113}
114
115/**
116 * Enter a new block and increase the indentation level
117 * @param keyword contains a label to the new block
118 *
119 */
120inline
121void
122DGtal::Trace::beginBlock(const std::string &keyword)
123{
124 myWriter.outputStream()<< myCurrentPrefix
125 << myWriter.prefixEmphase()
126 << "New Block ["<<keyword << "]"
127 << myWriter.postfixReset()
128 <<std::endl;
129 myCurrentLevel++;
130 myCurrentPrefix += TRACE_PATTERN;
131 myKeywordStack.push(keyword);
132 myProgressBarCurrent = -1;
133 myProgressBarRotation = 0;
134
135 //Block timer start
136 Clock *c = new(Clock);
137 c->startClock();
138 myClockStack.push(c);
139}
140
141/**
142 * Leave a current block, decrease the indentation level and display
143 * the associate keyword with ellapsed time in ms.
144 *
145 * @return the ellapsed time in the block in milliseconds (Class Clock).
146 *
147 */
148inline double
149DGtal::Trace::endBlock()
150{
151 double tick;
152 Clock *localClock;
153
154 ASSERT (myCurrentLevel >0);
155
156 localClock = myClockStack.top();
157 tick = localClock->stopClock();
158
159 myCurrentLevel--;
160 myCurrentPrefix = "";
161 for(unsigned int i = 0; i < myCurrentLevel; i++)
162 myCurrentPrefix += TRACE_PATTERN;
163
164 myWriter.outputStream() << myCurrentPrefix
165 << myWriter.prefixEmphase()
166 << "EndBlock [" << myKeywordStack.top()
167 << "] (" << tick<<" ms)"
168 << myWriter.postfixReset()<< std::endl;
169 myKeywordStack.pop();
170 myClockStack.pop();
171 delete localClock;
172 return tick;
173}
174
175/**
176 * Create a string with an indentation prefix for a warning trace.
177 * The string is postfixed by the keyword "[WRNG]"
178 * @return the output stream with the prefix
179 */
180inline std::ostream &
181DGtal::Trace::warning()
182{
183 myWriter.outputStream() << myCurrentPrefix << myWriter.prefixWarning();
184 myStyle = true;
185 return myWriter.outputStream();
186}
187
188
189/**
190 * Create an output message with an indentation prefix for an emphased (bold) trace.
191 * The string is postfixed by the keyword "[ERR]"
192 * @return the output stream with the prefix
193 */
194inline std::ostream &
195DGtal::Trace::error()
196{
197 myWriter.outputStream() << myCurrentPrefix << myWriter.prefixError();
198 myStyle = true;
199 return myWriter.outputStream();
200}
201
202
203/**
204 * Create a string with an indentation prefix for an emphased trace.
205 *
206 * @return the output stream with the prefix
207 */
208inline std::ostream &
209DGtal::Trace::emphase()
210{
211 myWriter.outputStream() << myCurrentPrefix << myWriter.prefixEmphase();
212 myStyle = true;
213 return myWriter.outputStream();
214}
215
216/**
217 * Create a string with an indentation prefix for a normal trace.
218 * @return the cerr output stream with the prefix
219 */
220inline std::ostream &
221DGtal::Trace::info()
222{
223 myWriter.outputStream() << myCurrentPrefix;
224
225 if (myStyle)
226 myWriter.outputStream() << myWriter.prefixInfo();
227
228 myStyle = false;
229 return myWriter.outputStream();
230}
231
232inline void
233DGtal::Trace::progressBar(const double currentValue, const double maximumValue)
234{
235 // how wide you want the progress meter to be
236 double fraction = currentValue /maximumValue;
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 const 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 */
280inline
281std::ostream&
282DGtal::operator<<( std::ostream & out,
283 const Trace & object )
284{
285 object.selfDisplay( out );
286 return out;
287}
288
289// //
290///////////////////////////////////////////////////////////////////////////////