GeezerVison is showing github.com — src: add C++-style sprintf utility · lance/node@9cc747b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: node.gyp +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 608 | 608 | 'src/connect_wrap.h', | 609 | 609 | 'src/connection_wrap.h', | 610 | 610 | 'src/debug_utils.h', | | 611 | + 'src/debug_utils-inl.h', | 611 | 612 | 'src/env.h', | 612 | 613 | 'src/env-inl.h', | 613 | 614 | 'src/handle_wrap.h', | |
Collapse file Copy file name to clipboard +90Lines changed: 90 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +#ifndef SRC_DEBUG_UTILS_INL_H_ | | 2 | +#define SRC_DEBUG_UTILS_INL_H_ | | 3 | + | | 4 | +#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | | 5 | + | | 6 | +#include "debug_utils.h" | | 7 | + | | 8 | +#include <type_traits> | | 9 | + | | 10 | +namespace node { | | 11 | + | | 12 | +struct ToStringHelper { | | 13 | + template <typename T> | | 14 | + static std::string Convert( | | 15 | + const T& value, | | 16 | + std::string(T::* to_string)() const = &T::ToString) { | | 17 | + return (value.*to_string)(); | | 18 | + } | | 19 | + template <typename T, | | 20 | + typename test_for_number = typename std:: | | 21 | + enable_if<std::is_arithmetic<T>::value, bool>::type, | | 22 | + typename dummy = bool> | | 23 | + static std::string Convert(const T& value) { return std::to_string(value); } | | 24 | + static std::string Convert(const char* value) { return value; } | | 25 | + static std::string Convert(const std::string& value) { return value; } | | 26 | + static std::string Convert(bool value) { return value ? "true" : "false"; } | | 27 | +}; | | 28 | + | | 29 | +template <typename T> | | 30 | +std::string ToString(const T& value) { | | 31 | + return ToStringHelper::Convert(value); | | 32 | +} | | 33 | + | | 34 | +inline std::string SPrintFImpl(const char* format) { | | 35 | + const char* p = strchr(format, '%'); | | 36 | + if (LIKELY(p == nullptr)) return format; | | 37 | + CHECK_EQ(p[1], '%'); // Only '%%' allowed when there are no arguments. | | 38 | + | | 39 | + return std::string(format, p + 1) + SPrintFImpl(p + 2); | | 40 | +} | | 41 | + | | 42 | +template <typename Arg, typename... Args> | | 43 | +std::string COLD_NOINLINE SPrintFImpl( // NOLINT(runtime/string) | | 44 | + const char* format, Arg&& arg, Args&&... args) { | | 45 | + const char* p = strchr(format, '%'); | | 46 | + CHECK_NOT_NULL(p); // If you hit this, you passed in too many arguments. | | 47 | + std::string ret(format, p); | | 48 | + // Ignore long / size_t modifiers | | 49 | + while (strchr("lz", *++p) != nullptr) {} | | 50 | + switch (*p) { | | 51 | + case '%': { | | 52 | + return ret + '%' + SPrintFImpl(p + 1, | | 53 | + std::forward<Arg>(arg), | | 54 | + std::forward<Args>(args)...); | | 55 | + } | | 56 | + default: { | | 57 | + return ret + '%' + SPrintFImpl(p, | | 58 | + std::forward<Arg>(arg), | | 59 | + std::forward<Args>(args)...); | | 60 | + } | | 61 | + case 'd': | | 62 | + case 'i': | | 63 | + case 'u': | | 64 | + case 's': ret += ToString(arg); break; | | 65 | + case 'p': { | | 66 | + CHECK(std::is_pointer<typename std::remove_reference<Arg>::type>::value); | | 67 | + char out[20]; | | 68 | + int n = snprintf(out, | | 69 | + sizeof(out), | | 70 | + "%p", | | 71 | + *reinterpret_cast<const void* const*>(&arg)); | | 72 | + CHECK_GE(n, 0); | | 73 | + ret += out; | | 74 | + break; | | 75 | + } | | 76 | + } | | 77 | + return ret + SPrintFImpl(p + 1, std::forward<Args>(args)...); | | 78 | +} | | 79 | + | | 80 | +template <typename... Args> | | 81 | +std::string COLD_NOINLINE SPrintF( // NOLINT(runtime/string) | | 82 | + const char* format, Args&&... args) { | | 83 | + return SPrintFImpl(format, std::forward<Args>(args)...); | | 84 | +} | | 85 | + | | 86 | +} // namespace node | | 87 | + | | 88 | +#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | | 89 | + | | 90 | +#endif // SRC_DEBUG_UTILS_INL_H_ |
Collapse file Copy file name to clipboardExpand all lines: src/debug_utils.cc +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -#include "debug_utils.h" | | 1 | +#include "debug_utils-inl.h" // NOLINT(build/include) | 2 | 2 | #include "env-inl.h" | 3 | 3 |
| 4 | 4 | #ifdef __POSIX__ | |
Collapse file Copy file name to clipboardExpand all lines: src/debug_utils.h +13-1Lines changed: 13 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 |
| 23 | 23 | namespace node { | 24 | 24 |
| | 25 | +template <typename T> | | 26 | +inline std::string ToString(const T& value); | | 27 | + | | 28 | +// C++-style variant of sprintf() that: | | 29 | +// - Returns an std::string | | 30 | +// - Handles \0 bytes correctly | | 31 | +// - Supports %p and %s. %d, %i and %u are aliases for %s. | | 32 | +// - Accepts any class that has a ToString() method for stringification. | | 33 | +template <typename... Args> | | 34 | +inline std::string SPrintF(const char* format, Args&&... args); | | 35 | + | 25 | 36 | template <typename... Args> | 26 | 37 | inline void FORCE_INLINE Debug(Environment* env, | 27 | 38 | DebugCategory cat, | 28 | 39 | const char* format, | 29 | 40 | Args&&... args) { | 30 | 41 | if (!UNLIKELY(env->debug_enabled(cat))) | 31 | 42 | return; | 32 | | - fprintf(stderr, format, std::forward<Args>(args)...); | | 43 | + std::string out = SPrintF(format, std::forward<Args>(args)...); | | 44 | + fwrite(out.data(), out.size(), 1, stderr); | 33 | 45 | } | 34 | 46 |
| 35 | 47 | inline void FORCE_INLINE Debug(Environment* env, | |
Collapse file Copy file name to clipboardExpand all lines: src/inspector_io.cc +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | #include "inspector/main_thread_interface.h" | 5 | 5 | #include "inspector/node_string.h" | 6 | 6 | #include "base_object-inl.h" | 7 | | -#include "debug_utils.h" | | 7 | +#include "debug_utils-inl.h" | 8 | 8 | #include "node.h" | 9 | 9 | #include "node_crypto.h" | 10 | 10 | #include "node_internals.h" | |
Collapse file Copy file name to clipboardExpand all lines: src/inspector_profiler.cc +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | #include "inspector_profiler.h" | 2 | 2 | #include "base_object-inl.h" | 3 | | -#include "debug_utils.h" | | 3 | +#include "debug_utils-inl.h" | 4 | 4 | #include "diagnosticfilename-inl.h" | 5 | 5 | #include "memory_tracker-inl.h" | 6 | 6 | #include "node_file.h" | |
Collapse file Copy file name to clipboardExpand all lines: src/node.cc +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 23 | 23 |
| 24 | 24 | // ========== local headers ========== | 25 | 25 |
| 26 | | -#include "debug_utils.h" | | 26 | +#include "debug_utils-inl.h" | 27 | 27 | #include "env-inl.h" | 28 | 28 | #include "memory_tracker-inl.h" | 29 | 29 | #include "node_binding.h" | |
Collapse file Copy file name to clipboardExpand all lines: src/node_http2.cc +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | #include "aliased_buffer.h" | 2 | | -#include "debug_utils.h" | | 2 | +#include "debug_utils-inl.h" | 3 | 3 | #include "memory_tracker-inl.h" | 4 | 4 | #include "node.h" | 5 | 5 | #include "node_buffer.h" | @@ -1959,7 +1959,7 @@ std::string Http2Stream::diagnostic_name() const { | 1959 | 1959 |
| 1960 | 1960 | // Notify the Http2Stream that a new block of HEADERS is being processed. | 1961 | 1961 | void Http2Stream::StartHeaders(nghttp2_headers_category category) { | 1962 | | - Debug(this, "starting headers, category: %d", id_, category); | | 1962 | + Debug(this, "starting headers, category: %d", category); | 1963 | 1963 | CHECK(!this->IsDestroyed()); | 1964 | 1964 | session_->DecrementCurrentSessionMemory(current_headers_length_); | 1965 | 1965 | current_headers_length_ = 0; | @@ -2217,7 +2217,7 @@ int Http2Stream::DoWrite(WriteWrap* req_wrap, | 2217 | 2217 | req_wrap->Done(UV_EOF); | 2218 | 2218 | return 0; | 2219 | 2219 | } | 2220 | | - Debug(this, "queuing %d buffers to send", id_, nbufs); | | 2220 | + Debug(this, "queuing %d buffers to send", nbufs); | 2221 | 2221 | for (size_t i = 0; i < nbufs; ++i) { | 2222 | 2222 | // Store the req_wrap on the last write info in the queue, so that it is | 2223 | 2223 | // only marked as finished once all buffers associated with it are finished. | |
Collapse file Copy file name to clipboardExpand all lines: src/node_messaging.cc +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | #include "node_messaging.h" | 2 | 2 |
| 3 | 3 | #include "async_wrap-inl.h" | 4 | | -#include "debug_utils.h" | | 4 | +#include "debug_utils-inl.h" | 5 | 5 | #include "memory_tracker-inl.h" | 6 | 6 | #include "node_contextify.h" | 7 | 7 | #include "node_buffer.h" | |
Collapse file Copy file name to clipboardExpand all lines: src/node_platform.cc +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 | #include "node_internals.h" | 3 | 3 |
| 4 | 4 | #include "env-inl.h" | 5 | | -#include "debug_utils.h" | | 5 | +#include "debug_utils-inl.h" | 6 | 6 | #include <algorithm> // find_if(), find(), move() | 7 | 7 | #include <cmath> // llround() | 8 | 8 | #include <memory> // unique_ptr(), shared_ptr(), make_shared() | |
|