mirror of
https://github.com/NixOS/nix
synced 2025-06-25 10:41:16 +02:00
Merge pull request #6962 from edolstra/unique-ptr-logger
Make 'logger' a std::unique_ptr
This commit is contained in:
commit
258b5ef80b
9 changed files with 53 additions and 43 deletions
|
@ -28,20 +28,15 @@ namespace nix {
|
||||||
};
|
};
|
||||||
|
|
||||||
class CaptureLogging {
|
class CaptureLogging {
|
||||||
Logger * oldLogger;
|
std::unique_ptr<Logger> oldLogger;
|
||||||
std::unique_ptr<CaptureLogger> tempLogger;
|
|
||||||
public:
|
public:
|
||||||
CaptureLogging() : tempLogger(std::make_unique<CaptureLogger>()) {
|
CaptureLogging() {
|
||||||
oldLogger = logger;
|
oldLogger = std::move(logger);
|
||||||
logger = tempLogger.get();
|
logger = std::make_unique<CaptureLogger>();
|
||||||
}
|
}
|
||||||
|
|
||||||
~CaptureLogging() {
|
~CaptureLogging() {
|
||||||
logger = oldLogger;
|
logger = std::move(oldLogger);
|
||||||
}
|
|
||||||
|
|
||||||
std::string get() const {
|
|
||||||
return tempLogger->get();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -113,7 +108,7 @@ namespace nix {
|
||||||
CaptureLogging l;
|
CaptureLogging l;
|
||||||
auto v = eval("builtins.trace \"test string 123\" 123");
|
auto v = eval("builtins.trace \"test string 123\" 123");
|
||||||
ASSERT_THAT(v, IsIntEq(123));
|
ASSERT_THAT(v, IsIntEq(123));
|
||||||
auto text = l.get();
|
auto text = (dynamic_cast<CaptureLogger *>(logger.get()))->get();
|
||||||
ASSERT_NE(text.find("test string 123"), std::string::npos);
|
ASSERT_NE(text.find("test string 123"), std::string::npos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,8 @@ namespace nix {
|
||||||
|
|
||||||
LogFormat defaultLogFormat = LogFormat::raw;
|
LogFormat defaultLogFormat = LogFormat::raw;
|
||||||
|
|
||||||
LogFormat parseLogFormat(const std::string & logFormatStr) {
|
LogFormat parseLogFormat(const std::string & logFormatStr)
|
||||||
|
{
|
||||||
if (logFormatStr == "raw" || getEnv("NIX_GET_COMPLETIONS"))
|
if (logFormatStr == "raw" || getEnv("NIX_GET_COMPLETIONS"))
|
||||||
return LogFormat::raw;
|
return LogFormat::raw;
|
||||||
else if (logFormatStr == "raw-with-logs")
|
else if (logFormatStr == "raw-with-logs")
|
||||||
|
@ -20,7 +21,8 @@ LogFormat parseLogFormat(const std::string & logFormatStr) {
|
||||||
throw Error("option 'log-format' has an invalid value '%s'", logFormatStr);
|
throw Error("option 'log-format' has an invalid value '%s'", logFormatStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger * makeDefaultLogger() {
|
std::unique_ptr<Logger> makeDefaultLogger()
|
||||||
|
{
|
||||||
switch (defaultLogFormat) {
|
switch (defaultLogFormat) {
|
||||||
case LogFormat::raw:
|
case LogFormat::raw:
|
||||||
return makeSimpleLogger(false);
|
return makeSimpleLogger(false);
|
||||||
|
@ -40,16 +42,19 @@ Logger * makeDefaultLogger() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void setLogFormat(const std::string & logFormatStr) {
|
void setLogFormat(const std::string & logFormatStr)
|
||||||
|
{
|
||||||
setLogFormat(parseLogFormat(logFormatStr));
|
setLogFormat(parseLogFormat(logFormatStr));
|
||||||
}
|
}
|
||||||
|
|
||||||
void setLogFormat(const LogFormat & logFormat) {
|
void setLogFormat(const LogFormat & logFormat)
|
||||||
|
{
|
||||||
defaultLogFormat = logFormat;
|
defaultLogFormat = logFormat;
|
||||||
createDefaultLogger();
|
createDefaultLogger();
|
||||||
}
|
}
|
||||||
|
|
||||||
void createDefaultLogger() {
|
void createDefaultLogger()
|
||||||
|
{
|
||||||
logger = makeDefaultLogger();
|
logger = makeDefaultLogger();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -117,13 +117,15 @@ public:
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
auto state(state_.lock());
|
auto state(state_.lock());
|
||||||
if (!state->active) return;
|
if (state->active) {
|
||||||
state->active = false;
|
state->active = false;
|
||||||
writeToStderr("\r\e[K");
|
writeToStderr("\r\e[K");
|
||||||
updateCV.notify_one();
|
updateCV.notify_one();
|
||||||
quitCV.notify_one();
|
quitCV.notify_one();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
updateThread.join();
|
if (updateThread.joinable())
|
||||||
|
updateThread.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
void pause() override {
|
void pause() override {
|
||||||
|
@ -553,9 +555,9 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Logger * makeProgressBar()
|
std::unique_ptr<Logger> makeProgressBar()
|
||||||
{
|
{
|
||||||
return new ProgressBar(isTTY());
|
return std::make_unique<ProgressBar>(isTTY());
|
||||||
}
|
}
|
||||||
|
|
||||||
void startProgressBar()
|
void startProgressBar()
|
||||||
|
@ -565,9 +567,8 @@ void startProgressBar()
|
||||||
|
|
||||||
void stopProgressBar()
|
void stopProgressBar()
|
||||||
{
|
{
|
||||||
auto progressBar = dynamic_cast<ProgressBar *>(logger);
|
if (auto progressBar = dynamic_cast<ProgressBar *>(logger.get()))
|
||||||
if (progressBar) progressBar->stop();
|
progressBar->stop();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
Logger * makeProgressBar();
|
std::unique_ptr<Logger> makeProgressBar();
|
||||||
|
|
||||||
void startProgressBar();
|
void startProgressBar();
|
||||||
|
|
||||||
|
|
|
@ -1041,11 +1041,15 @@ void processConnection(
|
||||||
conn.protoVersion = protoVersion;
|
conn.protoVersion = protoVersion;
|
||||||
conn.features = features;
|
conn.features = features;
|
||||||
|
|
||||||
auto tunnelLogger = new TunnelLogger(conn.to, protoVersion);
|
auto tunnelLogger_ = std::make_unique<TunnelLogger>(conn.to, protoVersion);
|
||||||
auto prevLogger = nix::logger;
|
auto tunnelLogger = tunnelLogger_.get();
|
||||||
|
std::unique_ptr<Logger> prevLogger_;
|
||||||
|
auto prevLogger = logger.get();
|
||||||
// FIXME
|
// FIXME
|
||||||
if (!recursive)
|
if (!recursive) {
|
||||||
logger = tunnelLogger;
|
prevLogger_ = std::move(logger);
|
||||||
|
logger = std::move(tunnelLogger_);
|
||||||
|
}
|
||||||
|
|
||||||
unsigned int opCount = 0;
|
unsigned int opCount = 0;
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,7 @@ void setCurActivity(const ActivityId activityId)
|
||||||
curActivity = activityId;
|
curActivity = activityId;
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger * logger = makeSimpleLogger(true);
|
std::unique_ptr<Logger> logger = makeSimpleLogger(true);
|
||||||
|
|
||||||
void Logger::warn(const std::string & msg)
|
void Logger::warn(const std::string & msg)
|
||||||
{
|
{
|
||||||
|
@ -128,9 +128,9 @@ void writeToStderr(std::string_view s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger * makeSimpleLogger(bool printBuildLogs)
|
std::unique_ptr<Logger> makeSimpleLogger(bool printBuildLogs)
|
||||||
{
|
{
|
||||||
return new SimpleLogger(printBuildLogs);
|
return std::make_unique<SimpleLogger>(printBuildLogs);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::atomic<uint64_t> nextId{0};
|
std::atomic<uint64_t> nextId{0};
|
||||||
|
@ -262,9 +262,9 @@ struct JSONLogger : Logger {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Logger * makeJSONLogger(Descriptor fd)
|
std::unique_ptr<Logger> makeJSONLogger(Descriptor fd)
|
||||||
{
|
{
|
||||||
return new JSONLogger(fd);
|
return std::make_unique<JSONLogger>(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Logger::Fields getFields(nlohmann::json & json)
|
static Logger::Fields getFields(nlohmann::json & json)
|
||||||
|
|
|
@ -180,11 +180,11 @@ struct PushActivity
|
||||||
~PushActivity() { setCurActivity(prevAct); }
|
~PushActivity() { setCurActivity(prevAct); }
|
||||||
};
|
};
|
||||||
|
|
||||||
extern Logger * logger;
|
extern std::unique_ptr<Logger> logger;
|
||||||
|
|
||||||
Logger * makeSimpleLogger(bool printBuildLogs = true);
|
std::unique_ptr<Logger> makeSimpleLogger(bool printBuildLogs = true);
|
||||||
|
|
||||||
Logger * makeJSONLogger(Descriptor fd);
|
std::unique_ptr<Logger> makeJSONLogger(Descriptor fd);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param source A noun phrase describing the source of the message, e.g. "the builder".
|
* @param source A noun phrase describing the source of the message, e.g. "the builder".
|
||||||
|
|
|
@ -200,8 +200,15 @@ static int childEntry(void * arg)
|
||||||
pid_t startProcess(std::function<void()> fun, const ProcessOptions & options)
|
pid_t startProcess(std::function<void()> fun, const ProcessOptions & options)
|
||||||
{
|
{
|
||||||
ChildWrapperFunction wrapper = [&] {
|
ChildWrapperFunction wrapper = [&] {
|
||||||
if (!options.allowVfork)
|
if (!options.allowVfork) {
|
||||||
|
/* Set a simple logger, while releasing (not destroying)
|
||||||
|
the parent logger. We don't want to run the parent
|
||||||
|
logger's destructor since that will crash (e.g. when
|
||||||
|
~ProgressBar() tries to join a thread that doesn't
|
||||||
|
exist. */
|
||||||
|
logger.release();
|
||||||
logger = makeSimpleLogger();
|
logger = makeSimpleLogger();
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
#if __linux__
|
#if __linux__
|
||||||
if (options.dieWithParent && prctl(PR_SET_PDEATHSIG, SIGKILL) == -1)
|
if (options.dieWithParent && prctl(PR_SET_PDEATHSIG, SIGKILL) == -1)
|
||||||
|
|
|
@ -388,8 +388,6 @@ void mainWrapped(int argc, char * * argv)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Finally f([] { logger->stop(); });
|
|
||||||
|
|
||||||
programPath = argv[0];
|
programPath = argv[0];
|
||||||
auto programName = std::string(baseNameOf(programPath));
|
auto programName = std::string(baseNameOf(programPath));
|
||||||
auto extensionPos = programName.find_last_of(".");
|
auto extensionPos = programName.find_last_of(".");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue