2 // Copyright (c) 2010-2017 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
22 #include "progress.hpp"
24 static uint64_t getSec()
28 gettimeofday(&tv, NULL);
32 Progress::Progress(size_t maxProgress, bool inPlace, bool showElapsedTime)
33 : maxProgress(maxProgress), curProgress(0), inPlace(inPlace), showElapsedTime(showElapsedTime), prevLength(0), title("Progress")
36 firstRefresh = getSec();
39 void Progress::setProgress(size_t curProgress)
41 this->curProgress = curProgress;
44 void Progress::setProgress()
46 this->curProgress = maxProgress;
49 uint32_t Progress::addDetail(const string& detail)
51 details.push_back(make_pair(detail, 0));
52 return details.size() - 1;
55 void Progress::setDetail(uint32_t idx, uint32_t val)
57 details[idx].second = val;
60 bool Progress::couldRefresh()
62 uint32_t cur = getSec();
64 return (lastRefresh != cur);
67 void Progress::refresh(bool withNewLine)
69 lastRefresh = getSec();
70 uint64_t elapsed = lastRefresh - firstRefresh;
71 size_t progress = curProgress * 100 / maxProgress;
72 size_t remainingTime = curProgress? (elapsed * maxProgress - elapsed * curProgress) / curProgress : 0;
78 ss << title << ": " << progress << "%";
79 ss << ", remaining: " << remainingTime;
81 ss << ", elapsed: " << elapsed;
82 for (size_t i = 0; i < details.size(); ++i)
83 ss << ", " << details[i].first << ": " << details[i].second;
85 size_t prevLength2 = ss.str().size();
87 while (ss.str().size() < prevLength)
89 prevLength = prevLength2;
91 if (!inPlace || withNewLine)