-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
src: use custom fprintf alike to write errors to stderr #31446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a35bf6b
src: add C++-style sprintf utility
addaleax ec00fda
src: use custom fprintf alike to write errors to stderr
addaleax 6db1bb7
fixup! src: add C++-style sprintf utility
addaleax 3d90553
fixup! src: use custom fprintf alike to write errors to stderr
addaleax 8824f8b
fixup! src: add C++-style sprintf utility
addaleax bed1750
fixup! src: use custom fprintf alike to write errors to stderr
addaleax cb717aa
fixup! fixup! src: add C++-style sprintf utility
addaleax 3a1c801
fixup! fixup! fixup! src: add C++-style sprintf utility
addaleax a851023
fixup! src: add C++-style sprintf utility
addaleax bd8c9d6
fixup! src: use custom fprintf alike to write errors to stderr
addaleax 13b9da7
fixup! src: add C++-style sprintf utility
addaleax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#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 <type_traits> | ||
|
||
namespace node { | ||
|
||
struct ToStringHelper { | ||
template <typename T> | ||
static std::string Convert( | ||
const T& value, | ||
std::string(T::* to_string)() const = &T::ToString) { | ||
return (value.*to_string)(); | ||
} | ||
template <typename T, | ||
typename test_for_number = typename std:: | ||
enable_if<std::is_arithmetic<T>::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 <typename T> | ||
std::string ToString(const T& value) { | ||
return ToStringHelper::Convert(value); | ||
} | ||
|
||
inline std::string SPrintFImpl(const char* format) { | ||
const char* p = strchr(format, '%'); | ||
if (LIKELY(p == nullptr)) return format; | ||
CHECK_EQ(p[1], '%'); // Only '%%' allowed when there are no arguments. | ||
|
||
return std::string(format, p + 1) + SPrintFImpl(p + 2); | ||
} | ||
|
||
template <typename Arg, typename... Args> | ||
std::string COLD_NOINLINE SPrintFImpl( // NOLINT(runtime/string) | ||
const char* format, Arg&& arg, Args&&... args) { | ||
const char* p = strchr(format, '%'); | ||
CHECK_NOT_NULL(p); // If you hit this, you passed in too many arguments. | ||
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>(arg), | ||
std::forward<Args>(args)...); | ||
} | ||
default: { | ||
return ret + '%' + SPrintFImpl(p, | ||
std::forward<Arg>(arg), | ||
std::forward<Args>(args)...); | ||
} | ||
case 'd': | ||
case 'i': | ||
case 'u': | ||
case 's': ret += ToString(arg); break; | ||
case 'p': { | ||
CHECK(std::is_pointer<typename std::remove_reference<Arg>::type>::value); | ||
char out[20]; | ||
int n = snprintf(out, | ||
sizeof(out), | ||
"%p", | ||
*reinterpret_cast<const void* const*>(&arg)); | ||
CHECK_GE(n, 0); | ||
ret += out; | ||
break; | ||
} | ||
} | ||
return ret + SPrintFImpl(p + 1, std::forward<Args>(args)...); | ||
} | ||
|
||
template <typename... Args> | ||
std::string COLD_NOINLINE SPrintF( // NOLINT(runtime/string) | ||
const char* format, Args&&... args) { | ||
return SPrintFImpl(format, std::forward<Args>(args)...); | ||
} | ||
|
||
template <typename... Args> | ||
void COLD_NOINLINE FPrintF(FILE* file, const char* format, Args&&... args) { | ||
FWrite(file, SPrintF(format, std::forward<Args>(args)...)); | ||
} | ||
|
||
} // namespace node | ||
|
||
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
||
#endif // SRC_DEBUG_UTILS_INL_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.