@@ -7,7 +7,10 @@ This Source Code Form is subject to the terms of the
77using System ;
88using System . Collections . Generic ;
99using System . IO ;
10+ using System . Runtime . CompilerServices ;
11+ using Newtonsoft . Json ;
1012using OneScript . DebugProtocol ;
13+ using Serilog ;
1114using VSCodeDebug ;
1215
1316
@@ -19,6 +22,8 @@ internal class OscriptDebugSession : DebugSession, IDebugEventListener
1922 private bool _startupPerformed = false ;
2023 private readonly Handles < OneScript . DebugProtocol . StackFrame > _framesHandles ;
2124 private readonly Handles < IVariableLocator > _variableHandles ;
25+
26+ private static readonly ILogger Log = Serilog . Log . ForContext < OscriptDebugSession > ( ) ;
2227
2328 public OscriptDebugSession ( ) : base ( true , false )
2429 {
@@ -30,7 +35,7 @@ public OscriptDebugSession() : base(true, false)
3035
3136 public override void Initialize ( Response response , dynamic args )
3237 {
33- SessionLog . WriteLine ( "Initialize:" + args ) ;
38+ LogCommandReceived ( ) ;
3439 AdapterID = ( string ) args . adapterID ;
3540
3641 _process = DebugeeFactory . CreateProcess ( AdapterID , PathStrategy ) ;
@@ -50,38 +55,38 @@ public override void Initialize(Response response, dynamic args)
5055
5156 public override void Launch ( Response response , dynamic args )
5257 {
53- SessionLog . WriteLine ( "Launch command accepted" ) ;
54-
58+ LogCommandReceived ( ) ;
5559 try
5660 {
5761 _process . Init ( args ) ;
5862 }
5963 catch ( InvalidDebugeeOptionsException e )
6064 {
65+ Log . Error ( e , "Wrong options received {ErrorCode}: {Message}" , e . ErrorCode , e . Message ) ;
6166 SendErrorResponse ( response , e . ErrorCode , e . Message ) ;
6267 return ;
6368 }
6469
6570 _process . OutputReceived += ( s , e ) =>
6671 {
67- SessionLog . WriteLine ( "output received: " + e . Content ) ;
72+ Log . Debug ( "Output received {Output}" , e . Content ) ;
6873 SendOutput ( e . Category , e . Content ) ;
6974 } ;
7075
7176 _process . ProcessExited += ( s , e ) =>
7277 {
73- SessionLog . WriteLine ( "_process exited") ;
78+ Log . Information ( "Debuggee has exited") ;
7479 SendEvent ( new TerminatedEvent ( ) ) ;
7580 } ;
7681
7782 try
7883 {
7984 _process . Start ( ) ;
80- SessionLog . WriteLine ( "Debuggee started" ) ;
85+ Log . Information ( "Debuggee started" ) ;
8186 }
8287 catch ( Exception e )
8388 {
84- SessionLog . WriteLine ( e . ToString ( ) ) ;
89+ Log . Error ( e , "Can't launch debuggee" ) ;
8590 SendErrorResponse ( response , 3012 , "Can't launch debugee ({reason})." , new { reason = e . Message } ) ;
8691 return ;
8792 }
@@ -97,7 +102,7 @@ public override void Launch(Response response, dynamic args)
97102 }
98103 else
99104 {
100- var tcpConnector = new TcpDebugConnector ( _process . DebugPort , this ) ;
105+ var tcpConnector = new TcpDebugServerClient ( _process . DebugPort , this ) ;
101106 tcpConnector . Connect ( ) ;
102107 service = tcpConnector ;
103108 }
@@ -108,7 +113,7 @@ public override void Launch(Response response, dynamic args)
108113 {
109114 _process . HandleDisconnect ( true ) ;
110115 _process = null ;
111- SessionLog . WriteLine ( e . ToString ( ) ) ;
116+ Log . Error ( e , "Can't connect to debug server" ) ;
112117 SendErrorResponse ( response , 4550 , "Can't connect: " + e . ToString ( ) ) ;
113118 return ;
114119 }
@@ -119,28 +124,28 @@ public override void Launch(Response response, dynamic args)
119124
120125 public override void Attach ( Response response , dynamic arguments )
121126 {
122- SessionLog . WriteLine ( "Attach command received" ) ;
127+ LogCommandReceived ( ) ;
123128 _process . DebugPort = getInt ( arguments , "debugPort" , 2801 ) ;
124129 _process . ProcessExited += ( s , e ) =>
125130 {
126- SessionLog . WriteLine ( "_process exited") ;
131+ Log . Information ( "Debuggee has exited") ;
127132 SendEvent ( new TerminatedEvent ( ) ) ;
128133 } ;
129134
130135 try
131136 {
132137 IDebuggerService service ;
133- var tcpConnector = new TcpDebugConnector ( _process . DebugPort , this ) ;
138+ var tcpConnector = new TcpDebugServerClient ( _process . DebugPort , this ) ;
134139 tcpConnector . Connect ( ) ;
135- SessionLog . WriteLine ( $ "Connected to host on port { _process . DebugPort } " ) ;
140+ Log . Debug ( "Connected to debuggee on port {Port}" , _process . DebugPort ) ;
136141 service = tcpConnector ;
137142
138143 _process . SetConnection ( service ) ;
139144 _process . InitAttached ( ) ;
140145 }
141146 catch ( Exception e )
142147 {
143- SessionLog . WriteLine ( e . ToString ( ) ) ;
148+ Log . Error ( e , "Can't connect debuggee" ) ;
144149 SendErrorResponse ( response , 4550 , "Can't connect: " + e . ToString ( ) ) ;
145150 return ;
146151 }
@@ -150,21 +155,18 @@ public override void Attach(Response response, dynamic arguments)
150155
151156 public override void Disconnect ( Response response , dynamic arguments )
152157 {
158+ LogCommandReceived ( arguments ) ;
153159 bool terminateDebuggee = arguments . terminateDebuggee == true ;
154160
155161 _process . HandleDisconnect ( terminateDebuggee ) ;
156- if ( terminateDebuggee )
157- {
158- // Для более быстрого прерывания сессии
159- SendEvent ( new TerminatedEvent ( ) ) ;
160- }
161162 SendResponse ( response ) ;
162163 }
163164
164165 public override void SetBreakpoints ( Response response , dynamic arguments )
165166 {
166- SessionLog . WriteLine ( $ "Set breakpoints command accepted { arguments } ") ;
167-
167+ LogCommandReceived ( ) ;
168+ Log . Debug ( "Breakpoints: {Data}" , JsonConvert . SerializeObject ( arguments ) ) ;
169+
168170 if ( ( bool ) arguments . sourceModified )
169171 {
170172 if ( _startupPerformed )
@@ -224,40 +226,42 @@ private string NormalizeDriveLetter(string path)
224226
225227 public void ThreadStopped ( int threadId , ThreadStopReason reason )
226228 {
227- SessionLog . WriteLine ( "thread stopped" ) ;
229+ LogEventOccured ( ) ;
228230 _framesHandles . Reset ( ) ;
229231 _variableHandles . Reset ( ) ;
230232 SendEvent ( new StoppedEvent ( threadId , reason . ToString ( ) ) ) ;
231233 }
232234
233235 public void ProcessExited ( int exitCode )
234236 {
235- SessionLog . WriteLine ( "Exited event recieved" ) ;
237+ LogEventOccured ( ) ;
236238 SendEvent ( new ExitedEvent ( exitCode ) ) ;
237239 }
238240
239241 public override void ConfigurationDone ( Response response , dynamic args )
240242 {
241243 if ( _process == null )
242244 {
243- SessionLog . WriteLine ( "Config Done. Process is not started" ) ;
245+ Log . Debug ( "Config Done. Process is not started" ) ;
244246 SendResponse ( response ) ;
245247 return ;
246248 }
247- SessionLog . WriteLine ( "Config Done. Process is started" ) ;
249+ Log . Debug ( "Config Done. Process is started, sending Execute " ) ;
248250 _process . BeginExecution ( - 1 ) ;
249251 _startupPerformed = true ;
250252 SendResponse ( response ) ;
251253 }
252254
253255 public override void Continue ( Response response , dynamic arguments )
254256 {
257+ LogCommandReceived ( ) ;
255258 SendResponse ( response ) ;
256259 _process . BeginExecution ( - 1 ) ;
257260 }
258261
259262 public override void Next ( Response response , dynamic arguments )
260263 {
264+ LogCommandReceived ( ) ;
261265 SendResponse ( response ) ;
262266 lock ( _process )
263267 {
@@ -271,6 +275,7 @@ public override void Next(Response response, dynamic arguments)
271275
272276 public override void StepIn ( Response response , dynamic arguments )
273277 {
278+ LogCommandReceived ( ) ;
274279 SendResponse ( response ) ;
275280 lock ( _process )
276281 {
@@ -283,6 +288,7 @@ public override void StepIn(Response response, dynamic arguments)
283288
284289 public override void StepOut ( Response response , dynamic arguments )
285290 {
291+ LogCommandReceived ( ) ;
286292 SendResponse ( response ) ;
287293 lock ( _process )
288294 {
@@ -295,13 +301,13 @@ public override void StepOut(Response response, dynamic arguments)
295301
296302 public override void Pause ( Response response , dynamic arguments )
297303 {
304+ LogCommandReceived ( ) ;
298305 throw new NotImplementedException ( ) ;
299306 }
300307
301308 public override void StackTrace ( Response response , dynamic arguments )
302309 {
303- SessionLog . WriteLine ( "Stacktrace request accepted" ) ;
304- SessionLog . WriteLine ( arguments . ToString ( ) ) ;
310+ LogCommandReceived ( ) ;
305311 var firstFrameIdx = ( int ? ) arguments . startFrame ?? 0 ;
306312 var limit = ( int ? ) arguments . levels ?? 0 ;
307313 var threadId = ( int ) arguments . threadId ;
@@ -321,6 +327,7 @@ public override void StackTrace(Response response, dynamic arguments)
321327
322328 public override void Scopes ( Response response , dynamic arguments )
323329 {
330+ LogCommandReceived ( ) ;
324331 int frameId = getInt ( arguments , "frameId" ) ;
325332 var frame = _framesHandles . Get ( frameId , null ) ;
326333 if ( frame == null )
@@ -332,13 +339,12 @@ public override void Scopes(Response response, dynamic arguments)
332339 var frameVariablesHandle = _variableHandles . Create ( frame ) ;
333340 var localScope = new Scope ( "Локальные переменные" , frameVariablesHandle ) ;
334341 SendResponse ( response , new ScopesResponseBody ( new Scope [ ] { localScope } ) ) ;
335- SessionLog . WriteLine ( "Scopes done" ) ;
336342 }
337343
338344 public override void Variables ( Response response , dynamic arguments )
339345 {
346+ LogCommandReceived ( ) ;
340347 int varsHandle = getInt ( arguments , "variablesReference" ) ;
341- SessionLog . WriteLine ( $ "variables request { varsHandle } ") ;
342348 var variables = _variableHandles . Get ( varsHandle , null ) ;
343349 if ( variables == null )
344350 {
@@ -371,20 +377,20 @@ public override void Variables(Response response, dynamic arguments)
371377
372378 public override void Threads ( Response response , dynamic arguments )
373379 {
380+ LogCommandReceived ( ) ;
374381 var threads = new List < VSCodeDebug . Thread > ( ) ;
375- SessionLog . WriteLine ( "Threads request accepted" ) ;
376382 var processThreads = _process . GetThreads ( ) ;
377383 for ( int i = 0 ; i < processThreads . Length ; i ++ )
378384 {
379385 threads . Add ( new VSCodeDebug . Thread ( processThreads [ i ] , $ "Thread { processThreads [ i ] } ") ) ;
380386 }
381387
382388 SendResponse ( response , new ThreadsResponseBody ( threads ) ) ;
383- SessionLog . WriteLine ( "Threads processed" ) ;
384389 }
385390
386391 public override void Evaluate ( Response response , dynamic arguments )
387392 {
393+ LogCommandReceived ( ) ;
388394 // expression, frameId, context
389395 int frameId = getInt ( arguments , "frameId" ) ;
390396 var frame = _framesHandles . Get ( frameId , null ) ;
@@ -396,6 +402,8 @@ public override void Evaluate(Response response, dynamic arguments)
396402
397403 var expression = ( string ) arguments . expression ;
398404 var context = ( string ) arguments . context ;
405+
406+ Log . Debug ( "Evaluate {Expression} in {Context}" , expression , context ) ;
399407
400408 int id = - 1 ;
401409 OneScript . DebugProtocol . Variable evalResult ;
@@ -449,12 +457,22 @@ private static int getInt(dynamic container, string propertyName, int dflt = 0)
449457 return dflt ;
450458 }
451459
452-
453- private void RequestDummy ( string message , Response response , dynamic arguments )
460+ protected override void OnRequestError ( Exception e )
454461 {
455- SessionLog . WriteLine ( message ) ;
456- SendResponse ( response , arguments ) ;
462+ Log . Error ( e , "Unhandled request processing error" ) ;
457463 }
458464
465+ private void LogCommandReceived ( dynamic args = null , [ CallerMemberName ] string commandName = "" )
466+ {
467+ if ( args == null )
468+ Log . Debug ( "Command received {Command}" , commandName ) ;
469+ else
470+ Log . Debug ( "Command received {Command}: {Args}" , commandName , JsonConvert . SerializeObject ( args ) ) ;
471+ }
472+
473+ private void LogEventOccured ( [ CallerMemberName ] string eventName = "" )
474+ {
475+ Log . Debug ( "Event occured {Event}" , eventName ) ;
476+ }
459477 }
460478}
0 commit comments