|
11 | 11 | using System.Reflection; |
12 | 12 | using System.Runtime.CompilerServices; |
13 | 13 | using System.Runtime.InteropServices; |
14 | | -using System.Threading; |
15 | 14 | using Microsoft.JavaScript.NodeApi.Interop; |
16 | 15 | using Microsoft.JavaScript.NodeApi.Runtime; |
17 | 16 | using static Microsoft.JavaScript.NodeApi.Runtime.JSRuntime; |
@@ -210,7 +209,7 @@ public static napi_value InitializeModule(napi_env env, napi_value exports) |
210 | 209 | Trace($" .NET Runtime version: {Environment.Version}"); |
211 | 210 | #endif |
212 | 211 |
|
213 | | - AttachDebugger(); |
| 212 | + DebugHelper.AttachDebugger("DEBUG_NODE_API_RUNTIME"); |
214 | 213 |
|
215 | 214 | JSRuntime runtime = new NodejsRuntime(); |
216 | 215 |
|
@@ -638,70 +637,4 @@ public override void TraceEvent( |
638 | 637 | params object?[]? args) |
639 | 638 | => WriteLine(string.Format(format ?? string.Empty, args ?? [])); |
640 | 639 | } |
641 | | - |
642 | | - [Conditional("DEBUG")] |
643 | | - private static void AttachDebugger() |
644 | | - { |
645 | | - string? debugValue = Environment.GetEnvironmentVariable("DEBUG_NODE_API_RUNTIME"); |
646 | | - if (string.Equals(debugValue, "VS", StringComparison.OrdinalIgnoreCase)) |
647 | | - { |
648 | | - // Launch the Visual Studio debugger. |
649 | | - Debugger.Launch(); |
650 | | - } |
651 | | - else if (!string.IsNullOrEmpty(debugValue)) |
652 | | - { |
653 | | - Process currentProcess = Process.GetCurrentProcess(); |
654 | | - string processName = currentProcess.ProcessName; |
655 | | - int processId = currentProcess.Id; |
656 | | - Console.WriteLine("###################### DEBUG ######################"); |
657 | | - Console.WriteLine($"Process \"{processName}\" ({processId}) is waiting for debugger."); |
658 | | - |
659 | | - int waitSeconds = 20; |
660 | | - string waitingMessage = "Press any key to continue without debugging... "; |
661 | | - |
662 | | - if (!Console.IsOutputRedirected) |
663 | | - { |
664 | | - Console.Write(waitingMessage + $"({waitSeconds})"); |
665 | | - } |
666 | | - |
667 | | - Stopwatch stopwatch = Stopwatch.StartNew(); |
668 | | - int remainingSeconds = waitSeconds; |
669 | | - while (!Debugger.IsAttached) |
670 | | - { |
671 | | - if (Console.KeyAvailable) |
672 | | - { |
673 | | - Console.ReadKey(true); |
674 | | - Console.WriteLine(); |
675 | | - return; |
676 | | - } |
677 | | - else if (stopwatch.Elapsed > TimeSpan.FromSeconds(waitSeconds)) |
678 | | - { |
679 | | - Console.WriteLine( |
680 | | - $"Debugger did not attach after {waitSeconds} seconds. Continuing."); |
681 | | - return; |
682 | | - } |
683 | | - |
684 | | - Thread.Sleep(100); |
685 | | - |
686 | | - if (remainingSeconds > waitSeconds - (int)stopwatch.Elapsed.TotalSeconds) |
687 | | - { |
688 | | - remainingSeconds = waitSeconds - (int)stopwatch.Elapsed.TotalSeconds; |
689 | | - |
690 | | - if (!Console.IsOutputRedirected) |
691 | | - { |
692 | | - Console.CursorLeft = waitingMessage.Length; |
693 | | - Console.Write($"({remainingSeconds:D2})"); |
694 | | - } |
695 | | - } |
696 | | - } |
697 | | - |
698 | | - if (!Console.IsOutputRedirected) |
699 | | - { |
700 | | - Console.CursorLeft = waitingMessage.Length; |
701 | | - Console.WriteLine(" "); |
702 | | - } |
703 | | - |
704 | | - Debugger.Break(); |
705 | | - } |
706 | | - } |
707 | 640 | } |
0 commit comments