demoing other error levels than warn/error; rename line and file fields in errPos

This commit is contained in:
Ben Burdette 2020-04-17 15:50:46 -06:00
parent 3d5b1032a1
commit 4697552948
4 changed files with 103 additions and 31 deletions

View file

@ -8,13 +8,46 @@ int main()
{ {
using namespace nix; using namespace nix;
std::unique_ptr<Logger> logger(makeDefaultLogger()); makeDefaultLogger();
verbosity = lvlError; verbosity = lvlVomit;
// In each program where errors occur, this has to be set. // In each program where errors occur, this has to be set.
ErrorInfo::programName = std::optional("error-demo"); 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. // Error in a program; no hint and no nix code.
logError( logError(
ErrorInfo { .name = "name", ErrorInfo { .name = "name",

View file

@ -17,9 +17,9 @@ std::ostream& operator<<(std::ostream &os, const hintformat &hf)
string showErrPos(const ErrPos &errPos) string showErrPos(const ErrPos &errPos)
{ {
if (errPos.column > 0) { if (errPos.column > 0) {
return fmt("(%1%:%2%)", errPos.lineNumber, errPos.column); return fmt("(%1%:%2%)", errPos.line, errPos.column);
} else { } 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()) { if (nixCode.prevLineOfCode.has_value()) {
std::cout << fmt("%1% %|2$5d|| %3%", std::cout << fmt("%1% %|2$5d|| %3%",
prefix, prefix,
(nixCode.errPos.lineNumber - 1), (nixCode.errPos.line - 1),
*nixCode.prevLineOfCode) *nixCode.prevLineOfCode)
<< std::endl; << std::endl;
} }
@ -37,7 +37,7 @@ void printCodeLines(const string &prefix, const NixCode &nixCode)
// line of code containing the error.%2$+5d% // line of code containing the error.%2$+5d%
std::cout << fmt("%1% %|2$5d|| %3%", std::cout << fmt("%1% %|2$5d|| %3%",
prefix, prefix,
(nixCode.errPos.lineNumber), (nixCode.errPos.line),
nixCode.errLineOfCode) nixCode.errLineOfCode)
<< std::endl; << std::endl;
@ -61,7 +61,7 @@ void printCodeLines(const string &prefix, const NixCode &nixCode)
if (nixCode.nextLineOfCode.has_value()) { if (nixCode.nextLineOfCode.has_value()) {
std::cout << fmt("%1% %|2$5d|| %3%", std::cout << fmt("%1% %|2$5d|| %3%",
prefix, prefix,
(nixCode.errPos.lineNumber + 1), (nixCode.errPos.line + 1),
*nixCode.nextLineOfCode) *nixCode.nextLineOfCode)
<< std::endl; << std::endl;
} }
@ -87,16 +87,26 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo)
break; break;
} }
case Verbosity::lvlInfo: { case Verbosity::lvlInfo: {
levelString = ANSI_YELLOW; levelString = ANSI_GREEN;
levelString += "info:"; levelString += "info:";
levelString += ANSI_NORMAL; levelString += ANSI_NORMAL;
break; break;
} }
case Verbosity::lvlTalkative: case Verbosity::lvlTalkative: {
case Verbosity::lvlChatty: levelString = ANSI_GREEN;
levelString += "talk:";
levelString += ANSI_NORMAL;
break;
}
case Verbosity::lvlChatty: {
levelString = ANSI_GREEN;
levelString += "chat:";
levelString += ANSI_NORMAL;
break;
}
case Verbosity::lvlVomit: { case Verbosity::lvlVomit: {
levelString = ANSI_GREEN; levelString = ANSI_GREEN;
levelString += "info:"; levelString += "vomit:";
levelString += ANSI_NORMAL; levelString += ANSI_NORMAL;
break; break;
} }
@ -121,25 +131,25 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo)
// divider. // divider.
out << fmt("%1%%2%" ANSI_BLUE " %3% %4% %5% %6%" ANSI_NORMAL, out << fmt("%1%%2%" ANSI_BLUE " %3% %4% %5% %6%" ANSI_NORMAL,
prefix, prefix,
levelString, levelString,
"---", "---",
einfo.name, einfo.name,
dashes, dashes,
einfo.programName.value_or("")) einfo.programName.value_or(""))
<< std::endl; << std::endl;
// filename. // filename.
if (einfo.nixCode.has_value()) { if (einfo.nixCode.has_value()) {
if (einfo.nixCode->errPos.nixFile != "") { if (einfo.nixCode->errPos.file != "") {
string eline = einfo.nixCode->errLineOfCode != "" string eline = einfo.nixCode->errLineOfCode != ""
? string(" ") + showErrPos(einfo.nixCode->errPos) ? string(" ") + showErrPos(einfo.nixCode->errPos)
: ""; : "";
out << fmt("%1%in file: " ANSI_BLUE "%2%%3%" ANSI_NORMAL, out << fmt("%1%in file: " ANSI_BLUE "%2%%3%" ANSI_NORMAL,
prefix, prefix,
einfo.nixCode->errPos.nixFile, einfo.nixCode->errPos.file,
eline) << std::endl; eline) << std::endl;
out << prefix << std::endl; out << prefix << std::endl;
} else { } else {
out << fmt("%1%from command line argument", prefix) << std::endl; out << fmt("%1%from command line argument", prefix) << std::endl;

View file

@ -22,16 +22,16 @@ typedef enum {
struct ErrPos struct ErrPos
{ {
int lineNumber; int line;
int column; int column;
string nixFile; string file;
template <class P> template <class P>
ErrPos& operator=(const P &pos) ErrPos& operator=(const P &pos)
{ {
lineNumber = pos.line; line = pos.line;
column = pos.column; column = pos.column;
nixFile = pos.file; file = pos.file;
return *this; return *this;
} }

View file

@ -146,11 +146,40 @@ struct JSONLogger : Logger
void logEI(const ErrorInfo & ei) override void logEI(const ErrorInfo & ei) override
{ {
// nlohmann::json json; // add fields like Pos info and etc?
// json["action"] = "msg"; std::ostringstream oss;
// json["level"] = lvl; oss << ei;
// json["msg"] = fs.s;
// write(json); 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, void startActivity(ActivityId act, Verbosity lvl, ActivityType type,