Skip to content

Commit b1342a2

Browse files
committedJan 10, 2022
Errata - fix issues with stream output different from BWF output.
1 parent e05eec7 commit b1342a2

File tree

3 files changed

+36
-42
lines changed

3 files changed

+36
-42
lines changed
 

‎code/include/swoc/Errata.h

-2
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,6 @@ class Errata {
185185

186186
Severity _severity{Errata::DEFAULT_SEVERITY}; ///< Severity.
187187
code_type _code{Errata::DEFAULT_CODE}; ///< Message code / ID
188-
unsigned _level{0}; ///< Nesting level.
189-
std::atomic<int> _ref_count{0}; ///< Reference count.
190188
Container _notes; ///< The message stack.
191189
swoc::MemArena _arena; ///< Annotation text storage.
192190
};

‎code/src/Errata.cc

+14-31
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Errata::note_s(std::optional<Severity> severity, std::string_view text) {
9595
Errata&
9696
Errata::note_localized(std::string_view const& text, std::optional<Severity> severity) {
9797
auto d = this->data();
98-
Annotation *n = d->_arena.make<Annotation>(text, severity, d->_level);
98+
Annotation *n = d->_arena.make<Annotation>(text, severity);
9999
d->_notes.append(n);
100100
return *this;
101101
}
@@ -107,8 +107,9 @@ Errata::alloc(size_t n) {
107107

108108
Errata&
109109
Errata::note(const self_type& that) {
110-
for (auto const& m : that) {
111-
this->note(m._text);
110+
auto d = this->data();
111+
for (auto const& annotation : that) {
112+
d->_notes.append(d->_arena.make<Annotation>(d->localize(annotation._text), annotation._severity, annotation._level + 1));
112113
}
113114
return *this;
114115
}
@@ -126,32 +127,6 @@ Errata::register_sink(Sink::Handle const& s) {
126127
Sink_List.push_back(s);
127128
}
128129

129-
std::ostream&
130-
Errata::write(std::ostream& out) const {
131-
string_view lead;
132-
133-
auto level = this->severity();
134-
if (level < Errata::SEVERITY_NAMES.size()) {
135-
out << Errata::SEVERITY_NAMES[level];
136-
} else {
137-
out << unsigned(level._raw);
138-
}
139-
140-
out << ": ";
141-
142-
if (this->code()) {
143-
out << this->code().message() << " [" << this->code().value() << "] - ";
144-
}
145-
146-
for (auto& m : *this) {
147-
out << lead << m._text << std::endl;
148-
if (0 == lead.size()) {
149-
lead = " "_sv;
150-
}
151-
}
152-
return out;
153-
}
154-
155130
BufferWriter&
156131
bwformat(BufferWriter& bw, bwf::Spec const& spec, Errata::Severity level) {
157132
if (level < Errata::SEVERITY_NAMES.size()) {
@@ -165,7 +140,7 @@ bwformat(BufferWriter& bw, bwf::Spec const& spec, Errata::Severity level) {
165140
BufferWriter&
166141
bwformat(BufferWriter& bw, bwf::Spec const&, Errata const& errata) {
167142

168-
bw.print("{} ", errata.severity());
143+
bw.print("{}: ", errata.severity());
169144

170145
if (errata.code()) {
171146
bw.print("[{0:s} {0:d}] ", errata.code());
@@ -175,13 +150,21 @@ bwformat(BufferWriter& bw, bwf::Spec const&, Errata const& errata) {
175150
if (note.text()) {
176151
bw.print("{}{}{}\n"
177152
, swoc::bwf::Pattern{int(note.level()), " "}
178-
, swoc::bwf::If(note.has_severity(), "{} ", note.severity())
153+
, swoc::bwf::If(note.has_severity(), "{}: ", note.severity())
179154
, note.text());
180155
}
181156
}
182157
return bw;
183158
}
184159

160+
std::ostream&
161+
Errata::write(std::ostream& out) const {
162+
std::string tmp;
163+
tmp.reserve(1024);
164+
bwprint(tmp, "{}", *this);
165+
return out << tmp;
166+
}
167+
185168
std::ostream&
186169
operator<<(std::ostream& os, Errata const& err) {
187170
return err.write(os);

‎unit_tests/test_Errata.cc

+22-9
Original file line numberDiff line numberDiff line change
@@ -214,18 +214,31 @@ TEST_CASE("Errata example", "[libswoc][Errata]") {
214214
Errata errata{ec, ERRATA_ERROR, R"(Failed to open file "{}")", path};
215215
w.print("{}", errata);
216216
REQUIRE(w.size() > 0);
217-
REQUIRE(w.view().starts_with("Error [enoent") == true);
217+
REQUIRE(w.view().starts_with("Error: [enoent") == true);
218218
REQUIRE(w.view().find("enoent") != swoc::TextView::npos);
219219
}
220220

221221
TEST_CASE("Errata local severity", "[libswoc][Errata]") {
222222
std::string s;
223-
Errata errata{ERRATA_ERROR, "Nominal failure"};
224-
NoteInfo(errata, "Some");
225-
errata.note(ERRATA_DIAG, "error code {}", std::error_code(EPERM, std::system_category()));
226-
swoc::bwprint(s, "{}", errata);
227-
REQUIRE(s.size() > 0);
228-
REQUIRE(std::string::npos != s.find("Error Nominal"));
229-
REQUIRE(std::string::npos != s.find("Info Some"));
230-
REQUIRE(std::string::npos != s.find("Diag error"));
223+
{
224+
Errata errata{ERRATA_ERROR, "Nominal failure"};
225+
NoteInfo(errata, "Some");
226+
errata.note(ERRATA_DIAG, "error code {}", std::error_code(EPERM, std::system_category()));
227+
swoc::bwprint(s, "{}", errata);
228+
REQUIRE(s.size() > 0);
229+
REQUIRE(std::string::npos != s.find("Error: Nominal"));
230+
REQUIRE(std::string::npos != s.find("Info: Some"));
231+
REQUIRE(std::string::npos != s.find("Diag: error"));
232+
}
233+
Errata::FILTER_SEVERITY = ERRATA_INFO; // diag is lesser serverity, shouldn't show up.
234+
{
235+
Errata errata{ERRATA_ERROR, "Nominal failure"};
236+
NoteInfo(errata, "Some");
237+
errata.note(ERRATA_DIAG, "error code {}", std::error_code(EPERM, std::system_category()));
238+
swoc::bwprint(s, "{}", errata);
239+
REQUIRE(s.size() > 0);
240+
REQUIRE(std::string::npos != s.find("Error: Nominal"));
241+
REQUIRE(std::string::npos != s.find("Info: Some"));
242+
REQUIRE(std::string::npos == s.find("Diag: error"));
243+
}
231244
}

0 commit comments

Comments
 (0)
Please sign in to comment.