44#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55
66#include " async_wrap.h"
7- #include " env.h"
87
98#include < sstream>
109#include < string>
2120#endif
2221
2322namespace node {
23+ class Environment ;
2424
2525template <typename T>
2626inline std::string ToString (const T& value);
@@ -36,31 +36,71 @@ template <typename... Args>
3636inline void FPrintF (FILE* file, const char * format, Args&&... args);
3737void FWrite (FILE* file, const std::string& str);
3838
39+ // Listing the AsyncWrap provider types first enables us to cast directly
40+ // from a provider type to a debug category.
41+ #define DEBUG_CATEGORY_NAMES (V ) \
42+ NODE_ASYNC_PROVIDER_TYPES (V) \
43+ V (INSPECTOR_SERVER) \
44+ V (INSPECTOR_PROFILER) \
45+ V (WASI)
46+
47+ enum class DebugCategory {
48+ #define V (name ) name,
49+ DEBUG_CATEGORY_NAMES (V)
50+ #undef V
51+ CATEGORY_COUNT
52+ };
53+
54+ class EnabledDebugList {
55+ public:
56+ bool enabled (DebugCategory category) const {
57+ DCHECK_GE (static_cast <int >(category), 0 );
58+ DCHECK_LT (static_cast <int >(category),
59+ static_cast <int >(DebugCategory::CATEGORY_COUNT));
60+ return enabled_[static_cast <int >(category)];
61+ }
62+
63+ // Uses NODE_DEBUG_NATIVE to initialize the categories. When env is not a
64+ // nullptr, the environment variables set in the Environment are used.
65+ // Otherwise the system environment variables are used.
66+ void Parse (Environment* env);
67+
68+ private:
69+ // Set all categories matching cats to the value of enabled.
70+ void Parse (const std::string& cats, bool enabled);
71+ void set_enabled (DebugCategory category, bool enabled) {
72+ DCHECK_GE (static_cast <int >(category), 0 );
73+ DCHECK_LT (static_cast <int >(category),
74+ static_cast <int >(DebugCategory::CATEGORY_COUNT));
75+ enabled_[static_cast <int >(category)] = true ;
76+ }
77+
78+ bool enabled_[static_cast <int >(DebugCategory::CATEGORY_COUNT)] = {false };
79+ };
80+
3981template <typename ... Args>
40- inline void FORCE_INLINE Debug (Environment* env ,
82+ inline void FORCE_INLINE Debug (EnabledDebugList* list ,
4183 DebugCategory cat,
4284 const char * format,
43- Args&&... args) {
44- if (!UNLIKELY (env->debug_enabled (cat)))
45- return ;
46- FPrintF (stderr, format, std::forward<Args>(args)...);
47- }
85+ Args&&... args);
86+
87+ inline void FORCE_INLINE Debug (EnabledDebugList* list,
88+ DebugCategory cat,
89+ const char * message);
90+
91+ template <typename ... Args>
92+ inline void FORCE_INLINE
93+ Debug (Environment* env, DebugCategory cat, const char * format, Args&&... args);
4894
4995inline void FORCE_INLINE Debug (Environment* env,
5096 DebugCategory cat,
51- const char * message) {
52- if (!UNLIKELY (env->debug_enabled (cat)))
53- return ;
54- FPrintF (stderr, " %s" , message);
55- }
97+ const char * message);
5698
5799template <typename ... Args>
58100inline void Debug (Environment* env,
59101 DebugCategory cat,
60102 const std::string& format,
61- Args&&... args) {
62- Debug (env, cat, format.c_str (), std::forward<Args>(args)...);
63- }
103+ Args&&... args);
64104
65105// Used internally by the 'real' Debug(AsyncWrap*, ...) functions below, so that
66106// the FORCE_INLINE flag on them doesn't apply to the contents of this function
@@ -72,31 +112,17 @@ inline void Debug(Environment* env,
72112template <typename ... Args>
73113void COLD_NOINLINE UnconditionalAsyncWrapDebug (AsyncWrap* async_wrap,
74114 const char * format,
75- Args&&... args) {
76- Debug (async_wrap->env (),
77- static_cast <DebugCategory>(async_wrap->provider_type ()),
78- async_wrap->diagnostic_name () + " " + format + " \n " ,
79- std::forward<Args>(args)...);
80- }
115+ Args&&... args);
81116
82117template <typename ... Args>
83118inline void FORCE_INLINE Debug (AsyncWrap* async_wrap,
84119 const char * format,
85- Args&&... args) {
86- DCHECK_NOT_NULL (async_wrap);
87- DebugCategory cat =
88- static_cast <DebugCategory>(async_wrap->provider_type ());
89- if (!UNLIKELY (async_wrap->env ()->debug_enabled (cat)))
90- return ;
91- UnconditionalAsyncWrapDebug (async_wrap, format, std::forward<Args>(args)...);
92- }
120+ Args&&... args);
93121
94122template <typename ... Args>
95123inline void FORCE_INLINE Debug (AsyncWrap* async_wrap,
96124 const std::string& format,
97- Args&&... args) {
98- Debug (async_wrap, format.c_str (), std::forward<Args>(args)...);
99- }
125+ Args&&... args);
100126
101127// Debug helper for inspecting the currently running `node` executable.
102128class NativeSymbolDebuggingContext {
0 commit comments