41 enum InternalLogLevel { START = DEBUG_LOG + 1, END };
43 static const uint TIMER_MAX_CHAR_NUMBER = 12;
45 uint mPercPrecision = 0;
49 std::stack<std::pair<double, double>> mIntervals;
52 double mGlobalPercProgress = 0;
61 std::string mProgressMessage;
62 LogLevel mPrintLevel = PROGRESS_LOG;
65 uint mProgressPercStep;
68 bool mIsProgressActive =
false;
71 bool mPrintPerc =
true;
72 bool mPrintMsgDuringProgress =
true;
74 bool mPrintTimer =
false;
81 mIntervals.push({0, 100});
85 void enableIndentation()
override final { mIndent =
true; }
87 void disableIndentation()
override final { mIndent =
false; }
89 void enablePrintPercentage()
override final { mPrintPerc =
true; }
91 void disablePrintPercentage()
override final { mPrintPerc =
false; }
97 mPrintMsgDuringProgress =
true;
100 void disablePrintMessageDuringProgress() override final
102 mPrintMsgDuringProgress =
false;
105 void enablePrintTimer() override final { mPrintTimer =
true; }
107 void disablePrintTimer() override final { mPrintTimer =
false; }
109 void reset() override final
111 while (!mIntervals.empty())
113 mIntervals.push({0, 100});
117 void setMaxLineWidth(uint w)
override final { mLineWidth = w; }
119 void startTimer() override final { mTimer.
start(); }
121 void stopTimer() override final { mTimer.
stop(); }
132 std::pair<double, double>
newP;
133 newP.first = mIntervals.top().first +
134 (mIntervals.top().second - mIntervals.top().first) *
137 (mIntervals.top().second - mIntervals.top().first) * (
toPerc / 100);
138 mGlobalPercProgress =
newP.first;
139 mIntervals.push(
newP);
143 void endTask(
const std::string& action)
override final
145 mGlobalPercProgress = mIntervals.top().second;
146 if (mIntervals.size() > 1) {
150 printLine(action, END);
154 double percentage() const override final
156 double k = std::pow(10, mPercPrecision);
157 uint c = mGlobalPercProgress * k;
161 virtual void setPercentage(uint newPerc)
override
163 if (newPerc >= 0 && newPerc <= 100) {
164 mGlobalPercProgress = (mIntervals.top().first) + mStep * newPerc;
168 void log(
const std::string&
msg)
const override final
170 printLine(
msg, PROGRESS_LOG);
173 void log(
const std::string&
msg, LogLevel
lvl)
const override final
192 const std::string&
msg,
200 mIsProgressActive =
true;
201 mProgressMessage =
msg;
207 if (mProgressStep == 0)
215 mIsProgressActive =
false;
221 assert(mIsProgressActive);
224 mProgressPerc =
progress * mProgressPercStep;
225 if (mPrintMsgDuringProgress)
226 log(mProgressPerc, mProgressMessage, PROGRESS_LOG);
228 setPercentage(mProgressPerc);
245 virtual void alignLeft(
Stream&
o)
const {}
247 virtual void alignRight(
Stream&
o)
const {}
249 virtual void setWidth(Stream& o, uint w)
const {}
251 virtual void flush(Stream& o)
const {}
256 mStep = (mIntervals.top().second - mIntervals.top().first) / 100;
259 void printLine(
const std::string& msg, uint lvl)
const
261 if (!mPrintPerc && msg.empty())
265 LogLevel l = PROGRESS_LOG;
266 if (lvl <= DEBUG_LOG) {
277 s = printPercentage(*stream);
279 s += printIndentation(*stream);
280 printMessage(*stream, msg, lvl, s);
281 printElapsedTime(*stream);
287 uint printPercentage(Stream& o)
const
290 if (mPercPrecision > 0)
291 size += 1 + mPercPrecision;
296 o << percentage() <<
"%]";
300 uint printIndentation(Stream& o)
const
304 uint n = mIntervals.size() - 1;
305 for (uint i = 0; i < n; i++) {
313 void printMessage(Stream& o,
const std::string& msg, uint lvl, uint n)
const
315 uint maxMsgSize = mLineWidth - n;
317 maxMsgSize -= TIMER_MAX_CHAR_NUMBER;
319 case LogLevel::ERROR_LOG:
323 case LogLevel::WARNING_LOG:
327 case LogLevel::PROGRESS_LOG:
331 case LogLevel::DEBUG_LOG:
335 case InternalLogLevel::START:
339 case InternalLogLevel::END:
344 setWidth(o, maxMsgSize);
349 void printElapsedTime(Stream& o)
const
353 setWidth(o, TIMER_MAX_CHAR_NUMBER - 3);
355 o << mTimer.
delay() <<
"s]";
void log(uint perc, const std::string &msg, LogLevel lvl) override final
Prints a message to the logger, with the given level and with the given percentage.
Definition logger.h:183
void startProgress(const std::string &msg, uint progressSize, uint percPrintProgress=10, uint startPerc=0, uint endPerc=100) override final
Allows to easily manage progresses with the logger, along with the progress and endProgress member fu...
Definition logger.h:191