diff options
author | Ben Burdette <bburdette@gmail.com> | 2020-06-29 10:20:51 -0600 |
---|---|---|
committer | Ben Burdette <bburdette@gmail.com> | 2020-06-29 10:20:51 -0600 |
commit | 8f81fae116b449ef17561c3574d01574544c798c (patch) | |
tree | e775375a0407c1cf16b727b2928335a392aab679 /src/libutil | |
parent | ef24a0835d16baf1aa3f19ffa7ba7af54e6e63e6 (diff) |
showTrace flag in loggers
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/error.cc | 3 | ||||
-rw-r--r-- | src/libutil/logging.cc | 31 | ||||
-rw-r--r-- | src/libutil/logging.hh | 6 | ||||
-rw-r--r-- | src/libutil/tests/logging.cc | 4 | ||||
-rw-r--r-- | src/libutil/util.cc | 2 |
5 files changed, 34 insertions, 12 deletions
diff --git a/src/libutil/error.cc b/src/libutil/error.cc index 0210555c5..0199ee5f8 100644 --- a/src/libutil/error.cc +++ b/src/libutil/error.cc @@ -26,7 +26,8 @@ const string& BaseError::calcWhat() const err.name = sname(); std::ostringstream oss; - oss << err; + showErrorInfo(oss, err, false); + // oss << err; what_ = oss.str(); return *what_; diff --git a/src/libutil/logging.cc b/src/libutil/logging.cc index 105fadb15..4de4c4012 100644 --- a/src/libutil/logging.cc +++ b/src/libutil/logging.cc @@ -18,7 +18,7 @@ void setCurActivity(const ActivityId activityId) curActivity = activityId; } -Logger * logger = makeSimpleLogger(true); +Logger * logger = makeSimpleLogger(true, false); void Logger::warn(const std::string & msg) { @@ -36,9 +36,10 @@ public: bool systemd, tty; bool printBuildLogs; + bool showTrace; - SimpleLogger(bool printBuildLogs) - : printBuildLogs(printBuildLogs) + SimpleLogger(bool printBuildLogs, bool showTrace) + : printBuildLogs(printBuildLogs), showTrace(showTrace) { systemd = getEnv("IN_SYSTEMD") == "1"; tty = isatty(STDERR_FILENO); @@ -48,6 +49,13 @@ public: return printBuildLogs; } + bool getShowTrace() const override { + return showTrace; + } + void setShowTrace(bool showTrace) override { + this->showTrace = showTrace; + } + void log(Verbosity lvl, const FormatOrString & fs) override { if (lvl > verbosity) return; @@ -72,7 +80,8 @@ public: void logEI(const ErrorInfo & ei) override { std::stringstream oss; - oss << ei; + showErrorInfo(oss, ei, showTrace); + // oss << ei; 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}; @@ -143,6 +152,13 @@ struct JSONLogger : Logger { 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) { if (fields.empty()) return; @@ -173,7 +189,8 @@ struct JSONLogger : Logger { void logEI(const ErrorInfo & ei) override { std::ostringstream oss; - oss << ei; + showErrorInfo(oss, ei, getShowTrace()); + // oss << ei; nlohmann::json json; json["action"] = "msg"; diff --git a/src/libutil/logging.hh b/src/libutil/logging.hh index b1583eced..3c164c670 100644 --- a/src/libutil/logging.hh +++ b/src/libutil/logging.hh @@ -75,6 +75,9 @@ public: logEI(ei); } + virtual bool getShowTrace() const = 0; + virtual void setShowTrace(bool showTrace) = 0; + virtual void warn(const std::string & msg); virtual void startActivity(ActivityId act, Verbosity lvl, ActivityType type, @@ -146,7 +149,8 @@ struct PushActivity 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); diff --git a/src/libutil/tests/logging.cc b/src/libutil/tests/logging.cc index 7531d6c7a..1bd90f009 100644 --- a/src/libutil/tests/logging.cc +++ b/src/libutil/tests/logging.cc @@ -251,7 +251,7 @@ namespace nix { TEST(addTrace, showTracesWithShowTrace) { SymbolTable testTable; - ErrorInfo::showTrace = true; + // ErrorInfo::showTrace = true; auto problem_file = testTable.create(test_file); auto oneliner_file = testTable.create(one_liner); @@ -275,7 +275,7 @@ namespace nix { TEST(addTrace, hideTracesWithoutShowTrace) { SymbolTable testTable; - ErrorInfo::showTrace = false; + // ErrorInfo::showTrace = false; auto problem_file = testTable.create(test_file); auto oneliner_file = testTable.create(one_liner); diff --git a/src/libutil/util.cc b/src/libutil/util.cc index 1268b146a..92035c3ad 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -972,7 +972,7 @@ pid_t startProcess(std::function<void()> fun, const ProcessOptions & options) { auto wrapper = [&]() { if (!options.allowVfork) - logger = makeSimpleLogger(); + logger = makeSimpleLogger(true, false); // TODO remove args. try { #if __linux__ if (options.dieWithParent && prctl(PR_SET_PDEATHSIG, SIGKILL) == -1) |