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 //////////////////////////////////////////////////////////////////////////////
39 //////////////////////////////////////////////////////////////////////////////
42 ///////////////////////////////////////////////////////////////////////////////
43 // Implementation of inline methods //
49 * @param outputStream the output stream that will receive the traces.
52 DGtal::Trace::Trace(DGtal::TraceWriter &writer):
53 myCurrentLevel(0), myCurrentPrefix (""), myWriter(writer), myProgressBarCurrent(-1), myProgressBarRotation(0), myStyle(false)
60 * We send a last postfixReset to prevent bugs in some TraceWriterTerm
64 DGtal::Trace::~Trace()
67 myWriter.outputStream() << myWriter.postfixReset();
72 * Writes/Displays the object on an output stream.
73 * @param out the output stream where the object is written.
77 DGtal::Trace::selfDisplay( std::ostream & out ) const
83 * Checks the validity/consistency of the object.
84 * @return 'true' if the object is valid, 'false' otherwise.
88 DGtal::Trace::isValid() const
94 * Reset all the variables of the Trace object (indentation level and
100 DGtal::Trace::reset()
102 myProgressBarCurrent = -1;
103 myProgressBarRotation = 0;
105 myCurrentPrefix = "";
107 while( !myKeywordStack.empty() )
108 myKeywordStack.pop();
109 while( !myClockStack.empty() )
115 * Enter a new block and increase the indentation level
116 * @param keyword contains a label to the new block
121 DGtal::Trace::beginBlock(const std::string &keyword)
123 myWriter.outputStream()<< myCurrentPrefix
124 << myWriter.prefixEmphase()
125 << "New Block ["<<keyword << "]"
126 << myWriter.postfixReset()
129 myCurrentPrefix += TRACE_PATTERN;
130 myKeywordStack.push(keyword);
131 myProgressBarCurrent = -1;
132 myProgressBarRotation = 0;
135 Clock *c = new(Clock);
137 myClockStack.push(c);
141 * Leave a current block, decrease the indentation level and display
142 * the associate keyword with ellapsed time in ms.
144 * @return the ellapsed time in the block in milliseconds (Class Clock).
148 DGtal::Trace::endBlock()
153 ASSERT (myCurrentLevel >0);
155 localClock = myClockStack.top();
156 tick = localClock->stopClock();
159 myCurrentPrefix = "";
160 for(unsigned int i = 0; i < myCurrentLevel; i++)
161 myCurrentPrefix += TRACE_PATTERN;
163 myWriter.outputStream() << myCurrentPrefix
164 << myWriter.prefixEmphase()
165 << "EndBlock [" << myKeywordStack.top()
166 << "] (" << tick<<" ms)"
167 << myWriter.postfixReset()<< std::endl;
168 myKeywordStack.pop();
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
179 inline std::ostream &
180 DGtal::Trace::warning()
182 myWriter.outputStream() << myCurrentPrefix << myWriter.prefixWarning();
184 return myWriter.outputStream();
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
193 inline std::ostream &
194 DGtal::Trace::error()
196 myWriter.outputStream() << myCurrentPrefix << myWriter.prefixError();
198 return myWriter.outputStream();
203 * Create a string with an indentation prefix for an emphased trace.
205 * @return the output stream with the prefix
207 inline std::ostream &
208 DGtal::Trace::emphase()
210 myWriter.outputStream() << myCurrentPrefix << myWriter.prefixEmphase();
212 return myWriter.outputStream();
216 * Create a string with an indentation prefix for a normal trace.
217 * @return the cerr output stream with the prefix
219 inline std::ostream &
222 myWriter.outputStream() << myCurrentPrefix;
225 myWriter.outputStream() << myWriter.prefixInfo();
228 return myWriter.outputStream();
232 DGtal::Trace::progressBar(const double currentValue, const double maximumValue)
234 // how wide you want the progress meter to be
235 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;
241 // if the fullness hasn't changed skip display
242 if (dotz == myProgressBarCurrent) return;
244 myProgressBarCurrent = dotz;
245 myProgressBarRotation++;
247 // create the "meter"
249 myWriter.outputStream() << myCurrentPrefix<<"[";
250 // part that's full already
251 for ( ; ii < dotz;ii++)
253 myWriter.outputStream()<< "#";
255 // remaining part (spaces)
256 for ( ; ii < PROGRESSBARWIDTH;ii++)
258 myWriter.outputStream()<< " ";
260 // and back to line begin - do not forget the fflush to avoid output buffering problems!
262 BOOST_STATIC_CONSTANT(std::string, rotation_string = "|\\-/");
263 myProgressBarRotation %= 4;
265 myWriter.outputStream()<< "] " << rotation_string[myProgressBarRotation] << " " << (int)(fraction*100)<<"/100\r";
266 myWriter.outputStream().flush();
271 ///////////////////////////////////////////////////////////////////////////////
272 // Implementation of inline functions and external operators //
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.
282 DGtal::operator<<( std::ostream & out,
283 const Trace & object )
285 object.selfDisplay( out );
290 ///////////////////////////////////////////////////////////////////////////////