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;
std::unique_ptr<Logger> 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",

View file

@ -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;
}
@ -131,14 +141,14 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo)
// 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,
einfo.nixCode->errPos.file,
eline) << std::endl;
out << prefix << std::endl;
} else {

View file

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

View file

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