Logger: Add method for writing to stdout

Usually this just writes to stdout, but for ProgressBar, we need to
clear the current line, write the line to stdout, and then redraw the
progress bar.

(cherry picked from commit 696c026006)
This commit is contained in:
Eelco Dolstra 2020-04-16 13:12:58 +02:00
parent fcd048a526
commit 67a5941472
3 changed files with 29 additions and 0 deletions

View file

@ -3,6 +3,7 @@
#include <atomic>
#include <nlohmann/json.hpp>
#include <iostream>
namespace nix {
@ -24,6 +25,11 @@ void Logger::warn(const std::string & msg)
log(lvlWarn, ANSI_YELLOW "warning:" ANSI_NORMAL " " + msg);
}
void Logger::writeToStdout(std::string_view s)
{
std::cout << s << "\n";
}
class SimpleLogger : public Logger
{
public:

View file

@ -78,6 +78,16 @@ public:
virtual void stopActivity(ActivityId act) { };
virtual void result(ActivityId act, ResultType type, const Fields & fields) { };
virtual void writeToStdout(std::string_view s);
template<typename... Args>
inline void stdout(const std::string & fs, const Args & ... args)
{
boost::format f(fs);
formatHelper(f, args...);
writeToStdout(f.str());
}
};
ActivityId getCurActivity();

View file

@ -7,6 +7,7 @@
#include <atomic>
#include <map>
#include <thread>
#include <iostream>
namespace nix {
@ -442,6 +443,18 @@ public:
return res;
}
void writeToStdout(std::string_view s) override
{
auto state(state_.lock());
if (state->active) {
std::cerr << "\r\e[K";
Logger::writeToStdout(s);
draw(*state);
} else {
Logger::writeToStdout(s);
}
}
};
void startProgressBar(bool printBuildLogs)