Skip to content

Commit edc93df

Browse files
committed
Add managed thread ID to extractor log messages
1 parent dfd7f1e commit edc93df

File tree

14 files changed

+85
-107
lines changed

14 files changed

+85
-107
lines changed

csharp/autobuilder/Semmle.Autobuild.Shared/Autobuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ protected string RequireEnvironmentVariable(string name)
267267

268268
protected DiagnosticClassifier DiagnosticClassifier { get; }
269269

270-
private readonly ILogger logger = new ConsoleLogger(Verbosity.Info);
270+
private readonly ILogger logger = new ConsoleLogger(Verbosity.Info, logThreadId: false);
271271

272272
private readonly IDiagnosticsWriter diagnostics;
273273

csharp/autobuilder/Semmle.Autobuild.Shared/BuildActions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ int IBuildActions.RunProcess(string cmd, string args, string? workingDirectory,
237237
int IBuildActions.RunProcess(string cmd, string args, string? workingDirectory, IDictionary<string, string>? environment, out IList<string> stdOut)
238238
{
239239
var pi = GetProcessStartInfo(cmd, args, workingDirectory, environment, true);
240-
return pi.ReadOutput(out stdOut);
240+
return pi.ReadOutput(out stdOut, printToConsole: false);
241241
}
242242

243243
void IBuildActions.DirectoryDelete(string dir, bool recursive) => Directory.Delete(dir, recursive);

csharp/extractor/Semmle.Extraction.CIL.Driver/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static void Main(string[] args)
3838
}
3939

4040
var options = new ExtractorOptions(args);
41-
using var logger = new ConsoleLogger(options.Verbosity);
41+
using var logger = new ConsoleLogger(options.Verbosity, logThreadId: false);
4242

4343
var actions = options.AssembliesToExtract
4444
.Select(asm => asm.Filename)

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.cs

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ private void GenerateSourceFilesFromWebViews(List<FileInfo> allFiles)
219219
}
220220
}
221221

222-
public DependencyManager(string srcDir) : this(srcDir, DependencyOptions.Default, new ConsoleLogger(Verbosity.Info)) { }
222+
public DependencyManager(string srcDir) : this(srcDir, DependencyOptions.Default, new ConsoleLogger(Verbosity.Info, logThreadId: true)) { }
223223

224224
private IEnumerable<FileInfo> GetAllFiles()
225225
{
@@ -430,8 +430,8 @@ private void AnalyseProject(FileInfo project)
430430

431431
}
432432

433-
private bool RestoreProject(string project, out string stdout, string? pathToNugetConfig = null) =>
434-
dotnet.RestoreProjectToDirectory(project, packageDirectory.DirInfo.FullName, out stdout, pathToNugetConfig);
433+
private bool RestoreProject(string project, string? pathToNugetConfig = null) =>
434+
dotnet.RestoreProjectToDirectory(project, packageDirectory.DirInfo.FullName, pathToNugetConfig);
435435

436436
private bool RestoreSolution(string solution, out IEnumerable<string> projects) =>
437437
dotnet.RestoreSolutionToDirectory(solution, packageDirectory.DirInfo.FullName, out projects);
@@ -454,25 +454,14 @@ private IEnumerable<string> RestoreSolutions(IEnumerable<string> solutions) =>
454454
/// <summary>
455455
/// Executes `dotnet restore` on all projects in projects.
456456
/// This is done in parallel for performance reasons.
457-
/// To ensure that output is not interleaved, the output of each
458-
/// restore is collected and printed.
459457
/// </summary>
460458
/// <param name="projects">A list of paths to project files.</param>
461459
private void RestoreProjects(IEnumerable<string> projects)
462460
{
463-
var stdoutLines = projects
464-
.AsParallel()
465-
.WithDegreeOfParallelism(options.Threads)
466-
.Select(project =>
467-
{
468-
RestoreProject(project, out var stdout);
469-
return stdout;
470-
})
471-
.ToList();
472-
foreach (var line in stdoutLines)
461+
Parallel.ForEach(projects, new ParallelOptions { MaxDegreeOfParallelism = options.Threads }, project =>
473462
{
474-
Console.WriteLine(line);
475-
}
463+
RestoreProject(project);
464+
});
476465
}
477466

478467
private void DownloadMissingPackages(List<FileInfo> allFiles)
@@ -500,38 +489,29 @@ private void DownloadMissingPackages(List<FileInfo> allFiles)
500489
.Select(d => Path.GetFileName(d).ToLowerInvariant());
501490
var notYetDownloadedPackages = fileContent.AllPackages.Except(alreadyDownloadedPackages);
502491

503-
var stdoutLines = notYetDownloadedPackages
504-
.AsParallel()
505-
.WithDegreeOfParallelism(options.Threads)
506-
.Select(package =>
507-
{
508-
progressMonitor.NugetInstall(package);
509-
using var tempDir = new TemporaryDirectory(ComputeTempDirectory(package));
510-
var success = dotnet.New(tempDir.DirInfo.FullName, out var stdout1);
511-
if (!success)
512-
{
513-
return new[] { stdout1 };
514-
}
515-
516-
success = dotnet.AddPackage(tempDir.DirInfo.FullName, package, out var stdout2);
517-
if (!success)
518-
{
519-
return new[] { stdout1, stdout2 };
520-
}
521-
522-
success = RestoreProject(tempDir.DirInfo.FullName, out var stdout3, nugetConfig);
523-
// TODO: the restore might fail, we could retry with a prerelease (*-* instead of *) version of the package.
524-
if (!success)
525-
{
526-
progressMonitor.FailedToRestoreNugetPackage(package);
527-
}
528-
return new[] { stdout1, stdout2, stdout3 };
529-
})
530-
.ToList();
531-
foreach (var line in stdoutLines.SelectMany(l => l))
492+
Parallel.ForEach(notYetDownloadedPackages, new ParallelOptions { MaxDegreeOfParallelism = options.Threads }, package =>
532493
{
533-
Console.WriteLine(line);
534-
}
494+
progressMonitor.NugetInstall(package);
495+
using var tempDir = new TemporaryDirectory(ComputeTempDirectory(package));
496+
var success = dotnet.New(tempDir.DirInfo.FullName);
497+
if (!success)
498+
{
499+
return;
500+
}
501+
502+
success = dotnet.AddPackage(tempDir.DirInfo.FullName, package);
503+
if (!success)
504+
{
505+
return;
506+
}
507+
508+
success = RestoreProject(tempDir.DirInfo.FullName, nugetConfig);
509+
// TODO: the restore might fail, we could retry with a prerelease (*-* instead of *) version of the package.
510+
if (!success)
511+
{
512+
progressMonitor.FailedToRestoreNugetPackage(package);
513+
}
514+
});
535515
}
536516

537517
private void AnalyseSolutions(IEnumerable<string> solutions)

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,18 @@ private void Info()
3737
}
3838
}
3939

40-
private bool RunCommand(string args, out string stdout)
41-
{
42-
var success = dotnetCliInvoker.RunCommand(args, out var output);
43-
stdout = string.Join("\n", output);
44-
return success;
45-
}
46-
4740
private static string GetRestoreArgs(string projectOrSolutionFile, string packageDirectory) =>
4841
$"restore --no-dependencies \"{projectOrSolutionFile}\" --packages \"{packageDirectory}\" /p:DisableImplicitNuGetFallbackFolder=true";
4942

50-
public bool RestoreProjectToDirectory(string projectFile, string packageDirectory, out string stdout, string? pathToNugetConfig = null)
43+
public bool RestoreProjectToDirectory(string projectFile, string packageDirectory, string? pathToNugetConfig = null)
5144
{
5245
var args = GetRestoreArgs(projectFile, packageDirectory);
5346
if (pathToNugetConfig != null)
5447
{
5548
args += $" --configfile \"{pathToNugetConfig}\"";
5649
}
5750

58-
return RunCommand(args, out stdout);
51+
return dotnetCliInvoker.RunCommand(args);
5952
}
6053

6154
public bool RestoreSolutionToDirectory(string solutionFile, string packageDirectory, out IEnumerable<string> projects)
@@ -76,16 +69,16 @@ public bool RestoreSolutionToDirectory(string solutionFile, string packageDirect
7669
return false;
7770
}
7871

79-
public bool New(string folder, out string stdout)
72+
public bool New(string folder)
8073
{
8174
var args = $"new console --no-restore --output \"{folder}\"";
82-
return RunCommand(args, out stdout);
75+
return dotnetCliInvoker.RunCommand(args);
8376
}
8477

85-
public bool AddPackage(string folder, string package, out string stdout)
78+
public bool AddPackage(string folder, string package)
8679
{
8780
var args = $"add \"{folder}\" package \"{package}\" --no-restore";
88-
return RunCommand(args, out stdout);
81+
return dotnetCliInvoker.RunCommand(args);
8982
}
9083

9184
public IList<string> GetListedRuntimes() => GetListed("--list-runtimes", "runtime");
@@ -94,9 +87,8 @@ public bool AddPackage(string folder, string package, out string stdout)
9487

9588
private IList<string> GetListed(string args, string artifact)
9689
{
97-
if (dotnetCliInvoker.RunCommand(args, out IList<string> artifacts))
90+
if (dotnetCliInvoker.RunCommand(args, out var artifacts))
9891
{
99-
progressMonitor.LogInfo($"Found {artifact}s: {string.Join("\n", artifacts)}");
10092
return artifacts;
10193
}
10294
return new List<string>();

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNetCliInvoker.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@ public DotNetCliInvoker(ProgressMonitor progressMonitor, string exec)
1919
this.Exec = exec;
2020
}
2121

22-
private ProcessStartInfo MakeDotnetStartInfo(string args, bool redirectStandardOutput)
22+
private ProcessStartInfo MakeDotnetStartInfo(string args)
2323
{
2424
var startInfo = new ProcessStartInfo(Exec, args)
2525
{
2626
UseShellExecute = false,
27-
RedirectStandardOutput = redirectStandardOutput
27+
RedirectStandardOutput = true
2828
};
2929
// Set the .NET CLI language to English to avoid localized output.
3030
startInfo.EnvironmentVariables["DOTNET_CLI_UI_LANGUAGE"] = "en";
3131
return startInfo;
3232
}
3333

34-
private bool RunCommandAux(string args, bool redirectStandardOutput, out IList<string> output)
34+
private bool RunCommandAux(string args, out IList<string> output)
3535
{
3636
progressMonitor.RunningProcess($"{Exec} {args}");
37-
var pi = MakeDotnetStartInfo(args, redirectStandardOutput);
38-
var exitCode = pi.ReadOutput(out output);
37+
var pi = MakeDotnetStartInfo(args);
38+
var exitCode = pi.ReadOutput(out output, true);
3939
if (exitCode != 0)
4040
{
4141
progressMonitor.CommandFailed(Exec, args, exitCode);
@@ -45,9 +45,9 @@ private bool RunCommandAux(string args, bool redirectStandardOutput, out IList<s
4545
}
4646

4747
public bool RunCommand(string args) =>
48-
RunCommandAux(args, redirectStandardOutput: false, out _);
48+
RunCommandAux(args, out _);
4949

5050
public bool RunCommand(string args, out IList<string> output) =>
51-
RunCommandAux(args, redirectStandardOutput: true, out output);
51+
RunCommandAux(args, out output);
5252
}
5353
}

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/IDotNet.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
44
{
55
internal interface IDotNet
66
{
7-
bool RestoreProjectToDirectory(string project, string directory, out string stdout, string? pathToNugetConfig = null);
7+
bool RestoreProjectToDirectory(string project, string directory, string? pathToNugetConfig = null);
88
bool RestoreSolutionToDirectory(string solutionFile, string packageDirectory, out IEnumerable<string> projects);
9-
bool New(string folder, out string stdout);
10-
bool AddPackage(string folder, string package, out string stdout);
9+
bool New(string folder);
10+
bool AddPackage(string folder, string package);
1111
IList<string> GetListedRuntimes();
1212
IList<string> GetListedSdks();
1313
bool Exec(string execArgs);

csharp/extractor/Semmle.Extraction.CSharp.Standalone/Extractor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public static ExitCode Run(Options options)
119119
var stopwatch = new Stopwatch();
120120
stopwatch.Start();
121121

122-
using var logger = new ConsoleLogger(options.Verbosity);
122+
using var logger = new ConsoleLogger(options.Verbosity, logThreadId: true);
123123
logger.Log(Severity.Info, "Running C# standalone extractor");
124124
using var a = new Analysis(logger, options);
125125
var sourceFileCount = a.Extraction.Sources.Count;

csharp/extractor/Semmle.Extraction.CSharp.Standalone/Options.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Semmle.Util;
33
using Semmle.Util.Logging;
44
using Semmle.Extraction.CSharp.DependencyFetching;
5+
using System;
56

67
namespace Semmle.Extraction.CSharp.Standalone
78
{
@@ -64,15 +65,15 @@ public override bool HandleArgument(string arg)
6465
var fi = new FileInfo(dependencies.SolutionFile);
6566
if (!fi.Exists)
6667
{
67-
System.Console.WriteLine("Error: The solution {0} does not exist", fi.FullName);
68+
System.Console.WriteLine($"[{Environment.CurrentManagedThreadId:D3}] Error: The solution {fi.FullName} does not exist");
6869
Errors = true;
6970
}
7071
return true;
7172
}
7273

7374
public override void InvalidArgument(string argument)
7475
{
75-
System.Console.WriteLine($"Error: Invalid argument {argument}");
76+
System.Console.WriteLine($"[{Environment.CurrentManagedThreadId:D3}] Error: Invalid argument {argument}");
7677
Errors = true;
7778
}
7879

csharp/extractor/Semmle.Extraction.CSharp/Extractor/Extractor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ public static void SetInvariantCulture()
7171

7272
public static ILogger MakeLogger(Verbosity verbosity, bool includeConsole)
7373
{
74-
var fileLogger = new FileLogger(verbosity, GetCSharpLogPath());
74+
var fileLogger = new FileLogger(verbosity, GetCSharpLogPath(), logThreadId: true);
7575
return includeConsole
76-
? new CombinedLogger(new ConsoleLogger(verbosity), fileLogger)
76+
? new CombinedLogger(new ConsoleLogger(verbosity, logThreadId: true), fileLogger)
7777
: (ILogger)fileLogger;
7878
}
7979

0 commit comments

Comments
 (0)