tree-wide: make logger a shared_ptr so it actually destructs at the end of the program

It was, of course, just leaked before. Fixing this naturally exposed some bugs.

Change-Id: I013d4631ae424929e28c2705943410592116c965
This commit is contained in:
jade 2024-06-13 20:17:07 -07:00
parent dace8fa1a4
commit 8610f42230
7 changed files with 29 additions and 23 deletions

View file

@ -20,7 +20,7 @@ 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::shared_ptr<Logger> makeDefaultLogger() {
switch (defaultLogFormat) { switch (defaultLogFormat) {
case LogFormat::raw: case LogFormat::raw:
return makeSimpleLogger(false); return makeSimpleLogger(false);

View file

@ -1,10 +1,10 @@
#include "progress-bar.hh" #include "progress-bar.hh"
#include "file-system.hh"
#include "strings.hh"
#include "sync.hh" #include "sync.hh"
#include "store-api.hh"
#include "names.hh" #include "names.hh"
#include "terminal.hh" #include "terminal.hh"
#include <atomic>
#include <map> #include <map>
#include <thread> #include <thread>
#include <sstream> #include <sstream>
@ -119,7 +119,14 @@ public:
{ {
{ {
auto state(state_.lock()); auto state(state_.lock());
if (!state->active) return; if (!state->active) {
// Even if the thread is inactive, the handle needs to be
// explicitly joined to not call terminate if it is destructed.
if (updateThread.joinable()) {
updateThread.join();
}
return;
}
state->active = false; state->active = false;
writeToStderr("\r\e[K"); writeToStderr("\r\e[K");
updateCV.notify_one(); updateCV.notify_one();
@ -531,9 +538,9 @@ public:
} }
}; };
Logger * makeProgressBar() std::shared_ptr<Logger> makeProgressBar()
{ {
return new ProgressBar(shouldANSI()); return std::make_shared<ProgressBar>(shouldANSI());
} }
void startProgressBar() void startProgressBar()
@ -543,7 +550,7 @@ void startProgressBar()
void stopProgressBar() void stopProgressBar()
{ {
auto progressBar = dynamic_cast<ProgressBar *>(logger); auto progressBar = dynamic_cast<ProgressBar *>(logger.get());
if (progressBar) progressBar->stop(); if (progressBar) progressBar->stop();
} }

View file

@ -5,7 +5,7 @@
namespace nix { namespace nix {
Logger * makeProgressBar(); std::shared_ptr<Logger> makeProgressBar();
void startProgressBar(); void startProgressBar();

View file

@ -263,7 +263,7 @@ struct ClientSettings
} }
}; };
static void performOp(TunnelLogger * logger, ref<Store> store, static void performOp(std::shared_ptr<TunnelLogger> logger, ref<Store> store,
TrustedFlag trusted, RecursiveFlag recursive, WorkerProto::Version clientVersion, TrustedFlag trusted, RecursiveFlag recursive, WorkerProto::Version clientVersion,
Source & from, BufferedSink & to, WorkerProto::Op op) Source & from, BufferedSink & to, WorkerProto::Op op)
{ {
@ -1014,7 +1014,7 @@ void processConnection(
if (clientVersion < 0x10a) if (clientVersion < 0x10a)
throw Error("the Nix client version is too old"); throw Error("the Nix client version is too old");
auto tunnelLogger = new TunnelLogger(to, clientVersion); auto tunnelLogger = std::make_shared<TunnelLogger>(to, clientVersion);
auto prevLogger = nix::logger; auto prevLogger = nix::logger;
// FIXME // FIXME
if (!recursive) if (!recursive)

View file

@ -26,7 +26,7 @@ void setCurActivity(const ActivityId activityId)
curActivity = activityId; curActivity = activityId;
} }
Logger * logger = makeSimpleLogger(true); std::shared_ptr<Logger> logger = makeSimpleLogger(true);
void Logger::warn(const std::string & msg) void Logger::warn(const std::string & msg)
{ {
@ -122,9 +122,9 @@ void writeToStderr(std::string_view s)
} }
} }
Logger * makeSimpleLogger(bool printBuildLogs) std::shared_ptr<Logger> makeSimpleLogger(bool printBuildLogs)
{ {
return new SimpleLogger(printBuildLogs); return std::make_shared<SimpleLogger>(printBuildLogs);
} }
std::atomic<uint64_t> nextId{0}; std::atomic<uint64_t> nextId{0};
@ -247,9 +247,9 @@ struct JSONLogger : Logger {
} }
}; };
Logger * makeJSONLogger(Logger & prevLogger) std::shared_ptr<Logger> makeJSONLogger(Logger & prevLogger)
{ {
return new JSONLogger(prevLogger); return std::make_shared<JSONLogger>(prevLogger);
} }
static Logger::Fields getFields(nlohmann::json & json) static Logger::Fields getFields(nlohmann::json & json)

View file

@ -1,7 +1,6 @@
#pragma once #pragma once
///@file ///@file
#include "types.hh"
#include "error.hh" #include "error.hh"
#include "config.hh" #include "config.hh"
@ -176,11 +175,11 @@ struct PushActivity
~PushActivity() { setCurActivity(prevAct); } ~PushActivity() { setCurActivity(prevAct); }
}; };
extern Logger * logger; extern std::shared_ptr<Logger> logger;
Logger * makeSimpleLogger(bool printBuildLogs = true); std::shared_ptr<Logger> makeSimpleLogger(bool printBuildLogs = true);
Logger * makeJSONLogger(Logger & prevLogger); std::shared_ptr<Logger> makeJSONLogger(Logger & prevLogger);
/** /**
* suppress msgs > this * suppress msgs > this

View file

@ -27,12 +27,12 @@ namespace nix {
}; };
class CaptureLogging { class CaptureLogging {
Logger * oldLogger; std::shared_ptr<Logger> oldLogger;
std::unique_ptr<CaptureLogger> tempLogger; std::shared_ptr<CaptureLogger> tempLogger;
public: public:
CaptureLogging() : tempLogger(std::make_unique<CaptureLogger>()) { CaptureLogging() : tempLogger(std::make_shared<CaptureLogger>()) {
oldLogger = logger; oldLogger = logger;
logger = tempLogger.get(); logger = tempLogger;
} }
~CaptureLogging() { ~CaptureLogging() {