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() {
switch (defaultLogFormat) {
case LogFormat::raw:
return makeSimpleLogger(false);
return makeSimpleLogger(false, false);
case LogFormat::rawWithLogs:
return makeSimpleLogger(true);
return makeSimpleLogger(true, false);
case LogFormat::internalJson:
return makeJSONLogger(*makeSimpleLogger());
return makeJSONLogger(*makeSimpleLogger(true, false));
case LogFormat::bar:
return makeProgressBar();
case LogFormat::barWithLogs:

View file

@ -81,12 +81,14 @@ private:
bool printBuildLogs;
bool isTTY;
bool showTrace;
public:
ProgressBar(bool printBuildLogs, bool isTTY)
: printBuildLogs(printBuildLogs)
, isTTY(isTTY)
, showTrace(false)
{
state_.lock()->active = isTTY;
updateThread = std::thread([&]() {
@ -131,10 +133,17 @@ public:
auto state(state_.lock());
std::stringstream oss;
oss << ei;
showErrorInfo(oss, ei, showTrace);
// oss << ei;
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)
{

View file

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

View file

@ -41,9 +41,10 @@ struct TunnelLogger : public Logger
Sync<State> state_;
unsigned int clientVersion;
bool showTrace;
TunnelLogger(FdSink & to, unsigned int clientVersion)
: to(to), clientVersion(clientVersion) { }
: to(to), clientVersion(clientVersion), showTrace(false) { }
void enqueueMsg(const std::string & s)
{
@ -78,13 +79,21 @@ struct TunnelLogger : public Logger
if (ei.level > verbosity) return;
std::stringstream oss;
oss << ei;
showErrorInfo(oss, ei, false);
// oss << ei;
StringSink buf;
buf << STDERR_NEXT << oss.str() << "\n"; // (fs.s + "\n");
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
want to send out stderr to the client. */
void startWork()

View file

@ -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_;

View file

@ -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";

View file

@ -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);

View file

@ -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);

View file

@ -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)

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::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;