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.
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.
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/>.
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
24 * Implementation of inline methods defined in Trace.h
26 * This file is part of the DGtal library.
29///////////////////////////////////////////////////////////////////////////////
30// IMPLEMENTATION of inline methods.
31///////////////////////////////////////////////////////////////////////////////
33//////////////////////////////////////////////////////////////////////////////
40#include "DGtal/base/Assert.h" // for ASSERT
41//////////////////////////////////////////////////////////////////////////////
44///////////////////////////////////////////////////////////////////////////////
45// Implementation of inline methods //
51 * @param writer the output stream that will receive the traces.
54DGtal::Trace::Trace(DGtal::TraceWriter &writer):
55 myCurrentLevel(0), myCurrentPrefix (""), myWriter(writer), myProgressBarCurrent(-1), myProgressBarRotation(0), myStyle(false)
62 * We send a last postfixReset to prevent bugs in some TraceWriterTerm
69 myWriter.outputStream() << myWriter.postfixReset();
74 * Writes/Displays the object on an output stream.
75 * @param out the output stream where the object is written.
79DGtal::Trace::selfDisplay( std::ostream & out ) const
85 * Checks the validity/consistency of the object.
86 * @return 'true' if the object is valid, 'false' otherwise.
90DGtal::Trace::isValid() const
96 * Reset all the variables of the Trace object (indentation level and
104 myProgressBarCurrent = -1;
105 myProgressBarRotation = 0;
107 myCurrentPrefix = "";
109 while( !myKeywordStack.empty() )
110 myKeywordStack.pop();
111 while( !myClockStack.empty() )
117 * Enter a new block and increase the indentation level
118 * @param keyword contains a label to the new block
123DGtal::Trace::beginBlock(const std::string &keyword)
125 myWriter.outputStream()<< myCurrentPrefix
126 << myWriter.prefixEmphase()
127 << "New Block ["<<keyword << "]"
128 << myWriter.postfixReset()
131 myCurrentPrefix += TRACE_PATTERN;
132 myKeywordStack.push(keyword);
133 myProgressBarCurrent = -1;
134 myProgressBarRotation = 0;
137 Clock *c = new(Clock);
139 myClockStack.push(c);
143 * Leave a current block, decrease the indentation level and display
144 * the associate keyword with ellapsed time in ms.
146 * @return the ellapsed time in the block in milliseconds (Class Clock).
150DGtal::Trace::endBlock()
155 ASSERT(myCurrentLevel >0);
157 localClock = myClockStack.top();
158 tick = localClock->stopClock();
161 myCurrentPrefix = "";
162 for(unsigned int i = 0; i < myCurrentLevel; i++)
163 myCurrentPrefix += TRACE_PATTERN;
165 myWriter.outputStream() << myCurrentPrefix
166 << myWriter.prefixEmphase()
167 << "EndBlock [" << myKeywordStack.top()
168 << "] (" << tick<<" ms)"
169 << myWriter.postfixReset()<< std::endl;
170 myKeywordStack.pop();
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
182DGtal::Trace::warning()
184 myWriter.outputStream() << myCurrentPrefix << myWriter.prefixWarning();
186 return myWriter.outputStream();
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
198 myWriter.outputStream() << myCurrentPrefix << myWriter.prefixError();
200 return myWriter.outputStream();
205 * Create a string with an indentation prefix for an emphased trace.
207 * @return the output stream with the prefix
210DGtal::Trace::emphase()
212 myWriter.outputStream() << myCurrentPrefix << myWriter.prefixEmphase();
214 return myWriter.outputStream();
218 * Create a string with an indentation prefix for a normal trace.
219 * @return the cerr output stream with the prefix
224 myWriter.outputStream() << myCurrentPrefix;
227 myWriter.outputStream() << myWriter.prefixInfo();
230 return myWriter.outputStream();
234DGtal::Trace::progressBar(const double currentValue, const double maximumValue)
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;
242 // if the fullness hasn't changed skip display
243 if (dotz == myProgressBarCurrent) return;
245 myProgressBarCurrent = dotz;
246 myProgressBarRotation++;
248 // create the "meter"
250 myWriter.outputStream() << myCurrentPrefix<<"[";
251 // part that's full already
252 for ( ; ii < dotz;ii++)
254 myWriter.outputStream()<< "#";
256 // remaining part (spaces)
257 for ( ; ii < PROGRESSBARWIDTH;ii++)
259 myWriter.outputStream()<< " ";
261 // and back to line begin - do not forget the fflush to avoid output buffering problems!
263 const std::string rotation_string = "|\\-/";
264 myProgressBarRotation %= 4;
266 myWriter.outputStream()<< "] " << rotation_string[myProgressBarRotation] << " " << (int)(fraction*100)<<"/100\r";
267 myWriter.outputStream().flush();
272///////////////////////////////////////////////////////////////////////////////
273// Implementation of inline functions and external operators //
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.
283DGtal::operator<<( std::ostream & out,
284 const Trace & object )
286 object.selfDisplay( out );
291///////////////////////////////////////////////////////////////////////////////