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 //////////////////////////////////////////////////////////////////////////////
43 ///////////////////////////////////////////////////////////////////////////////
44 // Implementation of inline methods //
50 * @param writer the output stream that will receive the traces.
53 DGtal::Trace::Trace(DGtal::TraceWriter &writer):
54 myCurrentLevel(0), myCurrentPrefix (""), myWriter(writer), myProgressBarCurrent(-1), myProgressBarRotation(0), myStyle(false)
61 * We send a last postfixReset to prevent bugs in some TraceWriterTerm
65 DGtal::Trace::~Trace()
68 myWriter.outputStream() << myWriter.postfixReset();
73 * Writes/Displays the object on an output stream.
74 * @param out the output stream where the object is written.
78 DGtal::Trace::selfDisplay( std::ostream & out ) const
84 * Checks the validity/consistency of the object.
85 * @return 'true' if the object is valid, 'false' otherwise.
89 DGtal::Trace::isValid() const
95 * Reset all the variables of the Trace object (indentation level and
101 DGtal::Trace::reset()
103 myProgressBarCurrent = -1;
104 myProgressBarRotation = 0;
106 myCurrentPrefix = "";
108 while( !myKeywordStack.empty() )
109 myKeywordStack.pop();
110 while( !myClockStack.empty() )
116 * Enter a new block and increase the indentation level
117 * @param keyword contains a label to the new block
122 DGtal::Trace::beginBlock(const std::string &keyword)
124 myWriter.outputStream()<< myCurrentPrefix
125 << myWriter.prefixEmphase()
126 << "New Block ["<<keyword << "]"
127 << myWriter.postfixReset()
130 myCurrentPrefix += TRACE_PATTERN;
131 myKeywordStack.push(keyword);
132 myProgressBarCurrent = -1;
133 myProgressBarRotation = 0;
136 Clock *c = new(Clock);
138 myClockStack.push(c);
142 * Leave a current block, decrease the indentation level and display
143 * the associate keyword with ellapsed time in ms.
145 * @return the ellapsed time in the block in milliseconds (Class Clock).
149 DGtal::Trace::endBlock()
154 ASSERT (myCurrentLevel >0);
156 localClock = myClockStack.top();
157 tick = localClock->stopClock();
160 myCurrentPrefix = "";
161 for(unsigned int i = 0; i < myCurrentLevel; i++)
162 myCurrentPrefix += TRACE_PATTERN;
164 myWriter.outputStream() << myCurrentPrefix
165 << myWriter.prefixEmphase()
166 << "EndBlock [" << myKeywordStack.top()
167 << "] (" << tick<<" ms)"
168 << myWriter.postfixReset()<< std::endl;
169 myKeywordStack.pop();
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
180 inline std::ostream &
181 DGtal::Trace::warning()
183 myWriter.outputStream() << myCurrentPrefix << myWriter.prefixWarning();
185 return myWriter.outputStream();
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
194 inline std::ostream &
195 DGtal::Trace::error()
197 myWriter.outputStream() << myCurrentPrefix << myWriter.prefixError();
199 return myWriter.outputStream();
204 * Create a string with an indentation prefix for an emphased trace.
206 * @return the output stream with the prefix
208 inline std::ostream &
209 DGtal::Trace::emphase()
211 myWriter.outputStream() << myCurrentPrefix << myWriter.prefixEmphase();
213 return myWriter.outputStream();
217 * Create a string with an indentation prefix for a normal trace.
218 * @return the cerr output stream with the prefix
220 inline std::ostream &
223 myWriter.outputStream() << myCurrentPrefix;
226 myWriter.outputStream() << myWriter.prefixInfo();
229 return myWriter.outputStream();
233 DGtal::Trace::progressBar(const double currentValue, const double maximumValue)
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;
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 const 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 ///////////////////////////////////////////////////////////////////////////////