Add a Log::arg() variant to hexdump binary buffers
Perhaps we should make this work with Slices, easy to add once the need
arises.
Test: atest netdutils_test
Change-Id: I031b8e41e9ab7b59a857e77438bc3fa193e755b3
diff --git a/libnetdutils/Log.cpp b/libnetdutils/Log.cpp
index 1a742f6..d2ce98f 100644
--- a/libnetdutils/Log.cpp
+++ b/libnetdutils/Log.cpp
@@ -15,6 +15,7 @@
*/
#include "netdutils/Log.h"
+#include "netdutils/Slice.h"
#include <chrono>
#include <ctime>
@@ -117,7 +118,7 @@
return *this;
}
-template<>
+template <>
LogEntry& LogEntry::arg<>(bool val) {
mArgs.push_back(val ? "true" : "false");
return *this;
@@ -128,6 +129,11 @@
return *this;
}
+LogEntry& LogEntry::arg(const std::vector<uint8_t>& val) {
+ mArgs.push_back('{' + toHex(makeSlice(val)) + '}');
+ return *this;
+}
+
LogEntry& LogEntry::arg(const std::vector<std::string>& val) {
mArgs.push_back(StringPrintf("[%s]", Join(val, ", ").c_str()));
return *this;
diff --git a/libnetdutils/LogTest.cpp b/libnetdutils/LogTest.cpp
index e95b30d..1270560 100644
--- a/libnetdutils/LogTest.cpp
+++ b/libnetdutils/LogTest.cpp
@@ -128,6 +128,12 @@
EXPECT_EQ("testFunc(65, 100, -1000)", entry.toString());
}
+TEST(LogEntryTest, PrintHex) {
+ const std::vector<uint8_t> buf{0xDE, 0xAD, 0xBE, 0xEF};
+ const LogEntry entry = LogEntry().function("testFunc").arg(buf);
+ EXPECT_EQ("testFunc({deadbeef})", entry.toString());
+}
+
TEST(LogEntryTest, PrintArgumentPack) {
const LogEntry entry = LogEntry().function("testFunc").args("hello", 42, false);
EXPECT_EQ("testFunc(hello, 42, false)", entry.toString());
diff --git a/libnetdutils/include/netdutils/Log.h b/libnetdutils/include/netdutils/Log.h
index 90715db..2e94fdf 100644
--- a/libnetdutils/include/netdutils/Log.h
+++ b/libnetdutils/include/netdutils/Log.h
@@ -55,6 +55,8 @@
// Convenience methods for each of the common types of function arguments.
LogEntry& arg(const std::string& val);
+ // Intended for binary buffers, formats as hex
+ LogEntry& arg(const std::vector<uint8_t>& val);
LogEntry& arg(const std::vector<int32_t>& val);
LogEntry& arg(const std::vector<std::string>& val);
template <typename IntT, typename = std::enable_if_t<std::is_arithmetic_v<IntT>>>
@@ -67,10 +69,11 @@
template <>
LogEntry& arg<>(bool val);
- template <typename... Args> LogEntry& args(const Args&... a) {
+ template <typename... Args>
+ LogEntry& args(const Args&... a) {
// Cleverness ahead: we throw away the initializer_list filled with
// zeroes, all we care about is calling arg() for each argument.
- (void)std::initializer_list<int>{ (arg(a), 0)... };
+ (void) std::initializer_list<int>{(arg(a), 0)...};
return *this;
}
diff --git a/libnetdutils/include/netdutils/Slice.h b/libnetdutils/include/netdutils/Slice.h
index f194514..717fbd1 100644
--- a/libnetdutils/include/netdutils/Slice.h
+++ b/libnetdutils/include/netdutils/Slice.h
@@ -135,7 +135,7 @@
// Return a string containing a hexadecimal representation of the contents of s.
// This function inserts a newline into its output every wrap bytes.
-std::string toHex(const Slice s, int wrap);
+std::string toHex(const Slice s, int wrap = INT_MAX);
inline bool operator==(const Slice& lhs, const Slice& rhs) {
return (lhs.base() == rhs.base()) && (lhs.limit() == rhs.limit());