|
| 1 | +#ifndef SRC_DEBUG_UTILS_H_ |
| 2 | +#define SRC_DEBUG_UTILS_H_ |
| 3 | + |
| 4 | +#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
| 5 | + |
| 6 | +#include "async_wrap.h" |
| 7 | +#include "env.h" |
| 8 | +#include <string> |
| 9 | + |
| 10 | +// Use FORCE_INLINE on functions that have a debug-category-enabled check first |
| 11 | +// and then ideally only a single function call following it, to maintain |
| 12 | +// performance for the common case (no debugging used). |
| 13 | +#ifdef __GNUC__ |
| 14 | +#define FORCE_INLINE __attribute__((always_inline)) |
| 15 | +#define COLD_NOINLINE __attribute__((cold, noinline)) |
| 16 | +#else |
| 17 | +#define FORCE_INLINE |
| 18 | +#define COLD_NOINLINE |
| 19 | +#endif |
| 20 | + |
| 21 | +namespace node { |
| 22 | + |
| 23 | +template <typename... Args> |
| 24 | +inline void FORCE_INLINE Debug(Environment* env, |
| 25 | + DebugCategory cat, |
| 26 | + const char* format, |
| 27 | + Args&&... args) { |
| 28 | + if (!UNLIKELY(env->debug_enabled(cat))) |
| 29 | + return; |
| 30 | + fprintf(stderr, format, std::forward<Args>(args)...); |
| 31 | +} |
| 32 | + |
| 33 | +inline void FORCE_INLINE Debug(Environment* env, |
| 34 | + DebugCategory cat, |
| 35 | + const char* message) { |
| 36 | + if (!UNLIKELY(env->debug_enabled(cat))) |
| 37 | + return; |
| 38 | + fprintf(stderr, "%s", message); |
| 39 | +} |
| 40 | + |
| 41 | +template <typename... Args> |
| 42 | +inline void Debug(Environment* env, |
| 43 | + DebugCategory cat, |
| 44 | + const std::string& format, |
| 45 | + Args&&... args) { |
| 46 | + Debug(env, cat, format.c_str(), std::forward<Args>(args)...); |
| 47 | +} |
| 48 | + |
| 49 | +// Used internally by the 'real' Debug(AsyncWrap*, ...) functions below, so that |
| 50 | +// the FORCE_INLINE flag on them doesn't apply to the contents of this function |
| 51 | +// as well. |
| 52 | +// We apply COLD_NOINLINE to tell the compiler that it's not worth optimizing |
| 53 | +// this function for speed and it should rather focus on keeping it out of |
| 54 | +// hot code paths. In particular, we want to keep the string concatenating code |
| 55 | +// out of the function containing the original `Debug()` call. |
| 56 | +template <typename... Args> |
| 57 | +void COLD_NOINLINE UnconditionalAsyncWrapDebug(AsyncWrap* async_wrap, |
| 58 | + const char* format, |
| 59 | + Args&&... args) { |
| 60 | + Debug(async_wrap->env(), |
| 61 | + static_cast<DebugCategory>(async_wrap->provider_type()), |
| 62 | + async_wrap->diagnostic_name() + " " + format + "\n", |
| 63 | + std::forward<Args>(args)...); |
| 64 | +} |
| 65 | + |
| 66 | +template <typename... Args> |
| 67 | +inline void FORCE_INLINE Debug(AsyncWrap* async_wrap, |
| 68 | + const char* format, |
| 69 | + Args&&... args) { |
| 70 | +#ifdef DEBUG |
| 71 | + CHECK_NOT_NULL(async_wrap); |
| 72 | +#endif |
| 73 | + DebugCategory cat = |
| 74 | + static_cast<DebugCategory>(async_wrap->provider_type()); |
| 75 | + if (!UNLIKELY(async_wrap->env()->debug_enabled(cat))) |
| 76 | + return; |
| 77 | + UnconditionalAsyncWrapDebug(async_wrap, format, std::forward<Args>(args)...); |
| 78 | +} |
| 79 | + |
| 80 | +template <typename... Args> |
| 81 | +inline void FORCE_INLINE Debug(AsyncWrap* async_wrap, |
| 82 | + const std::string& format, |
| 83 | + Args&&... args) { |
| 84 | + Debug(async_wrap, format.c_str(), std::forward<Args>(args)...); |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +} // namespace node |
| 89 | + |
| 90 | +#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
| 91 | + |
| 92 | +#endif // SRC_DEBUG_UTILS_H_ |
0 commit comments