diff --git a/node.gyp b/node.gyp index c46becb586e7ce..1495b0dab2d80b 100644 --- a/node.gyp +++ b/node.gyp @@ -608,6 +608,7 @@ 'src/connect_wrap.h', 'src/connection_wrap.h', 'src/debug_utils.h', + 'src/debug_utils-inl.h', 'src/env.h', 'src/env-inl.h', 'src/handle_wrap.h', diff --git a/src/debug_utils-inl.h b/src/debug_utils-inl.h new file mode 100644 index 00000000000000..8e4570a37a5195 --- /dev/null +++ b/src/debug_utils-inl.h @@ -0,0 +1,86 @@ +#ifndef SRC_DEBUG_UTILS_INL_H_ +#define SRC_DEBUG_UTILS_INL_H_ + +#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS + +#include "debug_utils.h" + +#include + +namespace node { + +struct ToStringHelper { + template + static std::string Convert( + const T& value, + std::string(T::* to_string)() const = &T::ToString) { + return (value.*to_string)(); + } + template ::value, bool>::type, + typename dummy = bool> + static std::string Convert(const T& value) { return std::to_string(value); } + static std::string Convert(const char* value) { return value; } + static std::string Convert(const std::string& value) { return value; } + static std::string Convert(bool value) { return value ? "true" : "false"; } +}; + +template +std::string ToString(const T& value) { + return ToStringHelper::Convert(value); +} + +inline std::string SPrintFImpl(const char* format) { + return format; +} + +template +std::string COLD_NOINLINE SPrintFImpl( // NOLINT(runtime/string) + const char* format, Arg&& arg, Args&&... args) { + const char* p = strchr(format, '%'); + if (p == nullptr) return format; + std::string ret(format, p); + // Ignore long / size_t modifiers + while (strchr("lz", *++p) != nullptr) {} + switch (*p) { + case '%': { + return ret + '%' + SPrintFImpl(p + 1, + std::forward(arg), + std::forward(args)...); + } + default: { + return ret + '%' + SPrintFImpl(p, + std::forward(arg), + std::forward(args)...); + } + case 'd': + case 'i': + case 'u': + case 's': ret += ToString(arg); break; + case 'p': { + CHECK(std::is_pointer::type>::value); + char out[20]; + int n = snprintf(out, + sizeof(out), + "%p", + *reinterpret_cast(&arg)); + CHECK_GE(n, 0); + ret += out; + break; + } + } + return ret + SPrintFImpl(p + 1, std::forward(args)...); +} + +template +std::string COLD_NOINLINE SPrintF( // NOLINT(runtime/string) + const char* format, Args&&... args) { + return SPrintFImpl(format, std::forward(args)...); +} + +} // namespace node + +#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS + +#endif // SRC_DEBUG_UTILS_INL_H_ diff --git a/src/debug_utils.h b/src/debug_utils.h index db01cacba6a1b6..08d23bb7703fe6 100644 --- a/src/debug_utils.h +++ b/src/debug_utils.h @@ -22,6 +22,17 @@ namespace node { +template +inline std::string ToString(const T& value); + +// C++-style variant of sprintf() that: +// - Returns an std::string +// - Handles \0 bytes correctly +// - Supports %p and %s. %d, %i and %u are aliases for %s. +// - Accepts any class that has a ToString() method for stringification. +template +inline std::string SPrintF(const char* format, Args&&... args); + template inline void FORCE_INLINE Debug(Environment* env, DebugCategory cat, @@ -29,7 +40,8 @@ inline void FORCE_INLINE Debug(Environment* env, Args&&... args) { if (!UNLIKELY(env->debug_enabled(cat))) return; - fprintf(stderr, format, std::forward(args)...); + std::string out = SPrintF(format, std::forward(args)...); + fwrite(out.data(), out.size(), 1, stderr); } inline void FORCE_INLINE Debug(Environment* env, diff --git a/src/inspector_profiler.cc b/src/inspector_profiler.cc index e0d02d6952a3f9..f9d3c2b512f1c1 100644 --- a/src/inspector_profiler.cc +++ b/src/inspector_profiler.cc @@ -1,6 +1,6 @@ #include "inspector_profiler.h" #include "base_object-inl.h" -#include "debug_utils.h" +#include "debug_utils-inl.h" #include "diagnosticfilename-inl.h" #include "memory_tracker-inl.h" #include "node_file.h" diff --git a/src/node_http2.cc b/src/node_http2.cc index e0d6398f2a44a9..f8d9363902b829 100644 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -1,5 +1,5 @@ #include "aliased_buffer.h" -#include "debug_utils.h" +#include "debug_utils-inl.h" #include "memory_tracker-inl.h" #include "node.h" #include "node_buffer.h" diff --git a/src/node_wasi.cc b/src/node_wasi.cc index 277b171224c290..20872c58d60fb7 100644 --- a/src/node_wasi.cc +++ b/src/node_wasi.cc @@ -1,6 +1,6 @@ #include "env-inl.h" #include "base_object-inl.h" -#include "debug_utils.h" +#include "debug_utils-inl.h" #include "memory_tracker-inl.h" #include "node_mem-inl.h" #include "util-inl.h" @@ -1063,7 +1063,7 @@ void WASI::PathFilestatGet(const FunctionCallbackInfo& args) { CHECK_TO_TYPE_OR_RETURN(args, args[4], Uint32, buf_ptr); ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This()); Debug(wasi, - "path_filestat_get(%d, %d, %d, %d, %d)\n", + "path_filestat_get(%d, %d, %d)\n", fd, path_ptr, path_len); diff --git a/src/tls_wrap.cc b/src/tls_wrap.cc index 10ebc4ccd9a8de..82274fde6db0c1 100644 --- a/src/tls_wrap.cc +++ b/src/tls_wrap.cc @@ -21,7 +21,7 @@ #include "tls_wrap.h" #include "async_wrap-inl.h" -#include "debug_utils.h" +#include "debug_utils-inl.h" #include "memory_tracker-inl.h" #include "node_buffer.h" // Buffer #include "node_crypto.h" // SecureContext diff --git a/test/cctest/test_util.cc b/test/cctest/test_util.cc index 7a0da1e6185d51..de9c5368bddfda 100644 --- a/test/cctest/test_util.cc +++ b/test/cctest/test_util.cc @@ -1,4 +1,6 @@ #include "util-inl.h" +#include "debug_utils-inl.h" +#include "env-inl.h" #include "gtest/gtest.h" TEST(UtilTest, ListHead) { @@ -250,3 +252,43 @@ TEST(UtilTest, MaybeStackBuffer) { EXPECT_TRUE(buf.IsInvalidated()); } } + +TEST(UtilTest, SPrintF) { + using node::SPrintF; + + // %d, %u and %s all do the same thing. The actual C++ type is used to infer + // the right representation. + EXPECT_EQ(SPrintF("%s", false), "false"); + EXPECT_EQ(SPrintF("%s", true), "true"); + EXPECT_EQ(SPrintF("%d", true), "true"); + EXPECT_EQ(SPrintF("%u", true), "true"); + EXPECT_EQ(SPrintF("%d", 10000000000LL), "10000000000"); + EXPECT_EQ(SPrintF("%d", -10000000000LL), "-10000000000"); + EXPECT_EQ(SPrintF("%u", 10000000000LL), "10000000000"); + EXPECT_EQ(SPrintF("%u", -10000000000LL), "-10000000000"); + EXPECT_EQ(SPrintF("%i", 10), "10"); + EXPECT_EQ(SPrintF("%d", 10), "10"); + + EXPECT_EQ(atof(SPrintF("%s", 0.5).c_str()), 0.5); + EXPECT_EQ(atof(SPrintF("%s", -0.5).c_str()), -0.5); + + void (*fn)() = []() {}; + void* p = reinterpret_cast(&fn); + EXPECT_GE(SPrintF("%p", fn).size(), 8u); + EXPECT_GE(SPrintF("%p", p).size(), 8u); + + const std::string foo = "foo"; + const char* bar = "bar"; + EXPECT_EQ(SPrintF("%s %s", foo, "bar"), "foo bar"); + EXPECT_EQ(SPrintF("%s %s", foo, bar), "foo bar"); + + struct HasToString { + std::string ToString() const { + return "meow"; + } + }; + EXPECT_EQ(SPrintF("%s", HasToString{}), "meow"); + + const std::string with_zero = std::string("a") + '\0' + 'b'; + EXPECT_EQ(SPrintF("%s", with_zero), with_zero); +}