@@ -338,19 +338,73 @@ Maybe<void> KVStore::AssignToObject(v8::Isolate* isolate,
338338 return JustVoid ();
339339}
340340
341- void PrintTraceEnvStack (Environment* env) {
342- PrintTraceEnvStack (env->options ());
343- }
341+ struct TraceEnvVarOptions {
342+ bool print_message : 1 = 0 ;
343+ bool print_js_stack : 1 = 0 ;
344+ bool print_native_stack : 1 = 0 ;
345+ };
344346
345- void PrintTraceEnvStack (std::shared_ptr<EnvironmentOptions> options) {
346- if (options->trace_env_native_stack ) {
347+ template <typename ... Args>
348+ inline void TraceEnvVarImpl (Environment* env,
349+ TraceEnvVarOptions options,
350+ const char * format,
351+ Args&&... args) {
352+ if (options.print_message ) {
353+ fprintf (stderr, format, std::forward<Args>(args)...);
354+ }
355+ if (options.print_native_stack ) {
347356 DumpNativeBacktrace (stderr);
348357 }
349- if (options-> trace_env_js_stack ) {
358+ if (options. print_js_stack ) {
350359 DumpJavaScriptBacktrace (stderr);
351360 }
352361}
353362
363+ TraceEnvVarOptions GetTraceEnvVarOptions (Environment* env) {
364+ TraceEnvVarOptions options;
365+ auto cli_options = env != nullptr
366+ ? env->options ()
367+ : per_process::cli_options->per_isolate ->per_env ;
368+ if (cli_options->trace_env ) {
369+ options.print_message = 1 ;
370+ };
371+ if (cli_options->trace_env_js_stack ) {
372+ options.print_js_stack = 1 ;
373+ };
374+ if (cli_options->trace_env_native_stack ) {
375+ options.print_native_stack = 1 ;
376+ };
377+ return options;
378+ }
379+
380+ void TraceEnvVar (Environment* env, const char * message) {
381+ TraceEnvVarImpl (
382+ env, GetTraceEnvVarOptions (env), " [--trace-env] %s\n " , message);
383+ }
384+
385+ void TraceEnvVar (Environment* env, const char * message, const char * key) {
386+ TraceEnvVarImpl (env,
387+ GetTraceEnvVarOptions (env),
388+ " [--trace-env] %s \" %s\"\n " ,
389+ message,
390+ key);
391+ }
392+
393+ void TraceEnvVar (Environment* env,
394+ const char * message,
395+ v8::Local<v8::String> key) {
396+ TraceEnvVarOptions options = GetTraceEnvVarOptions (env);
397+ if (options.print_message ) {
398+ Utf8Value key_utf8 (env->isolate (), key);
399+ TraceEnvVarImpl (env,
400+ options,
401+ " [--trace-env] %s \" %.*s\"\n " ,
402+ message,
403+ static_cast <int >(key_utf8.length ()),
404+ key_utf8.out ());
405+ }
406+ }
407+
354408static Intercepted EnvGetter (Local<Name> property,
355409 const PropertyCallbackInfo<Value>& info) {
356410 Environment* env = Environment::GetCurrent (info);
@@ -364,14 +418,7 @@ static Intercepted EnvGetter(Local<Name> property,
364418 env->env_vars ()->Get (env->isolate (), property.As <String>());
365419
366420 bool has_env = !value_string.IsEmpty ();
367- if (env->options ()->trace_env ) {
368- Utf8Value key (env->isolate (), property.As <String>());
369- fprintf (stderr,
370- " [--trace-env] get environment variable \" %.*s\"\n " ,
371- static_cast <int >(key.length ()),
372- key.out ());
373- PrintTraceEnvStack (env);
374- }
421+ TraceEnvVar (env, " get" , property.As <String>());
375422
376423 if (has_env) {
377424 info.GetReturnValue ().Set (value_string.ToLocalChecked ());
@@ -411,14 +458,7 @@ static Intercepted EnvSetter(Local<Name> property,
411458 }
412459
413460 env->env_vars ()->Set (env->isolate (), key, value_string);
414- if (env->options ()->trace_env ) {
415- Utf8Value key_utf8 (env->isolate (), key);
416- fprintf (stderr,
417- " [--trace-env] set environment variable \" %.*s\"\n " ,
418- static_cast <int >(key_utf8.length ()),
419- key_utf8.out ());
420- PrintTraceEnvStack (env);
421- }
461+ TraceEnvVar (env, " set" , key);
422462
423463 return Intercepted::kYes ;
424464}
@@ -430,16 +470,7 @@ static Intercepted EnvQuery(Local<Name> property,
430470 if (property->IsString ()) {
431471 int32_t rc = env->env_vars ()->Query (env->isolate (), property.As <String>());
432472 bool has_env = (rc != -1 );
433-
434- if (env->options ()->trace_env ) {
435- Utf8Value key_utf8 (env->isolate (), property.As <String>());
436- fprintf (stderr,
437- " [--trace-env] query environment variable \" %.*s\" : %s\n " ,
438- static_cast <int >(key_utf8.length ()),
439- key_utf8.out (),
440- has_env ? " is set" : " is not set" );
441- PrintTraceEnvStack (env);
442- }
473+ TraceEnvVar (env, " query" , property.As <String>());
443474 if (has_env) {
444475 // Return attributes for the property.
445476 info.GetReturnValue ().Set (v8::None);
@@ -456,14 +487,7 @@ static Intercepted EnvDeleter(Local<Name> property,
456487 if (property->IsString ()) {
457488 env->env_vars ()->Delete (env->isolate (), property.As <String>());
458489
459- if (env->options ()->trace_env ) {
460- Utf8Value key_utf8 (env->isolate (), property.As <String>());
461- fprintf (stderr,
462- " [--trace-env] delete environment variable \" %.*s\"\n " ,
463- static_cast <int >(key_utf8.length ()),
464- key_utf8.out ());
465- PrintTraceEnvStack (env);
466- }
490+ TraceEnvVar (env, " delete" , property.As <String>());
467491 }
468492
469493 // process.env never has non-configurable properties, so always
@@ -476,11 +500,7 @@ static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
476500 Environment* env = Environment::GetCurrent (info);
477501 CHECK (env->has_run_bootstrapping_code ());
478502
479- if (env->options ()->trace_env ) {
480- fprintf (stderr, " [--trace-env] enumerate environment variables\n " );
481-
482- PrintTraceEnvStack (env);
483- }
503+ TraceEnvVar (env, " enumerate environment variables" );
484504
485505 info.GetReturnValue ().Set (
486506 env->env_vars ()->Enumerate (env->isolate ()));
0 commit comments