diff options
| author | Bernie Innocenti <codewiz@google.com> | 2018-09-06 16:36:38 +0900 |
|---|---|---|
| committer | Bernie Innocenti <codewiz@google.com> | 2018-09-06 12:19:59 +0000 |
| commit | c7994ae2259fcd850d0f5337689a6498be17dba9 (patch) | |
| tree | ff8b03bcf3bd0e13a88fbebf98bb8813d8a5d54b /libnetdutils/include | |
| parent | a5211079dddea75278b6cf8fa5016b6d15146ed9 (diff) | |
| download | platform_system_netd-c7994ae2259fcd850d0f5337689a6498be17dba9.tar.gz platform_system_netd-c7994ae2259fcd850d0f5337689a6498be17dba9.tar.bz2 platform_system_netd-c7994ae2259fcd850d0f5337689a6498be17dba9.zip | |
Add a variadic overload of Log::arg() to print all arguments
Additionally limit the scope of the arg(bool) overload to fix literal
strings being logged as 'true'. Found while adding a few more tests...
Test: atest netdutils_test
Change-Id: If2c398bab6c2c809274e6b2d247cdb38494cb8e3
Diffstat (limited to 'libnetdutils/include')
| -rw-r--r-- | libnetdutils/include/netdutils/Log.h | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/libnetdutils/include/netdutils/Log.h b/libnetdutils/include/netdutils/Log.h index 7a68a8ff5..90715db2a 100644 --- a/libnetdutils/include/netdutils/Log.h +++ b/libnetdutils/include/netdutils/Log.h @@ -21,6 +21,7 @@ #include <deque> #include <shared_mutex> #include <string> +#include <type_traits> #include <android-base/stringprintf.h> #include <android-base/thread_annotations.h> @@ -55,14 +56,23 @@ class LogEntry { // Convenience methods for each of the common types of function arguments. LogEntry& arg(const std::string& val); LogEntry& arg(const std::vector<int32_t>& val); - LogEntry& arg(const std::vector<uint8_t>& val); LogEntry& arg(const std::vector<std::string>& val); - LogEntry& arg(bool val); - template <class T> - LogEntry& arg(T val) { + template <typename IntT, typename = std::enable_if_t<std::is_arithmetic_v<IntT>>> + LogEntry& arg(IntT val) { mArgs.push_back(std::to_string(val)); return *this; } + // Not using a plain overload here to avoid the implicit conversion from + // any pointer to bool, which causes string literals to print as 'true'. + template <> + LogEntry& arg<>(bool val); + + 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)... }; + return *this; + } // Some things can return more than one value, or have multiple output // parameters, so each of these adds to the mReturns vector. |
