You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by am...@apache.org on 2018/08/06 20:27:04 UTC
[trafficserver] branch master updated: BWF: Add BWF support for
SourceLocation. Update test_History to validate the formatting support.
This is an automated email from the ASF dual-hosted git repository.
amc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
The following commit(s) were added to refs/heads/master by this push:
new 3dd8750 BWF: Add BWF support for SourceLocation. Update test_History to validate the formatting support.
3dd8750 is described below
commit 3dd8750eaad370108d01d61585a64f146f3a065a
Author: Alan M. Carroll <am...@apache.org>
AuthorDate: Fri Jul 13 20:47:47 2018 -0500
BWF: Add BWF support for SourceLocation.
Update test_History to validate the formatting support.
---
lib/ts/SourceLocation.cc | 16 ++++++++++++++--
lib/ts/SourceLocation.h | 12 ++++++++++++
lib/ts/unit-tests/test_History.cc | 22 ++++++++++++----------
3 files changed, 38 insertions(+), 12 deletions(-)
diff --git a/lib/ts/SourceLocation.cc b/lib/ts/SourceLocation.cc
index b2ac79d..daac873 100644
--- a/lib/ts/SourceLocation.cc
+++ b/lib/ts/SourceLocation.cc
@@ -21,10 +21,12 @@
* limitations under the License.
*/
-#include "SourceLocation.h"
-#include "ink_defs.h"
#include <cstdio>
#include <cstring>
+#include "SourceLocation.h"
+#include "ink_defs.h"
+#include "ts/BufferWriter.h"
+#include "ts/bwf_std_format.h"
// This method takes a SourceLocation source location data structure and
// converts it to a human-readable representation, in the buffer <buf>
@@ -53,3 +55,13 @@ SourceLocation::str(char *buf, int buflen) const
buf[buflen - 1] = NUL;
return (buf);
}
+
+ts::BufferWriter &
+SourceLocation::print(ts::BufferWriter &w, ts::BWFSpec const &) const
+{
+ if (this->valid()) {
+ ts::TextView base{ts::TextView{file, strlen(file)}.take_suffix_at('/')};
+ w.print("{}:{}{}", base, line, ts::bwf::OptionalAffix(func, ")"_sv, " ("_sv));
+ };
+ return w;
+}
diff --git a/lib/ts/SourceLocation.h b/lib/ts/SourceLocation.h
index 8c2ccac..53bc386 100644
--- a/lib/ts/SourceLocation.h
+++ b/lib/ts/SourceLocation.h
@@ -23,6 +23,8 @@
#pragma once
+#include "ts/BufferWriterForward.h"
+
// The SourceLocation class wraps up a source code location, including
// file name, function name, and line number, and contains a method to
// format the result into a string buffer.
@@ -57,4 +59,14 @@ public:
}
char *str(char *buf, int buflen) const;
+ ts::BufferWriter &print(ts::BufferWriter &w, ts::BWFSpec const &spec) const;
};
+
+namespace ts
+{
+inline BufferWriter &
+bwformat(BufferWriter &w, BWFSpec const &spec, SourceLocation const &loc)
+{
+ return loc.print(w, spec);
+}
+} // namespace ts
diff --git a/lib/ts/unit-tests/test_History.cc b/lib/ts/unit-tests/test_History.cc
index e402ea8..b206960 100644
--- a/lib/ts/unit-tests/test_History.cc
+++ b/lib/ts/unit-tests/test_History.cc
@@ -24,6 +24,7 @@
#include <string_view>
#include "ts/History.h"
+#include "BufferWriter.h"
#include "catch.hpp"
using std::string_view;
@@ -58,21 +59,22 @@ TEST_CASE("History", "[libts][History]")
REQUIRE(history[2].reentrancy == static_cast<short>(NO_REENTRANT));
history[0].location.str(buf, sizeof(buf));
- REQUIRE(string_view{buf} == "test_History.cc:47 (____C_A_T_C_H____T_E_S_T____0)");
+ REQUIRE(string_view{buf} == "test_History.cc:48 (____C_A_T_C_H____T_E_S_T____0)");
history[1].location.str(buf, sizeof(buf));
- REQUIRE(string_view{buf} == "test_History.cc:48 (____C_A_T_C_H____T_E_S_T____0)");
+ REQUIRE(string_view{buf} == "test_History.cc:49 (____C_A_T_C_H____T_E_S_T____0)");
+ ts::LocalBufferWriter<128> w;
SM<HISTORY_DEFAULT_SIZE> *sm = new SM<HISTORY_DEFAULT_SIZE>;
SM_REMEMBER(sm, 1, 1);
SM_REMEMBER(sm, 2, 2);
SM_REMEMBER(sm, 3, NO_REENTRANT);
- sm->history[0].location.str(buf, sizeof(buf));
- REQUIRE(string_view{buf} == "test_History.cc:67 (____C_A_T_C_H____T_E_S_T____0)");
+ w.print("{}", sm->history[0].location);
+ REQUIRE(w.view() == "test_History.cc:69 (____C_A_T_C_H____T_E_S_T____0)");
- sm->history[1].location.str(buf, sizeof(buf));
- REQUIRE(string_view{buf} == "test_History.cc:68 (____C_A_T_C_H____T_E_S_T____0)");
+ w.reset().print("{}", sm->history[1].location);
+ REQUIRE(w.view() == "test_History.cc:70 (____C_A_T_C_H____T_E_S_T____0)");
REQUIRE(sm->history[0].event == 1);
REQUIRE(sm->history[0].reentrancy == 1);
@@ -103,11 +105,11 @@ TEST_CASE("History", "[libts][History]")
REQUIRE(sm2->history.size() == 2);
REQUIRE(sm2->history.overflowed() == true);
- sm2->history[0].location.str(buf, sizeof(buf));
- REQUIRE(string_view{buf} == "test_History.cc:101 (____C_A_T_C_H____T_E_S_T____0)");
+ w.reset().print("{}", sm2->history[0].location);
+ REQUIRE(w.view() == "test_History.cc:103 (____C_A_T_C_H____T_E_S_T____0)");
- sm2->history[1].location.str(buf, sizeof(buf));
- REQUIRE(string_view{buf} == "test_History.cc:96 (____C_A_T_C_H____T_E_S_T____0)");
+ w.reset().print("{}", sm2->history[1].location);
+ REQUIRE(w.view() == "test_History.cc:98 (____C_A_T_C_H____T_E_S_T____0)");
sm2->history.clear();
REQUIRE(sm2->history.size() == 0);