-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathProgram.cs
121 lines (105 loc) · 3.94 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Microsoft.PowerShell.EditorServices.Protocol.Server;
using Microsoft.PowerShell.EditorServices.Utility;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
namespace Microsoft.PowerShell.EditorServices.Host
{
class Program
{
[STAThread]
static void Main(string[] args)
{
string logPath = null;
string logPathArgument =
args.FirstOrDefault(
arg =>
arg.StartsWith(
"/logPath:",
StringComparison.InvariantCultureIgnoreCase));
if (!string.IsNullOrEmpty(logPathArgument))
{
logPath = logPathArgument.Substring(9).Trim('"');
}
bool runDebugAdapter =
args.Any(
arg =>
string.Equals(
arg,
"/debugAdapter",
StringComparison.InvariantCultureIgnoreCase));
// Catch unhandled exceptions for logging purposes
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
ProtocolServer server = null;
if (runDebugAdapter)
{
logPath = logPath ?? "DebugAdapter.log";
server = new DebugAdapter();
}
else
{
logPath = logPath ?? "EditorServices.log";
server = new LanguageServer();
}
// Start the logger with the specified log path
// TODO: Set the level based on command line parameter
Logger.Initialize(logPath, LogLevel.Verbose);
#if DEBUG
bool waitForDebugger =
args.Any(
arg =>
string.Equals(
arg,
"/waitForDebugger",
StringComparison.InvariantCultureIgnoreCase));
// Should we wait for the debugger before starting?
if (waitForDebugger)
{
Logger.Write(LogLevel.Normal, "Waiting for debugger to attach before continuing...");
// Wait for 15 seconds and then continue
int waitCountdown = 15;
while (!Debugger.IsAttached && waitCountdown > 0)
{
Thread.Sleep(1000);
waitCountdown--;
}
if (Debugger.IsAttached)
{
Logger.Write(
LogLevel.Normal,
"Debugger attached, continuing startup sequence");
}
else if (waitCountdown == 0)
{
Logger.Write(
LogLevel.Normal,
"Timed out while waiting for debugger to attach, continuing startup sequence");
}
}
#endif
Logger.Write(LogLevel.Normal, "PowerShell Editor Services Host starting...");
// Start the server
server.Start();
Logger.Write(LogLevel.Normal, "PowerShell Editor Services Host started!");
// Wait for the server to finish
server.WaitForExit();
Logger.Write(LogLevel.Normal, "PowerShell Editor Services Host exited normally.");
}
static void CurrentDomain_UnhandledException(
object sender,
UnhandledExceptionEventArgs e)
{
// Log the exception
Logger.Write(
LogLevel.Error,
string.Format(
"FATAL UNHANDLED EXCEPTION:\r\n\r\n{0}",
e.ExceptionObject.ToString()));
}
}
}