Skip to content

Commit

Permalink
Errata - fix issues with stream output different from BWF output.
Browse files Browse the repository at this point in the history
  • Loading branch information
SolidWallOfCode committed Jan 10, 2022
1 parent e05eec7 commit b1342a2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 42 deletions.
2 changes: 0 additions & 2 deletions code/include/swoc/Errata.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,6 @@ class Errata {

Severity _severity{Errata::DEFAULT_SEVERITY}; ///< Severity.
code_type _code{Errata::DEFAULT_CODE}; ///< Message code / ID
unsigned _level{0}; ///< Nesting level.
std::atomic<int> _ref_count{0}; ///< Reference count.
Container _notes; ///< The message stack.
swoc::MemArena _arena; ///< Annotation text storage.
};
Expand Down
45 changes: 14 additions & 31 deletions code/src/Errata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Errata::note_s(std::optional<Severity> severity, std::string_view text) {
Errata&
Errata::note_localized(std::string_view const& text, std::optional<Severity> severity) {
auto d = this->data();
Annotation *n = d->_arena.make<Annotation>(text, severity, d->_level);
Annotation *n = d->_arena.make<Annotation>(text, severity);
d->_notes.append(n);
return *this;
}
Expand All @@ -107,8 +107,9 @@ Errata::alloc(size_t n) {

Errata&
Errata::note(const self_type& that) {
for (auto const& m : that) {
this->note(m._text);
auto d = this->data();
for (auto const& annotation : that) {
d->_notes.append(d->_arena.make<Annotation>(d->localize(annotation._text), annotation._severity, annotation._level + 1));
}
return *this;
}
Expand All @@ -126,32 +127,6 @@ Errata::register_sink(Sink::Handle const& s) {
Sink_List.push_back(s);
}

std::ostream&
Errata::write(std::ostream& out) const {
string_view lead;

auto level = this->severity();
if (level < Errata::SEVERITY_NAMES.size()) {
out << Errata::SEVERITY_NAMES[level];
} else {
out << unsigned(level._raw);
}

out << ": ";

if (this->code()) {
out << this->code().message() << " [" << this->code().value() << "] - ";
}

for (auto& m : *this) {
out << lead << m._text << std::endl;
if (0 == lead.size()) {
lead = " "_sv;
}
}
return out;
}

BufferWriter&
bwformat(BufferWriter& bw, bwf::Spec const& spec, Errata::Severity level) {
if (level < Errata::SEVERITY_NAMES.size()) {
Expand All @@ -165,7 +140,7 @@ bwformat(BufferWriter& bw, bwf::Spec const& spec, Errata::Severity level) {
BufferWriter&
bwformat(BufferWriter& bw, bwf::Spec const&, Errata const& errata) {

bw.print("{} ", errata.severity());
bw.print("{}: ", errata.severity());

if (errata.code()) {
bw.print("[{0:s} {0:d}] ", errata.code());
Expand All @@ -175,13 +150,21 @@ bwformat(BufferWriter& bw, bwf::Spec const&, Errata const& errata) {
if (note.text()) {
bw.print("{}{}{}\n"
, swoc::bwf::Pattern{int(note.level()), " "}
, swoc::bwf::If(note.has_severity(), "{} ", note.severity())
, swoc::bwf::If(note.has_severity(), "{}: ", note.severity())
, note.text());
}
}
return bw;
}

std::ostream&
Errata::write(std::ostream& out) const {
std::string tmp;
tmp.reserve(1024);
bwprint(tmp, "{}", *this);
return out << tmp;
}

std::ostream&
operator<<(std::ostream& os, Errata const& err) {
return err.write(os);
Expand Down
31 changes: 22 additions & 9 deletions unit_tests/test_Errata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,31 @@ TEST_CASE("Errata example", "[libswoc][Errata]") {
Errata errata{ec, ERRATA_ERROR, R"(Failed to open file "{}")", path};
w.print("{}", errata);
REQUIRE(w.size() > 0);
REQUIRE(w.view().starts_with("Error [enoent") == true);
REQUIRE(w.view().starts_with("Error: [enoent") == true);
REQUIRE(w.view().find("enoent") != swoc::TextView::npos);
}

TEST_CASE("Errata local severity", "[libswoc][Errata]") {
std::string s;
Errata errata{ERRATA_ERROR, "Nominal failure"};
NoteInfo(errata, "Some");
errata.note(ERRATA_DIAG, "error code {}", std::error_code(EPERM, std::system_category()));
swoc::bwprint(s, "{}", errata);
REQUIRE(s.size() > 0);
REQUIRE(std::string::npos != s.find("Error Nominal"));
REQUIRE(std::string::npos != s.find("Info Some"));
REQUIRE(std::string::npos != s.find("Diag error"));
{
Errata errata{ERRATA_ERROR, "Nominal failure"};
NoteInfo(errata, "Some");
errata.note(ERRATA_DIAG, "error code {}", std::error_code(EPERM, std::system_category()));
swoc::bwprint(s, "{}", errata);
REQUIRE(s.size() > 0);
REQUIRE(std::string::npos != s.find("Error: Nominal"));
REQUIRE(std::string::npos != s.find("Info: Some"));
REQUIRE(std::string::npos != s.find("Diag: error"));
}
Errata::FILTER_SEVERITY = ERRATA_INFO; // diag is lesser serverity, shouldn't show up.
{
Errata errata{ERRATA_ERROR, "Nominal failure"};
NoteInfo(errata, "Some");
errata.note(ERRATA_DIAG, "error code {}", std::error_code(EPERM, std::system_category()));
swoc::bwprint(s, "{}", errata);
REQUIRE(s.size() > 0);
REQUIRE(std::string::npos != s.find("Error: Nominal"));
REQUIRE(std::string::npos != s.find("Info: Some"));
REQUIRE(std::string::npos == s.find("Diag: error"));
}
}

0 comments on commit b1342a2

Please sign in to comment.