From 4697552948b4d9b958b0e8f8ff1b68b5e9608e4e Mon Sep 17 00:00:00 2001 From: Ben Burdette Date: Fri, 17 Apr 2020 15:50:46 -0600 Subject: [PATCH] demoing other error levels than warn/error; rename line and file fields in errPos --- src/error-demo/error-demo.cc | 37 ++++++++++++++++++++++++-- src/libutil/error.cc | 50 +++++++++++++++++++++--------------- src/libutil/error.hh | 8 +++--- src/libutil/logging.cc | 39 ++++++++++++++++++++++++---- 4 files changed, 103 insertions(+), 31 deletions(-) diff --git a/src/error-demo/error-demo.cc b/src/error-demo/error-demo.cc index b7dd4cc22..14027278d 100644 --- a/src/error-demo/error-demo.cc +++ b/src/error-demo/error-demo.cc @@ -8,13 +8,46 @@ int main() { using namespace nix; - std::unique_ptr logger(makeDefaultLogger()); + makeDefaultLogger(); - verbosity = lvlError; + verbosity = lvlVomit; // In each program where errors occur, this has to be set. ErrorInfo::programName = std::optional("error-demo"); + // For completeness sake, info through vomit levels. + // But this is maybe a heavy format for those. + logger->logEI( + ErrorInfo { .level = lvlInfo, + .name = "Info name", + .description = "Info description", + }); + + logger->logEI( + ErrorInfo { .level = lvlTalkative, + .name = "Talkative name", + .description = "Talkative description", + }); + + logger->logEI( + ErrorInfo { .level = lvlChatty, + .name = "Chatty name", + .description = "Chatty description", + }); + + logger->logEI( + ErrorInfo { .level = lvlDebug, + .name = "Debug name", + .description = "Debug description", + }); + + logger->logEI( + ErrorInfo { .level = lvlVomit, + .name = "Vomit name", + .description = "Vomit description", + }); + + // Error in a program; no hint and no nix code. logError( ErrorInfo { .name = "name", diff --git a/src/libutil/error.cc b/src/libutil/error.cc index 779f519c0..0e07c0d98 100644 --- a/src/libutil/error.cc +++ b/src/libutil/error.cc @@ -17,9 +17,9 @@ std::ostream& operator<<(std::ostream &os, const hintformat &hf) string showErrPos(const ErrPos &errPos) { if (errPos.column > 0) { - return fmt("(%1%:%2%)", errPos.lineNumber, errPos.column); + return fmt("(%1%:%2%)", errPos.line, errPos.column); } else { - return fmt("(%1%)", errPos.lineNumber); + return fmt("(%1%)", errPos.line); }; } @@ -29,7 +29,7 @@ void printCodeLines(const string &prefix, const NixCode &nixCode) if (nixCode.prevLineOfCode.has_value()) { std::cout << fmt("%1% %|2$5d|| %3%", prefix, - (nixCode.errPos.lineNumber - 1), + (nixCode.errPos.line - 1), *nixCode.prevLineOfCode) << std::endl; } @@ -37,7 +37,7 @@ void printCodeLines(const string &prefix, const NixCode &nixCode) // line of code containing the error.%2$+5d% std::cout << fmt("%1% %|2$5d|| %3%", prefix, - (nixCode.errPos.lineNumber), + (nixCode.errPos.line), nixCode.errLineOfCode) << std::endl; @@ -61,7 +61,7 @@ void printCodeLines(const string &prefix, const NixCode &nixCode) if (nixCode.nextLineOfCode.has_value()) { std::cout << fmt("%1% %|2$5d|| %3%", prefix, - (nixCode.errPos.lineNumber + 1), + (nixCode.errPos.line + 1), *nixCode.nextLineOfCode) << std::endl; } @@ -87,16 +87,26 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo) break; } case Verbosity::lvlInfo: { - levelString = ANSI_YELLOW; + levelString = ANSI_GREEN; levelString += "info:"; levelString += ANSI_NORMAL; break; } - case Verbosity::lvlTalkative: - case Verbosity::lvlChatty: + case Verbosity::lvlTalkative: { + levelString = ANSI_GREEN; + levelString += "talk:"; + levelString += ANSI_NORMAL; + break; + } + case Verbosity::lvlChatty: { + levelString = ANSI_GREEN; + levelString += "chat:"; + levelString += ANSI_NORMAL; + break; + } case Verbosity::lvlVomit: { levelString = ANSI_GREEN; - levelString += "info:"; + levelString += "vomit:"; levelString += ANSI_NORMAL; break; } @@ -121,25 +131,25 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo) // divider. out << fmt("%1%%2%" ANSI_BLUE " %3% %4% %5% %6%" ANSI_NORMAL, - prefix, - levelString, - "---", - einfo.name, - dashes, - einfo.programName.value_or("")) - << std::endl; + prefix, + levelString, + "---", + einfo.name, + dashes, + einfo.programName.value_or("")) + << std::endl; // filename. if (einfo.nixCode.has_value()) { - if (einfo.nixCode->errPos.nixFile != "") { + if (einfo.nixCode->errPos.file != "") { string eline = einfo.nixCode->errLineOfCode != "" ? string(" ") + showErrPos(einfo.nixCode->errPos) : ""; out << fmt("%1%in file: " ANSI_BLUE "%2%%3%" ANSI_NORMAL, - prefix, - einfo.nixCode->errPos.nixFile, - eline) << std::endl; + prefix, + einfo.nixCode->errPos.file, + eline) << std::endl; out << prefix << std::endl; } else { out << fmt("%1%from command line argument", prefix) << std::endl; diff --git a/src/libutil/error.hh b/src/libutil/error.hh index 0419a1b52..63778ebda 100644 --- a/src/libutil/error.hh +++ b/src/libutil/error.hh @@ -22,16 +22,16 @@ typedef enum { struct ErrPos { - int lineNumber; + int line; int column; - string nixFile; + string file; template ErrPos& operator=(const P &pos) { - lineNumber = pos.line; + line = pos.line; column = pos.column; - nixFile = pos.file; + file = pos.file; return *this; } diff --git a/src/libutil/logging.cc b/src/libutil/logging.cc index 98f500a65..4a8b98640 100644 --- a/src/libutil/logging.cc +++ b/src/libutil/logging.cc @@ -146,11 +146,40 @@ struct JSONLogger : Logger void logEI(const ErrorInfo & ei) override { - // nlohmann::json json; - // json["action"] = "msg"; - // json["level"] = lvl; - // json["msg"] = fs.s; - // write(json); + // add fields like Pos info and etc? + std::ostringstream oss; + oss << ei; + + nlohmann::json json; + json["action"] = "msg"; + json["level"] = ei.level; + json["msg"] = oss.str(); + + // Extra things that COULD go into json. Useful? + // TODO: decide if useful. + // TODO: make a json obj that goes into json["msg"]? + json["name"] = ei.name; + json["description"] = ei.description; + if (ei.hint.has_value()) + { + json["hint"] = ei.hint->str(); + } + if (ei.nixCode.has_value()) + { + if (ei.nixCode->errPos.line != 0) + json["line"] = ei.nixCode->errPos.line; + if (ei.nixCode->errPos.column != 0) + json["column"] = ei.nixCode->errPos.column; + if (ei.nixCode->errPos.file != "") + json["file"] = ei.nixCode->errPos.file; + if (ei.nixCode->prevLineOfCode.has_value()) + json["prevLineOfCode"] = *ei.nixCode->prevLineOfCode; + json["errLineOfCode"] = ei.nixCode->errLineOfCode; + if (ei.nixCode->nextLineOfCode.has_value()) + json["nextLineOfCode"] = *ei.nixCode->nextLineOfCode; + } + + write(json); } void startActivity(ActivityId act, Verbosity lvl, ActivityType type,