showTrace flag in loggers

This commit is contained in:
Ben Burdette 2020-06-29 10:20:51 -06:00
parent ef24a0835d
commit 8f81fae116
10 changed files with 60 additions and 20 deletions

View file

@ -22,11 +22,11 @@ LogFormat parseLogFormat(const std::string & logFormatStr) {
Logger * makeDefaultLogger() { Logger * makeDefaultLogger() {
switch (defaultLogFormat) { switch (defaultLogFormat) {
case LogFormat::raw: case LogFormat::raw:
return makeSimpleLogger(false); return makeSimpleLogger(false, false);
case LogFormat::rawWithLogs: case LogFormat::rawWithLogs:
return makeSimpleLogger(true); return makeSimpleLogger(true, false);
case LogFormat::internalJson: case LogFormat::internalJson:
return makeJSONLogger(*makeSimpleLogger()); return makeJSONLogger(*makeSimpleLogger(true, false));
case LogFormat::bar: case LogFormat::bar:
return makeProgressBar(); return makeProgressBar();
case LogFormat::barWithLogs: case LogFormat::barWithLogs:

View file

@ -81,12 +81,14 @@ private:
bool printBuildLogs; bool printBuildLogs;
bool isTTY; bool isTTY;
bool showTrace;
public: public:
ProgressBar(bool printBuildLogs, bool isTTY) ProgressBar(bool printBuildLogs, bool isTTY)
: printBuildLogs(printBuildLogs) : printBuildLogs(printBuildLogs)
, isTTY(isTTY) , isTTY(isTTY)
, showTrace(false)
{ {
state_.lock()->active = isTTY; state_.lock()->active = isTTY;
updateThread = std::thread([&]() { updateThread = std::thread([&]() {
@ -131,10 +133,17 @@ public:
auto state(state_.lock()); auto state(state_.lock());
std::stringstream oss; std::stringstream oss;
oss << ei; showErrorInfo(oss, ei, showTrace);
// oss << ei;
log(*state, ei.level, oss.str()); log(*state, ei.level, oss.str());
} }
bool getShowTrace() const override {
return showTrace;
}
void setShowTrace(bool showTrace) override {
this->showTrace = showTrace;
}
void log(State & state, Verbosity lvl, const std::string & s) void log(State & state, Verbosity lvl, const std::string & s)
{ {

View file

@ -303,7 +303,6 @@ int handleExceptions(const string & programName, std::function<void()> fun)
ReceiveInterrupts receiveInterrupts; // FIXME: need better place for this ReceiveInterrupts receiveInterrupts; // FIXME: need better place for this
ErrorInfo::programName = baseNameOf(programName); ErrorInfo::programName = baseNameOf(programName);
ErrorInfo::showTrace = settings.showTrace;
string error = ANSI_RED "error:" ANSI_NORMAL " "; string error = ANSI_RED "error:" ANSI_NORMAL " ";
try { try {
@ -324,6 +323,7 @@ int handleExceptions(const string & programName, std::function<void()> fun)
printError("Try '%1% --help' for more information.", programName); printError("Try '%1% --help' for more information.", programName);
return 1; return 1;
} catch (BaseError & e) { } catch (BaseError & e) {
logger->setShowTrace(settings.showTrace);
logError(e.info()); logError(e.info());
// TODO fix to detect non-empty trace here. // TODO fix to detect non-empty trace here.
if (e.hasTrace() && !settings.showTrace) if (e.hasTrace() && !settings.showTrace)

View file

@ -41,9 +41,10 @@ struct TunnelLogger : public Logger
Sync<State> state_; Sync<State> state_;
unsigned int clientVersion; unsigned int clientVersion;
bool showTrace;
TunnelLogger(FdSink & to, unsigned int clientVersion) TunnelLogger(FdSink & to, unsigned int clientVersion)
: to(to), clientVersion(clientVersion) { } : to(to), clientVersion(clientVersion), showTrace(false) { }
void enqueueMsg(const std::string & s) void enqueueMsg(const std::string & s)
{ {
@ -78,13 +79,21 @@ struct TunnelLogger : public Logger
if (ei.level > verbosity) return; if (ei.level > verbosity) return;
std::stringstream oss; std::stringstream oss;
oss << ei; showErrorInfo(oss, ei, false);
// oss << ei;
StringSink buf; StringSink buf;
buf << STDERR_NEXT << oss.str() << "\n"; // (fs.s + "\n"); buf << STDERR_NEXT << oss.str() << "\n"; // (fs.s + "\n");
enqueueMsg(*buf.s); enqueueMsg(*buf.s);
} }
bool getShowTrace() const override {
return showTrace;
}
void setShowTrace(bool showTrace) override {
this->showTrace = showTrace;
}
/* startWork() means that we're starting an operation for which we /* startWork() means that we're starting an operation for which we
want to send out stderr to the client. */ want to send out stderr to the client. */
void startWork() void startWork()

View file

@ -26,7 +26,8 @@ const string& BaseError::calcWhat() const
err.name = sname(); err.name = sname();
std::ostringstream oss; std::ostringstream oss;
oss << err; showErrorInfo(oss, err, false);
// oss << err;
what_ = oss.str(); what_ = oss.str();
return *what_; return *what_;

View file

@ -18,7 +18,7 @@ void setCurActivity(const ActivityId activityId)
curActivity = activityId; curActivity = activityId;
} }
Logger * logger = makeSimpleLogger(true); Logger * logger = makeSimpleLogger(true, false);
void Logger::warn(const std::string & msg) void Logger::warn(const std::string & msg)
{ {
@ -36,9 +36,10 @@ public:
bool systemd, tty; bool systemd, tty;
bool printBuildLogs; bool printBuildLogs;
bool showTrace;
SimpleLogger(bool printBuildLogs) SimpleLogger(bool printBuildLogs, bool showTrace)
: printBuildLogs(printBuildLogs) : printBuildLogs(printBuildLogs), showTrace(showTrace)
{ {
systemd = getEnv("IN_SYSTEMD") == "1"; systemd = getEnv("IN_SYSTEMD") == "1";
tty = isatty(STDERR_FILENO); tty = isatty(STDERR_FILENO);
@ -48,6 +49,13 @@ public:
return printBuildLogs; return printBuildLogs;
} }
bool getShowTrace() const override {
return showTrace;
}
void setShowTrace(bool showTrace) override {
this->showTrace = showTrace;
}
void log(Verbosity lvl, const FormatOrString & fs) override void log(Verbosity lvl, const FormatOrString & fs) override
{ {
if (lvl > verbosity) return; if (lvl > verbosity) return;
@ -72,7 +80,8 @@ public:
void logEI(const ErrorInfo & ei) override void logEI(const ErrorInfo & ei) override
{ {
std::stringstream oss; std::stringstream oss;
oss << ei; showErrorInfo(oss, ei, showTrace);
// oss << ei;
log(ei.level, oss.str()); log(ei.level, oss.str());
} }
@ -120,9 +129,9 @@ void writeToStderr(const string & s)
} }
} }
Logger * makeSimpleLogger(bool printBuildLogs) Logger * makeSimpleLogger(bool printBuildLogs, bool showTrace)
{ {
return new SimpleLogger(printBuildLogs); return new SimpleLogger(printBuildLogs, showTrace);
} }
std::atomic<uint64_t> nextId{(uint64_t) getpid() << 32}; std::atomic<uint64_t> nextId{(uint64_t) getpid() << 32};
@ -143,6 +152,13 @@ struct JSONLogger : Logger {
return true; return true;
} }
bool getShowTrace() const override {
return prevLogger.getShowTrace();
}
void setShowTrace(bool showTrace) override {
prevLogger.setShowTrace(showTrace);
}
void addFields(nlohmann::json & json, const Fields & fields) void addFields(nlohmann::json & json, const Fields & fields)
{ {
if (fields.empty()) return; if (fields.empty()) return;
@ -173,7 +189,8 @@ struct JSONLogger : Logger {
void logEI(const ErrorInfo & ei) override void logEI(const ErrorInfo & ei) override
{ {
std::ostringstream oss; std::ostringstream oss;
oss << ei; showErrorInfo(oss, ei, getShowTrace());
// oss << ei;
nlohmann::json json; nlohmann::json json;
json["action"] = "msg"; json["action"] = "msg";

View file

@ -75,6 +75,9 @@ public:
logEI(ei); logEI(ei);
} }
virtual bool getShowTrace() const = 0;
virtual void setShowTrace(bool showTrace) = 0;
virtual void warn(const std::string & msg); virtual void warn(const std::string & msg);
virtual void startActivity(ActivityId act, Verbosity lvl, ActivityType type, virtual void startActivity(ActivityId act, Verbosity lvl, ActivityType type,
@ -146,7 +149,8 @@ struct PushActivity
extern Logger * logger; extern Logger * logger;
Logger * makeSimpleLogger(bool printBuildLogs = true); Logger * makeSimpleLogger(bool printBuildLogs, bool showTrace);
// Logger * makeSimpleLogger(bool printBuildLogs = true, bool showTrace);
Logger * makeJSONLogger(Logger & prevLogger); Logger * makeJSONLogger(Logger & prevLogger);

View file

@ -251,7 +251,7 @@ namespace nix {
TEST(addTrace, showTracesWithShowTrace) { TEST(addTrace, showTracesWithShowTrace) {
SymbolTable testTable; SymbolTable testTable;
ErrorInfo::showTrace = true; // ErrorInfo::showTrace = true;
auto problem_file = testTable.create(test_file); auto problem_file = testTable.create(test_file);
auto oneliner_file = testTable.create(one_liner); auto oneliner_file = testTable.create(one_liner);
@ -275,7 +275,7 @@ namespace nix {
TEST(addTrace, hideTracesWithoutShowTrace) { TEST(addTrace, hideTracesWithoutShowTrace) {
SymbolTable testTable; SymbolTable testTable;
ErrorInfo::showTrace = false; // ErrorInfo::showTrace = false;
auto problem_file = testTable.create(test_file); auto problem_file = testTable.create(test_file);
auto oneliner_file = testTable.create(one_liner); auto oneliner_file = testTable.create(one_liner);

View file

@ -972,7 +972,7 @@ pid_t startProcess(std::function<void()> fun, const ProcessOptions & options)
{ {
auto wrapper = [&]() { auto wrapper = [&]() {
if (!options.allowVfork) if (!options.allowVfork)
logger = makeSimpleLogger(); logger = makeSimpleLogger(true, false); // TODO remove args.
try { try {
#if __linux__ #if __linux__
if (options.dieWithParent && prctl(PR_SET_PDEATHSIG, SIGKILL) == -1) if (options.dieWithParent && prctl(PR_SET_PDEATHSIG, SIGKILL) == -1)

View file

@ -212,7 +212,7 @@ std::string attrRegex = R"([A-Za-z_][A-Za-z0-9-_+]*)";
static std::regex attrPathRegex(fmt(R"(%1%(\.%1%)*)", attrRegex)); static std::regex attrPathRegex(fmt(R"(%1%(\.%1%)*)", attrRegex));
static std::vector<std::shared_ptr<Installable>> parseInstallables( static std::vector<std::shared_ptr<Installable>> parseInstallables(
SourceExprCommand & cmd, ref<Store> store, std::vector<std::string> ss, bool useDefaultInstallables) SourceExprCommand & cmd, ref<Store> store, std::vetor<std::string> ss, bool useDefaultInstallables)
{ {
std::vector<std::shared_ptr<Installable>> result; std::vector<std::shared_ptr<Installable>> result;