Skip to content

Commit cf58575

Browse files
committed
Add logging of Alire interactions
1 parent 0b7fb1d commit cf58575

File tree

1 file changed

+76
-45
lines changed

1 file changed

+76
-45
lines changed

source/ada/lsp-alire.adb

Lines changed: 76 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ with Ada.Streams;
1919
with GNAT.OS_Lib;
2020
with GNATCOLL.VFS;
2121

22+
with LSP.GNATCOLL_Tracers;
2223
with VSS.Stream_Element_Vectors;
2324
with VSS.Strings.Conversions;
2425
with VSS.Strings.Converters.Decoders;
@@ -34,27 +35,30 @@ with Spawn.String_Vectors;
3435

3536
package body LSP.Alire is
3637

38+
Trace : constant GNATCOLL_Tracers.Tracer :=
39+
GNATCOLL_Tracers.Create ("ALS.ALIRE");
40+
3741
Fallback_Msg : constant VSS.Strings.Virtual_String :=
3842
"falling back to other methods to load a project";
3943

4044
type Process_Listener is limited
41-
new Spawn.Process_Listeners.Process_Listener with record
45+
new Spawn.Process_Listeners.Process_Listener
46+
with record
4247
Process : Spawn.Processes.Process;
4348
Stdout : VSS.Stream_Element_Vectors.Stream_Element_Vector;
4449
Stderr : VSS.Stream_Element_Vectors.Stream_Element_Vector;
4550
Error : Integer := 0; -- Error_Occurred argument
4651
Text : VSS.Strings.Virtual_String; -- Stdout as a text
47-
end record;
52+
end record;
4853

49-
overriding procedure Standard_Output_Available
50-
(Self : in out Process_Listener);
54+
overriding
55+
procedure Standard_Output_Available (Self : in out Process_Listener);
5156

52-
overriding procedure Standard_Error_Available
53-
(Self : in out Process_Listener);
57+
overriding
58+
procedure Standard_Error_Available (Self : in out Process_Listener);
5459

55-
overriding procedure Error_Occurred
56-
(Self : in out Process_Listener;
57-
Error : Integer);
60+
overriding
61+
procedure Error_Occurred (Self : in out Process_Listener; Error : Integer);
5862

5963
procedure Start_Alire
6064
(ALR : String;
@@ -71,9 +75,9 @@ package body LSP.Alire is
7175
-- Error_Occurred --
7276
--------------------
7377

74-
overriding procedure Error_Occurred
75-
(Self : in out Process_Listener;
76-
Error : Integer) is
78+
overriding
79+
procedure Error_Occurred (Self : in out Process_Listener; Error : Integer)
80+
is
7781
begin
7882
Self.Error := Error;
7983
end Error_Occurred;
@@ -83,9 +87,9 @@ package body LSP.Alire is
8387
---------------
8488

8589
procedure Determine_Alire_Project
86-
(Root : String;
87-
Error : out VSS.Strings.Virtual_String;
88-
Project : out VSS.Strings.Virtual_String)
90+
(Root : String;
91+
Error : out VSS.Strings.Virtual_String;
92+
Project : out VSS.Strings.Virtual_String)
8993
is
9094
use type GNAT.OS_Lib.String_Access;
9195
use type VSS.Strings.Virtual_String;
@@ -100,12 +104,13 @@ package body LSP.Alire is
100104
VSS.Regular_Expressions.To_Regular_Expression
101105
(" +Project_File: ([^\n]+)");
102106

103-
Lines : VSS.String_Vectors.Virtual_String_Vector;
107+
Lines : VSS.String_Vectors.Virtual_String_Vector;
104108
begin
105109
Project.Clear;
106110

107111
if ALR = null then
108-
Error := "Alire executable ('alr') not found in PATH: " & Fallback_Msg;
112+
Error :=
113+
"Alire executable ('alr') not found in PATH: " & Fallback_Msg;
109114
return;
110115
end if;
111116

@@ -142,8 +147,9 @@ package body LSP.Alire is
142147
for Line of Lines loop
143148
declare
144149
-- We should keep copy of regexp subject string while we have a match
145-
Match : constant VSS.Regular_Expressions.Regular_Expression_Match :=
146-
Crate_Pattern.Match (Line);
150+
Match :
151+
constant VSS.Regular_Expressions.Regular_Expression_Match :=
152+
Crate_Pattern.Match (Line);
147153
begin
148154
if Match.Has_Match then
149155
Project := Match.Captured (1);
@@ -156,8 +162,9 @@ package body LSP.Alire is
156162
-- Next check if there is a Project_File line, take the first one.
157163
for Line of Lines loop
158164
declare
159-
Match : constant VSS.Regular_Expressions.Regular_Expression_Match
160-
:= Project_Pattern.Match (Line, Anchored);
165+
Match :
166+
constant VSS.Regular_Expressions.Regular_Expression_Match :=
167+
Project_Pattern.Match (Line, Anchored);
161168
begin
162169
if Match.Has_Match then
163170
Project := Match.Captured (1);
@@ -167,7 +174,8 @@ package body LSP.Alire is
167174
end loop;
168175

169176
if Project.Is_Empty then
170-
Error.Append ("No project file could be determined from the output of `alr show`:");
177+
Error.Append
178+
("No project file could be determined from the output of `alr show`:");
171179
for Line of Lines loop
172180
Error.Append (Line);
173181
end loop;
@@ -194,7 +202,7 @@ package body LSP.Alire is
194202
VSS.Regular_Expressions.To_Regular_Expression
195203
("export ([^=]+)=""([^\n]+)""");
196204

197-
Lines : VSS.String_Vectors.Virtual_String_Vector;
205+
Lines : VSS.String_Vectors.Virtual_String_Vector;
198206
begin
199207

200208
if ALR = null then
@@ -211,15 +219,18 @@ package body LSP.Alire is
211219

212220
for Line of Lines loop
213221
declare
214-
Match : constant VSS.Regular_Expressions.Regular_Expression_Match
215-
:= Export_Pattern.Match (Line, Anchored);
222+
Match :
223+
constant VSS.Regular_Expressions.Regular_Expression_Match :=
224+
Export_Pattern.Match (Line, Anchored);
216225
begin
217226
if Match.Has_Match then
218227
Environment.Insert
219-
(Key => VSS.Strings.Conversions.To_UTF_8_String
220-
(Match.Captured (1)),
221-
Value => VSS.Strings.Conversions.To_UTF_8_String
222-
(Match.Captured (2)));
228+
(Key =>
229+
VSS.Strings.Conversions.To_UTF_8_String
230+
(Match.Captured (1)),
231+
Value =>
232+
VSS.Strings.Conversions.To_UTF_8_String
233+
(Match.Captured (2)));
223234
end if;
224235
end;
225236
end loop;
@@ -241,18 +252,24 @@ package body LSP.Alire is
241252
use type Spawn.Process_Exit_Status;
242253
use type Spawn.Process_Status;
243254

244-
Item : aliased Process_Listener;
245-
Process : Spawn.Processes.Process renames Item.Process;
246-
Options : Spawn.String_Vectors.UTF_8_String_Vector;
247-
Decoder : VSS.Strings.Converters.Decoders.Virtual_String_Decoder;
248-
Text : VSS.Strings.Virtual_String;
255+
Item : aliased Process_Listener;
256+
Process : Spawn.Processes.Process renames Item.Process;
257+
Options : Spawn.String_Vectors.UTF_8_String_Vector;
258+
Decoder : VSS.Strings.Converters.Decoders.Virtual_String_Decoder;
259+
Text : VSS.Strings.Virtual_String;
249260
begin
250261
Options.Append (Option_1);
251262
Options.Append (Option_2);
252263
Process.Set_Arguments (Options);
253264
Process.Set_Working_Directory (Root);
254265
Process.Set_Program (ALR);
255266
Process.Set_Listener (Item'Unchecked_Access);
267+
268+
if Trace.Is_Active then
269+
Trace.Trace
270+
("(in " & Root & ") " & ALR & " " & Option_1 & " " & Option_2);
271+
end if;
272+
256273
Process.Start;
257274

258275
loop
@@ -261,6 +278,21 @@ package body LSP.Alire is
261278
exit when Item.Process.Status = Spawn.Not_Running;
262279
end loop;
263280

281+
if Trace.Is_Active then
282+
Trace.Trace
283+
("Alire exit code "
284+
& Item.Process.Exit_Code'Image
285+
& " with output:");
286+
287+
if not Item.Stdout.Is_Empty then
288+
Trace.Trace (Item.Stdout);
289+
end if;
290+
291+
if not Item.Stderr.Is_Empty then
292+
Trace.Trace (Item.Stderr);
293+
end if;
294+
end if;
295+
264296
Decoder.Initialize ("utf-8");
265297

266298
-- Decode output and check errors
@@ -309,15 +341,15 @@ package body LSP.Alire is
309341
(GNAT.OS_Lib.Errno_Message (Item.Error)));
310342
end if;
311343
end if;
344+
312345
end Start_Alire;
313346

314347
------------------------------
315348
-- Standard_Error_Available --
316349
------------------------------
317350

318-
overriding procedure Standard_Error_Available
319-
(Self : in out Process_Listener)
320-
is
351+
overriding
352+
procedure Standard_Error_Available (Self : in out Process_Listener) is
321353
use type Ada.Streams.Stream_Element_Count;
322354

323355
Data : Ada.Streams.Stream_Element_Array (1 .. 256);
@@ -338,9 +370,8 @@ package body LSP.Alire is
338370
-- Standard_Output_Available --
339371
-------------------------------
340372

341-
overriding procedure Standard_Output_Available
342-
(Self : in out Process_Listener)
343-
is
373+
overriding
374+
procedure Standard_Output_Available (Self : in out Process_Listener) is
344375
use type Ada.Streams.Stream_Element_Count;
345376

346377
Data : Ada.Streams.Stream_Element_Array (1 .. 256);
@@ -364,10 +395,9 @@ package body LSP.Alire is
364395
function Is_Alire_Crate
365396
(Client : LSP.Ada_Client_Capabilities.Client_Capability) return Boolean
366397
is
367-
Alire_TOML : constant GNATCOLL.VFS.Virtual_File :=
368-
(if Client.Root.Is_Empty then GNATCOLL.VFS.No_File
369-
else Client.Root_Directory.Create_From_Dir
370-
("alire.toml"));
398+
Alire_TOML : constant GNATCOLL.VFS.Virtual_File :=
399+
(if Client.Root.Is_Empty then GNATCOLL.VFS.No_File
400+
else Client.Root_Directory.Create_From_Dir ("alire.toml"));
371401
begin
372402
return Alire_TOML.Is_Regular_File;
373403
end Is_Alire_Crate;
@@ -379,7 +409,8 @@ package body LSP.Alire is
379409
function Should_Setup_Alire_Env
380410
(Client : LSP.Ada_Client_Capabilities.Client_Capability) return Boolean is
381411
begin
382-
return Is_Alire_Crate (Client)
412+
return
413+
Is_Alire_Crate (Client)
383414
and Spawn.Environments.System_Environment.Value ("ALIRE") /= "True";
384415
end Should_Setup_Alire_Env;
385416

0 commit comments

Comments
 (0)