You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by bn...@apache.org on 2024/01/30 16:24:03 UTC
(trafficserver-libswoc) 02/04: Errata: Add default error values / names, clean up examples.
This is an automated email from the ASF dual-hosted git repository.
bneradt pushed a commit to branch errata-consolidation
in repository https://gitbox.apache.org/repos/asf/trafficserver-libswoc.git
commit 2a8ecfa21d1131036df1d1b013732e264000b108
Author: Alan M. Carroll <am...@apache.org>
AuthorDate: Fri Sep 17 16:06:28 2021 -0500
Errata: Add default error values / names, clean up examples.
---
code/include/swoc/Errata.h | 4 ++--
code/src/Errata.cc | 30 ++++++++++++++++++++++++------
doc/appendix.en.rst | 42 ++++++++++++++++++++++++++++++++++++++++++
example/CMakeLists.txt | 2 +-
example/ex_flat_space.cc | 11 ++++++-----
unit_tests/unit_test_main.cc | 2 +-
6 files changed, 76 insertions(+), 15 deletions(-)
diff --git a/code/include/swoc/Errata.h b/code/include/swoc/Errata.h
index c7f7fa2..ad416dc 100644
--- a/code/include/swoc/Errata.h
+++ b/code/include/swoc/Errata.h
@@ -77,7 +77,7 @@ public:
/// Mapping of severity to string.
/// Values larger than the span size will be rendered as numbers.
/// Defaults to an empty span, meaning all severities will be printed as integers.
- static MemSpan<TextView> SEVERITY_NAME;
+ static MemSpan<TextView> SEVERITY_NAMES;
/** An annotation to the Errata consisting of a severity and informative text.
*
@@ -696,7 +696,7 @@ template <typename... Args> Errata::Errata(code_type const& code, Severity sever
this->note_v(fmt, std::forward_as_tuple(args...));
}
-template <typename... Args> Errata::Errata(code_type const& type, std::string_view fmt, Args &&... args) : Errata(code, DEFAULT_SEVERITY, fmt, std::forward<Args>(args)...) { }
+template <typename... Args> Errata::Errata(code_type const& code, std::string_view fmt, Args &&... args) : Errata(code, DEFAULT_SEVERITY, fmt, std::forward<Args>(args)...) { }
template <typename... Args> Errata::Errata(Severity severity, std::string_view fmt, Args &&... args) : Errata(DEFAULT_CODE, severity, fmt, std::forward<Args>(args)...) { }
template <typename... Args> Errata::Errata(std::string_view fmt, Args &&... args) : Errata(DEFAULT_CODE, DEFAULT_SEVERITY, fmt, std::forward<Args>(args)...) { }
diff --git a/code/src/Errata.cc b/code/src/Errata.cc
index 433c982..b5c7f48 100644
--- a/code/src/Errata.cc
+++ b/code/src/Errata.cc
@@ -41,9 +41,14 @@ Errata::Data::localize(string_view src) {
/* ----------------------------------------------------------------------- */
// methods for Errata
-Errata::Severity Errata::DEFAULT_SEVERITY(1);
-Errata::Severity Errata::FAILURE_SEVERITY(1);
-swoc::MemSpan<TextView> Errata::SEVERITY_NAME;
+Errata::Severity Errata::DEFAULT_SEVERITY(2);
+Errata::Severity Errata::FAILURE_SEVERITY(2);
+// Provide a somewhat reasonable set of default severities and names
+std::array<swoc::TextView, 4> Severity_Names { {
+ "Info", "Warn", "Error"
+}};
+
+swoc::MemSpan<TextView> Errata::SEVERITY_NAMES { Severity_Names.data(), Severity_Names.size() };
Errata::~Errata() {
if (_data) {
@@ -108,6 +113,20 @@ Errata::register_sink(Sink::Handle const& 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()) {
@@ -119,8 +138,8 @@ Errata::write(std::ostream& out) const {
BufferWriter&
bwformat(BufferWriter& bw, bwf::Spec const& spec, Errata::Severity level) {
- if (level < Errata::SEVERITY_NAME.size()) {
- bwformat(bw, spec, Errata::SEVERITY_NAME[level]);
+ if (level < Errata::SEVERITY_NAMES.size()) {
+ bwformat(bw, spec, Errata::SEVERITY_NAMES[level]);
} else {
bwformat(bw, spec, level._raw);
}
@@ -136,7 +155,6 @@ bwformat(BufferWriter& bw, bwf::Spec const&, Errata const& errata) {
bw.print("[{0:s} {0:d}] ", errata.code());
}
-
for (auto& m : errata) {
bw.print("{}{}\n", swoc::bwf::Pattern{int(m.level()), " "}, m.text());
}
diff --git a/doc/appendix.en.rst b/doc/appendix.en.rst
new file mode 100644
index 0000000..23dc621
--- /dev/null
+++ b/doc/appendix.en.rst
@@ -0,0 +1,42 @@
+.. Licensed to the Apache Software Foundation (ASF) under one or more contributor license
+ agreements. See the NOTICE file distributed with this work for
+ additional information regarding copyright ownership. The ASF licenses this file to you under the
+ Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software distributed under the License
+ is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ or implied. See the License for the specific language governing permissions and limitations under
+ the License.
+
+.. include:: common-defs.rst
+
+.. _appendix:
+
+Appendix
+********
+
+ATS Correspondence
+==================
+
+This library began as a set of classes in `Apache Traffic Server<https://trafficserver.apache.org>`__.
+It has continued to be driven primarily by usefulness in writing ATS core and plugin code. For that
+reason it should be incorporated back in to ATS as a replacement for existing classes.
+
+================ =================== =======================================
+libswoc Traffic Server
+================ =================== =======================================
+BufferWriter BufferWriter Significant upgrades
+Errata Errata Significant upgrades
+IntrusiveDList IntrusiveDList Almost identical
+IntrusiveHashMap IntrusiveHashMap Almost identical
+IpAddr IpAddr Significant changes
+IPSpace IpMap Massive upgrade
+MemSpan MemSpan Almost identical
+MemArena Arena Complete rewrite
+Scalar Scalar Almost identical
+TextView TextView Significant upgrades
+Lexicon *
+================ =================== =======================================
diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt
index 8bae50b..8d072e0 100644
--- a/example/CMakeLists.txt
+++ b/example/CMakeLists.txt
@@ -19,5 +19,5 @@ endif()
add_executable(ex_flat_space ex_flat_space.cc)
target_link_libraries(ex_flat_space PUBLIC libswoc)
if (CMAKE_COMPILER_IS_GNUCXX)
- target_compile_options(ex_netcompact PRIVATE -Wall -Wextra -Werror)
+ target_compile_options(ex_flat_space PRIVATE -Wall -Wextra -Werror)
endif()
diff --git a/example/ex_flat_space.cc b/example/ex_flat_space.cc
index 638301e..2192cd8 100644
--- a/example/ex_flat_space.cc
+++ b/example/ex_flat_space.cc
@@ -47,6 +47,8 @@ using swoc::IPSpace;
// Temp for error messages.
std::string err_text;
+std::error_code errno_code() { return std::error_code(errno, std::system_category()); }
+
/** Allocate a span of type @a T.
*
* @tparam T Element type.
@@ -138,13 +140,12 @@ template <typename METRIC, typename PAYLOAD> Errata IPArray<METRIC, PAYLOAD>::st
auto fd = ::open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
if (fd >= 0) {
auto written = ::write(fd, _nodes.data(), _nodes.size());
- if (written != _nodes.size()) {
- return Errata().note("Failed to write IP4 output - {} of {} bytes written to '{}' [{}]\n", written, _nodes.size(), path,
- swoc::bwf::Errno{});
+ if (written != ssize_t(_nodes.size())) {
+ return Errata(errno_code(), "Failed to write IP4 output - {} of {} bytes written to '{}'\n", written, _nodes.size(), path);
}
close(fd);
} else {
- return Errata().note("Failed to open IP4 output '{}' [{}]\n", path, swoc::bwf::Errno{});
+ return Errata(errno_code(), "Failed to open IP4 output '{}'\n", path);
}
return {};
}
@@ -169,7 +170,7 @@ void build(IPSpace<unsigned> & space, swoc::file::path src) {
}
int main(int argc, char const *argv[]) {
- swoc::file::path path_4{"/tmp/ip4.mem"};
+ swoc::file::path path_4{"/opt/ip4.mem"};
swoc::file::path path_6{"/tmp/ip6.mem"};
swoc::file::path src;
diff --git a/unit_tests/unit_test_main.cc b/unit_tests/unit_test_main.cc
index 5f87462..e82bc96 100644
--- a/unit_tests/unit_test_main.cc
+++ b/unit_tests/unit_test_main.cc
@@ -40,7 +40,7 @@ main(int argc, char *argv[])
swoc::Errata::DEFAULT_SEVERITY = ERRATA_ERROR;
swoc::Errata::FAILURE_SEVERITY = ERRATA_WARN;
- swoc::Errata::SEVERITY_NAME = swoc::MemSpan<swoc::TextView>(Severity_Names.data(), Severity_Names.size());
+ swoc::Errata::SEVERITY_NAMES = swoc::MemSpan<swoc::TextView>(Severity_Names.data(), Severity_Names.size());
int result = Catch::Session().run(argc, argv);