forked from lix-project/lix
extract ProgressBar declaration into its header file
Change-Id: Ica9e2ec41d99eaa196a0d535501edf45c589b2b6
This commit is contained in:
parent
706f482216
commit
61ac408a70
|
@ -36,87 +36,15 @@ static std::string_view storePathToName(std::string_view path)
|
|||
return i == std::string::npos ? base.substr(0, 0) : base.substr(i + 1);
|
||||
}
|
||||
|
||||
// 100 years ought to be enough for anyone (yet sufficiently smaller than max() to not cause signed integer overflow).
|
||||
constexpr const auto A_LONG_TIME = std::chrono::duration_cast<std::chrono::milliseconds>(100 * 365 * 86400s);
|
||||
|
||||
class ProgressBar : public Logger
|
||||
ProgressBar::~ProgressBar()
|
||||
{
|
||||
private:
|
||||
|
||||
struct ActInfo
|
||||
{
|
||||
std::string s, lastLine, phase;
|
||||
ActivityType type = actUnknown;
|
||||
uint64_t done = 0;
|
||||
uint64_t expected = 0;
|
||||
uint64_t running = 0;
|
||||
uint64_t failed = 0;
|
||||
std::map<ActivityType, uint64_t> expectedByType;
|
||||
bool visible = true;
|
||||
ActivityId parent;
|
||||
std::optional<std::string> name;
|
||||
std::chrono::time_point<std::chrono::steady_clock> startTime;
|
||||
};
|
||||
|
||||
struct ActivitiesByType
|
||||
{
|
||||
std::map<ActivityId, std::list<ActInfo>::iterator> its;
|
||||
uint64_t done = 0;
|
||||
uint64_t expected = 0;
|
||||
uint64_t failed = 0;
|
||||
};
|
||||
|
||||
struct State
|
||||
{
|
||||
std::list<ActInfo> activities;
|
||||
std::map<ActivityId, std::list<ActInfo>::iterator> its;
|
||||
|
||||
std::map<ActivityType, ActivitiesByType> activitiesByType;
|
||||
|
||||
uint64_t filesLinked = 0, bytesLinked = 0;
|
||||
|
||||
uint64_t corruptedPaths = 0, untrustedPaths = 0;
|
||||
|
||||
bool active = true;
|
||||
bool paused = false;
|
||||
bool haveUpdate = true;
|
||||
};
|
||||
|
||||
Sync<State> state_;
|
||||
|
||||
std::thread updateThread;
|
||||
|
||||
std::condition_variable quitCV, updateCV;
|
||||
|
||||
bool printBuildLogs = false;
|
||||
bool isTTY;
|
||||
|
||||
public:
|
||||
|
||||
ProgressBar(bool isTTY)
|
||||
: isTTY(isTTY)
|
||||
{
|
||||
state_.lock()->active = isTTY;
|
||||
updateThread = std::thread([&]() {
|
||||
auto state(state_.lock());
|
||||
auto nextWakeup = A_LONG_TIME;
|
||||
while (state->active) {
|
||||
if (!state->haveUpdate)
|
||||
state.wait_for(updateCV, nextWakeup);
|
||||
nextWakeup = draw(*state);
|
||||
state.wait_for(quitCV, std::chrono::milliseconds(50));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
~ProgressBar()
|
||||
{
|
||||
stop();
|
||||
}
|
||||
}
|
||||
|
||||
/* Called by destructor, can't be overridden */
|
||||
void stop() override final
|
||||
{
|
||||
/* Called by destructor, can't be overridden */
|
||||
void ProgressBar::stop()
|
||||
{
|
||||
{
|
||||
auto state(state_.lock());
|
||||
if (!state->active) return;
|
||||
|
@ -126,44 +54,46 @@ public:
|
|||
quitCV.notify_one();
|
||||
}
|
||||
updateThread.join();
|
||||
}
|
||||
}
|
||||
|
||||
void pause() override {
|
||||
void ProgressBar::pause()
|
||||
{
|
||||
state_.lock()->paused = true;
|
||||
writeToStderr("\r\e[K");
|
||||
}
|
||||
}
|
||||
|
||||
void resume() override {
|
||||
void ProgressBar::resume()
|
||||
{
|
||||
state_.lock()->paused = false;
|
||||
writeToStderr("\r\e[K");
|
||||
state_.lock()->haveUpdate = true;
|
||||
updateCV.notify_one();
|
||||
}
|
||||
}
|
||||
|
||||
bool isVerbose() override
|
||||
{
|
||||
bool ProgressBar::isVerbose()
|
||||
{
|
||||
return printBuildLogs;
|
||||
}
|
||||
}
|
||||
|
||||
void log(Verbosity lvl, std::string_view s) override
|
||||
{
|
||||
void ProgressBar::log(Verbosity lvl, std::string_view s)
|
||||
{
|
||||
if (lvl > verbosity) return;
|
||||
auto state(state_.lock());
|
||||
log(*state, lvl, s);
|
||||
}
|
||||
}
|
||||
|
||||
void logEI(const ErrorInfo & ei) override
|
||||
{
|
||||
void ProgressBar::logEI(const ErrorInfo & ei)
|
||||
{
|
||||
auto state(state_.lock());
|
||||
|
||||
std::stringstream oss;
|
||||
showErrorInfo(oss, ei, loggerSettings.showTrace.get());
|
||||
|
||||
log(*state, ei.level, oss.str());
|
||||
}
|
||||
}
|
||||
|
||||
void log(State & state, Verbosity lvl, std::string_view s)
|
||||
{
|
||||
void ProgressBar::log(State & state, Verbosity lvl, std::string_view s)
|
||||
{
|
||||
if (state.active) {
|
||||
writeToStderr("\r\e[K" + filterANSIEscapes(s, !isTTY) + ANSI_NORMAL "\n");
|
||||
draw(state);
|
||||
|
@ -172,11 +102,17 @@ public:
|
|||
if (!isTTY) s2 = filterANSIEscapes(s2, true);
|
||||
writeToStderr(s2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void startActivity(ActivityId act, Verbosity lvl, ActivityType type,
|
||||
const std::string & s, const Fields & fields, ActivityId parent) override
|
||||
{
|
||||
void ProgressBar::startActivity(
|
||||
ActivityId act,
|
||||
Verbosity lvl,
|
||||
ActivityType type,
|
||||
const std::string & s,
|
||||
const Fields & fields,
|
||||
ActivityId parent
|
||||
)
|
||||
{
|
||||
auto state(state_.lock());
|
||||
|
||||
if (lvl <= verbosity && !s.empty() && type != actBuildWaiting)
|
||||
|
@ -238,12 +174,12 @@ public:
|
|||
i->visible = false;
|
||||
|
||||
update(*state);
|
||||
}
|
||||
}
|
||||
|
||||
/* Check whether an activity has an ancestore with the specified
|
||||
/* Check whether an activity has an ancestore with the specified
|
||||
type. */
|
||||
bool hasAncestor(State & state, ActivityType type, ActivityId act)
|
||||
{
|
||||
bool ProgressBar::hasAncestor(State & state, ActivityType type, ActivityId act)
|
||||
{
|
||||
while (act != 0) {
|
||||
auto i = state.its.find(act);
|
||||
if (i == state.its.end()) break;
|
||||
|
@ -251,10 +187,10 @@ public:
|
|||
act = i->second->parent;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void stopActivity(ActivityId act) override
|
||||
{
|
||||
void ProgressBar::stopActivity(ActivityId act)
|
||||
{
|
||||
auto state(state_.lock());
|
||||
|
||||
auto i = state->its.find(act);
|
||||
|
@ -273,10 +209,10 @@ public:
|
|||
}
|
||||
|
||||
update(*state);
|
||||
}
|
||||
}
|
||||
|
||||
void result(ActivityId act, ResultType type, const std::vector<Field> & fields) override
|
||||
{
|
||||
void ProgressBar::result(ActivityId act, ResultType type, const std::vector<Field> & fields)
|
||||
{
|
||||
auto state(state_.lock());
|
||||
|
||||
if (type == resFileLinked) {
|
||||
|
@ -346,16 +282,16 @@ public:
|
|||
state->activitiesByType[type].expected += j;
|
||||
update(*state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void update(State & state)
|
||||
{
|
||||
void ProgressBar::update(State & state)
|
||||
{
|
||||
state.haveUpdate = true;
|
||||
updateCV.notify_one();
|
||||
}
|
||||
}
|
||||
|
||||
std::chrono::milliseconds draw(State & state)
|
||||
{
|
||||
std::chrono::milliseconds ProgressBar::draw(State & state)
|
||||
{
|
||||
auto nextWakeup = A_LONG_TIME;
|
||||
|
||||
state.haveUpdate = false;
|
||||
|
@ -410,10 +346,10 @@ public:
|
|||
writeToStderr("\r" + filterANSIEscapes(line, false, width) + ANSI_NORMAL + "\e[K");
|
||||
|
||||
return nextWakeup;
|
||||
}
|
||||
}
|
||||
|
||||
std::string getStatus(State & state)
|
||||
{
|
||||
std::string ProgressBar::getStatus(State & state)
|
||||
{
|
||||
constexpr auto MiB = 1024.0 * 1024.0;
|
||||
|
||||
std::string res;
|
||||
|
@ -500,10 +436,10 @@ public:
|
|||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
void writeToStdout(std::string_view s) override
|
||||
{
|
||||
void ProgressBar::writeToStdout(std::string_view s)
|
||||
{
|
||||
auto state(state_.lock());
|
||||
if (state->active) {
|
||||
std::cerr << "\r\e[K";
|
||||
|
@ -512,10 +448,10 @@ public:
|
|||
} else {
|
||||
Logger::writeToStdout(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<char> ask(std::string_view msg) override
|
||||
{
|
||||
std::optional<char> ProgressBar::ask(std::string_view msg)
|
||||
{
|
||||
auto state(state_.lock());
|
||||
if (!state->active || !isatty(STDIN_FILENO)) return {};
|
||||
std::cerr << fmt("\r\e[K%s ", msg);
|
||||
|
@ -523,13 +459,12 @@ public:
|
|||
if (s.size() != 1) return {};
|
||||
draw(*state);
|
||||
return s[0];
|
||||
}
|
||||
}
|
||||
|
||||
void setPrintBuildLogs(bool printBuildLogs) override
|
||||
{
|
||||
void ProgressBar::setPrintBuildLogs(bool printBuildLogs)
|
||||
{
|
||||
this->printBuildLogs = printBuildLogs;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Logger * makeProgressBar()
|
||||
{
|
||||
|
|
|
@ -1,10 +1,131 @@
|
|||
#pragma once
|
||||
///@file
|
||||
|
||||
#include <chrono>
|
||||
|
||||
#include "logging.hh"
|
||||
#include "sync.hh"
|
||||
|
||||
namespace nix {
|
||||
|
||||
// 100 years ought to be enough for anyone (yet sufficiently smaller than max() to not cause signed integer overflow).
|
||||
constexpr const auto A_LONG_TIME = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
100 * 365 * std::chrono::seconds(86400)
|
||||
);
|
||||
|
||||
class ProgressBar : public Logger
|
||||
{
|
||||
private:
|
||||
struct ActInfo
|
||||
{
|
||||
std::string s, lastLine, phase;
|
||||
ActivityType type = actUnknown;
|
||||
uint64_t done = 0;
|
||||
uint64_t expected = 0;
|
||||
uint64_t running = 0;
|
||||
uint64_t failed = 0;
|
||||
std::map<ActivityType, uint64_t> expectedByType;
|
||||
bool visible = true;
|
||||
ActivityId parent;
|
||||
std::optional<std::string> name;
|
||||
std::chrono::time_point<std::chrono::steady_clock> startTime;
|
||||
};
|
||||
|
||||
struct ActivitiesByType
|
||||
{
|
||||
std::map<ActivityId, std::list<ActInfo>::iterator> its;
|
||||
uint64_t done = 0;
|
||||
uint64_t expected = 0;
|
||||
uint64_t failed = 0;
|
||||
};
|
||||
|
||||
struct State
|
||||
{
|
||||
std::list<ActInfo> activities;
|
||||
std::map<ActivityId, std::list<ActInfo>::iterator> its;
|
||||
|
||||
std::map<ActivityType, ActivitiesByType> activitiesByType;
|
||||
|
||||
uint64_t filesLinked = 0, bytesLinked = 0;
|
||||
|
||||
uint64_t corruptedPaths = 0, untrustedPaths = 0;
|
||||
|
||||
bool active = true;
|
||||
bool paused = false;
|
||||
bool haveUpdate = true;
|
||||
};
|
||||
|
||||
Sync<State> state_;
|
||||
|
||||
std::thread updateThread;
|
||||
|
||||
std::condition_variable quitCV, updateCV;
|
||||
|
||||
bool printBuildLogs = false;
|
||||
bool isTTY;
|
||||
|
||||
public:
|
||||
|
||||
ProgressBar(bool isTTY)
|
||||
: isTTY(isTTY)
|
||||
{
|
||||
state_.lock()->active = isTTY;
|
||||
updateThread = std::thread([&]() {
|
||||
auto state(state_.lock());
|
||||
auto nextWakeup = A_LONG_TIME;
|
||||
while (state->active) {
|
||||
if (!state->haveUpdate)
|
||||
state.wait_for(updateCV, nextWakeup);
|
||||
nextWakeup = draw(*state);
|
||||
state.wait_for(quitCV, std::chrono::milliseconds(50));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
~ProgressBar();
|
||||
|
||||
void stop() override final;
|
||||
|
||||
void pause() override;
|
||||
|
||||
void resume() override;
|
||||
|
||||
bool isVerbose() override;
|
||||
|
||||
void log(Verbosity lvl, std::string_view s) override;
|
||||
|
||||
void logEI(const ErrorInfo & ei) override;
|
||||
|
||||
void log(State & state, Verbosity lvl, std::string_view s);
|
||||
|
||||
void startActivity(
|
||||
ActivityId act,
|
||||
Verbosity lvl,
|
||||
ActivityType type,
|
||||
const std::string & s,
|
||||
const Fields & fields,
|
||||
ActivityId parent
|
||||
) override;
|
||||
|
||||
bool hasAncestor(State & state, ActivityType type, ActivityId act);
|
||||
|
||||
void stopActivity(ActivityId act) override;
|
||||
|
||||
void result(ActivityId act, ResultType type, const std::vector<Field> & fields) override;
|
||||
|
||||
void update(State & state);
|
||||
|
||||
std::chrono::milliseconds draw(State & state);
|
||||
|
||||
std::string getStatus(State & state);
|
||||
|
||||
void writeToStdout(std::string_view s) override;
|
||||
|
||||
std::optional<char> ask(std::string_view msg) override;
|
||||
|
||||
void setPrintBuildLogs(bool printBuildLogs) override;
|
||||
};
|
||||
|
||||
Logger * makeProgressBar();
|
||||
|
||||
void startProgressBar();
|
||||
|
|
Loading…
Reference in a new issue