diff options
author | Anna Henningsen <anna@addaleax.net> | 2020-01-21 20:30:20 +0300 |
---|---|---|
committer | Shelley Vohr <shelley.vohr@gmail.com> | 2020-02-17 22:17:00 +0300 |
commit | a685827a55a74427562fbe7255e78e5b9fba0d68 (patch) | |
tree | 1419c9a2b9e0a201c6f83204112919d1fbeef6cd /src/debug_utils.h | |
parent | f84b34d42c88329b23e5de9592bb9d5e9bb51ac8 (diff) |
src: add C++-style sprintf utility
Add an utility that handles C++-style strings and objects well.
PR-URL: https://github.com/nodejs/node/pull/31446
Fixes: https://github.com/nodejs/node/issues/28761
Fixes: https://github.com/nodejs/node/issues/31218
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'src/debug_utils.h')
-rw-r--r-- | src/debug_utils.h | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/src/debug_utils.h b/src/debug_utils.h index db01cacba6a..08d23bb7703 100644 --- a/src/debug_utils.h +++ b/src/debug_utils.h @@ -22,6 +22,17 @@ namespace node { +template <typename T> +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 <typename... Args> +inline std::string SPrintF(const char* format, Args&&... args); + template <typename... Args> 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>(args)...); + std::string out = SPrintF(format, std::forward<Args>(args)...); + fwrite(out.data(), out.size(), 1, stderr); } inline void FORCE_INLINE Debug(Environment* env, |