Skip to content

Commit

Permalink
fix severity colorization
Browse files Browse the repository at this point in the history
esp. information as red was disturbing.
  • Loading branch information
rurban committed Jul 18, 2024
1 parent 8ec2a65 commit 8df610d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1394,7 +1394,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a

// Default template format..
if (mSettings.templateFormat.empty()) {
mSettings.templateFormat = "{bold}{file}:{line}:{column}: {red}{inconclusive:{magenta}}{severity}:{inconclusive: inconclusive:}{default} {message} [{id}]{reset}\\n{code}";
mSettings.templateFormat = "{bold}{file}:{line}:{column}: {inconclusive:}{severity}:{inconclusive: inconclusive:}{default} {message} [{id}]{reset}\\n{code}";
if (mSettings.templateLocation.empty())
mSettings.templateLocation = "{bold}{file}:{line}:{column}: {dim}note:{reset} {info}\\n{code}";
}
Expand Down
4 changes: 3 additions & 1 deletion lib/errorlogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,9 @@ std::string ErrorMessage::toString(bool verbose, const std::string &templateForm
findAndReplace(result, replaceFrom, replaceWith);
pos1 = result.find("{inconclusive:", pos1);
}
findAndReplace(result, "{severity}", severityToString(severity));
std::string sseverity = coloredSeverityToString(severity);
replaceColors(sseverity);
findAndReplace(result, "{severity}", sseverity);
findAndReplace(result, "{cwe}", std::to_string(cwe.id));
findAndReplace(result, "{message}", verbose ? mVerboseMessage : mShortMessage);
findAndReplace(result, "{remark}", remark);
Expand Down
25 changes: 25 additions & 0 deletions lib/errortypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,31 @@ std::string severityToString(Severity severity)
throw InternalError(nullptr, "Unknown severity");
}

std::string coloredSeverityToString(Severity severity)
{
switch (severity) {
case Severity::none:
return "";
case Severity::error:
return "{red}error";
case Severity::warning:
return "{magenta}warning";
case Severity::style:
return "{bold}style";
case Severity::performance:
return "{bold}performance";
case Severity::portability:
return "{bold}portability";
case Severity::information:
return "{green}information";
case Severity::debug:
return "debug";
case Severity::internal:
return "internal";
}
throw InternalError(nullptr, "Unknown severity");
}

// TODO: bail out on invalid severity
Severity severityFromString(const std::string& severity)
{
Expand Down
1 change: 1 addition & 0 deletions lib/errortypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ enum class Severity : std::uint8_t {
};

CPPCHECKLIB std::string severityToString(Severity severity);
CPPCHECKLIB std::string coloredSeverityToString(Severity severity);
CPPCHECKLIB Severity severityFromString(const std::string &severity);

struct CWE {
Expand Down
29 changes: 29 additions & 0 deletions test/testerrorlogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class TestErrorLogger : public TestFixture {
TEST_CASE(ErrorMessageVerbose);
TEST_CASE(ErrorMessageVerboseLocations);
TEST_CASE(ErrorMessageFromInternalError);
TEST_CASE(ErrorMessageColorized);
TEST_CASE(CustomFormat);
TEST_CASE(CustomFormat2);
TEST_CASE(CustomFormatLocations);
Expand Down Expand Up @@ -193,6 +194,34 @@ class TestErrorLogger : public TestFixture {
}
}

void ErrorMessageColorized() const {
const bool oDisableColors = gDisableColors;
gDisableColors = false;
setenv("CLICOLOR_FORCE", "1", 1);
std::list<ErrorMessage::FileLocation> locs = { };
{
ErrorMessage msg(std::move(locs), emptyString, Severity::error, "Programming error.\nVerbose error", "errorId",
Certainty::normal);
ASSERT_EQUALS("{bold} \x1b[31merror: Programming error.", msg.toString(false, "{bold} {severity}: {message}"));
}
{
ErrorMessage msg(std::move(locs), emptyString, Severity::warning, "Programming warning.\nVerbose warning", "errorId",
Certainty::normal);
ASSERT_EQUALS("{bold} \x1b[35mwarning: Programming warning.", msg.toString(false, "{bold} {severity}: {message}"));
}
{
ErrorMessage msg(std::move(locs), emptyString, Severity::style, "Style.\nVerbose style", "errorId", Certainty::normal);
ASSERT_EQUALS("{bold} \x1b[1mstyle: Style.", msg.toString(false, "{bold} {severity}: {message}"));
}
{
ErrorMessage msg(std::move(locs), emptyString, Severity::information, "Programming information.\nProgramming information",
"errorId", Certainty::normal);
ASSERT_EQUALS("{bold} \x1b[32minformation: Programming information.", msg.toString(false, "{bold} {severity}: {message}"));
}
setenv("CLICOLOR_FORCE", "", 1);
gDisableColors = oDisableColors;
}

void CustomFormat() const {
std::list<ErrorMessage::FileLocation> locs(1, fooCpp5);
ErrorMessage msg(std::move(locs), emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", Certainty::normal);
Expand Down

0 comments on commit 8df610d

Please sign in to comment.