Skip to content

Commit 01b8950

Browse files
authored
Merge pull request #15424 from tamasvajk/standalone/logging
C#: Improve log messages in buildless mode + some cleanup/refactoring
2 parents 930f1b5 + 199b057 commit 01b8950

22 files changed

+395
-524
lines changed

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

+19-25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using System.IO;
33
using System.Linq;
4+
using Semmle.Util.Logging;
45

56
namespace Semmle.Extraction.CSharp.DependencyFetching
67
{
@@ -18,25 +19,26 @@ internal class AssemblyCache
1819
/// Paths to search. Directories are searched recursively. Files are added directly to the
1920
/// assembly cache.
2021
/// </param>
21-
/// <param name="progressMonitor">Callback for progress.</param>
22-
public AssemblyCache(IEnumerable<string> paths, IEnumerable<string> frameworkPaths, ProgressMonitor progressMonitor)
22+
/// <param name="logger">Callback for progress.</param>
23+
public AssemblyCache(IEnumerable<string> paths, IEnumerable<string> frameworkPaths, ILogger logger)
2324
{
25+
this.logger = logger;
2426
foreach (var path in paths)
2527
{
2628
if (File.Exists(path))
2729
{
28-
pendingDllsToIndex.Enqueue(path);
30+
dllsToIndex.Add(path);
2931
continue;
3032
}
3133

3234
if (Directory.Exists(path))
3335
{
34-
progressMonitor.FindingFiles(path);
36+
logger.LogInfo($"Finding reference DLLs in {path}...");
3537
AddReferenceDirectory(path);
3638
}
3739
else
3840
{
39-
progressMonitor.LogInfo("AssemblyCache: Path not found: " + path);
41+
logger.LogInfo("AssemblyCache: Path not found: " + path);
4042
}
4143
}
4244
IndexReferences(frameworkPaths);
@@ -52,7 +54,7 @@ private void AddReferenceDirectory(string dir)
5254
{
5355
foreach (var dll in new DirectoryInfo(dir).EnumerateFiles("*.dll", SearchOption.AllDirectories))
5456
{
55-
pendingDllsToIndex.Enqueue(dll.FullName);
57+
dllsToIndex.Add(dll.FullName);
5658
}
5759
}
5860

@@ -62,12 +64,16 @@ private void AddReferenceDirectory(string dir)
6264
/// </summary>
6365
private void IndexReferences(IEnumerable<string> frameworkPaths)
6466
{
67+
logger.LogInfo($"Indexing {dllsToIndex.Count} assemblies...");
68+
6569
// Read all of the files
66-
foreach (var filename in pendingDllsToIndex)
70+
foreach (var filename in dllsToIndex)
6771
{
6872
IndexReference(filename);
6973
}
7074

75+
logger.LogInfo($"Read {assemblyInfoByFileName.Count} assembly infos");
76+
7177
foreach (var info in assemblyInfoByFileName.Values
7278
.OrderBy(info => info.Name)
7379
.OrderAssemblyInfosByPreference(frameworkPaths))
@@ -83,25 +89,16 @@ private void IndexReference(string filename)
8389
{
8490
try
8591
{
92+
logger.LogDebug($"Reading assembly info from {filename}");
8693
var info = AssemblyInfo.ReadFromFile(filename);
8794
assemblyInfoByFileName[filename] = info;
8895
}
8996
catch (AssemblyLoadException)
9097
{
91-
failedAssemblyInfoFileNames.Add(filename);
98+
logger.LogInfo($"Couldn't read assembly info from {filename}");
9299
}
93100
}
94101

95-
/// <summary>
96-
/// The number of DLLs which are assemblies.
97-
/// </summary>
98-
public int AssemblyCount => assemblyInfoByFileName.Count;
99-
100-
/// <summary>
101-
/// The number of DLLs which weren't assemblies. (E.g. C++).
102-
/// </summary>
103-
public int NonAssemblyCount => failedAssemblyInfoFileNames.Count;
104-
105102
/// <summary>
106103
/// Given an assembly id, determine its full info.
107104
/// </summary>
@@ -113,8 +110,7 @@ public AssemblyInfo ResolveReference(string id)
113110
if (failedAssemblyInfoIds.Contains(id))
114111
throw new AssemblyLoadException();
115112

116-
string assemblyName;
117-
(id, assemblyName) = AssemblyInfo.ComputeSanitizedAssemblyInfo(id);
113+
(id, var assemblyName) = AssemblyInfo.ComputeSanitizedAssemblyInfo(id);
118114

119115
// Look up the id in our references map.
120116
if (assemblyInfoById.TryGetValue(id, out var result))
@@ -164,17 +160,15 @@ public AssemblyInfo GetAssemblyInfo(string filepath)
164160
throw new AssemblyLoadException();
165161
}
166162

167-
private readonly Queue<string> pendingDllsToIndex = new Queue<string>();
163+
private readonly List<string> dllsToIndex = new List<string>();
168164

169165
private readonly Dictionary<string, AssemblyInfo> assemblyInfoByFileName = new Dictionary<string, AssemblyInfo>();
170166

171-
// List of DLLs which are not assemblies.
172-
// We probably don't need to keep this
173-
private readonly List<string> failedAssemblyInfoFileNames = new List<string>();
174-
175167
// Map from assembly id (in various formats) to the full info.
176168
private readonly Dictionary<string, AssemblyInfo> assemblyInfoById = new Dictionary<string, AssemblyInfo>();
177169

178170
private readonly HashSet<string> failedAssemblyInfoIds = new HashSet<string>();
171+
172+
private readonly ILogger logger;
179173
}
180174
}

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

-15
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,6 @@ private AssemblyInfo(string filename, string name, Version version, string cultu
120120
NetCoreVersion = netCoreVersion;
121121
}
122122

123-
/// <summary>
124-
/// Get AssemblyInfo from a loaded Assembly.
125-
/// </summary>
126-
/// <param name="assembly">The assembly.</param>
127-
/// <returns>Info about the assembly.</returns>
128-
public static AssemblyInfo MakeFromAssembly(Assembly assembly)
129-
{
130-
if (assembly.FullName is null)
131-
{
132-
throw new InvalidOperationException("Assembly with empty full name is not expected.");
133-
}
134-
135-
return new AssemblyInfo(assembly.FullName, assembly.Location);
136-
}
137-
138123
/// <summary>
139124
/// Returns the id and name of the assembly that would be created from the received id.
140125
/// </summary>

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

+15-14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using Newtonsoft.Json.Linq;
77
using Semmle.Util;
8+
using Semmle.Util.Logging;
89

910
namespace Semmle.Extraction.CSharp.DependencyFetching
1011
{
@@ -13,11 +14,11 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
1314
/// </summary>
1415
internal class Assets
1516
{
16-
private readonly ProgressMonitor progressMonitor;
17+
private readonly ILogger logger;
1718

18-
internal Assets(ProgressMonitor progressMonitor)
19+
internal Assets(ILogger logger)
1920
{
20-
this.progressMonitor = progressMonitor;
21+
this.logger = logger;
2122
}
2223

2324
/// <summary>
@@ -35,7 +36,7 @@ private record class ReferenceInfo(string? Type, Dictionary<string, object>? Com
3536

3637
/// <summary>
3738
/// Add the package dependencies from the assets file to dependencies.
38-
///
39+
///
3940
/// Parse a part of the JSon assets file and add the paths
4041
/// to the dependencies required for compilation (and collect
4142
/// information about used packages).
@@ -60,7 +61,7 @@ private record class ReferenceInfo(string? Type, Dictionary<string, object>? Com
6061
/// }
6162
/// }
6263
/// }
63-
///
64+
///
6465
/// Adds the following dependencies
6566
/// Paths: {
6667
/// "castle.core/4.4.1/lib/netstandard1.5/Castle.Core.dll",
@@ -85,7 +86,7 @@ private void AddPackageDependencies(JObject json, DependencyContainer dependenci
8586

8687
if (references is null)
8788
{
88-
progressMonitor.LogDebug("No references found in the targets section in the assets file.");
89+
logger.LogDebug("No references found in the targets section in the assets file.");
8990
return;
9091
}
9192

@@ -157,7 +158,7 @@ private void AddFrameworkDependencies(JObject json, DependencyContainer dependen
157158

158159
if (frameworks is null)
159160
{
160-
progressMonitor.LogDebug("No framework section in assets.json.");
161+
logger.LogDebug("No framework section in assets.json.");
161162
return;
162163
}
163164

@@ -171,7 +172,7 @@ private void AddFrameworkDependencies(JObject json, DependencyContainer dependen
171172

172173
if (references is null)
173174
{
174-
progressMonitor.LogDebug("No framework references in assets.json.");
175+
logger.LogDebug("No framework references in assets.json.");
175176
return;
176177
}
177178

@@ -196,12 +197,12 @@ public bool TryParse(string json, DependencyContainer dependencies)
196197
}
197198
catch (Exception e)
198199
{
199-
progressMonitor.LogDebug($"Failed to parse assets file (unexpected error): {e.Message}");
200+
logger.LogDebug($"Failed to parse assets file (unexpected error): {e.Message}");
200201
return false;
201202
}
202203
}
203204

204-
private static bool TryReadAllText(string path, ProgressMonitor progressMonitor, [NotNullWhen(returnValue: true)] out string? content)
205+
private static bool TryReadAllText(string path, ILogger logger, [NotNullWhen(returnValue: true)] out string? content)
205206
{
206207
try
207208
{
@@ -210,19 +211,19 @@ private static bool TryReadAllText(string path, ProgressMonitor progressMonitor,
210211
}
211212
catch (Exception e)
212213
{
213-
progressMonitor.LogInfo($"Failed to read assets file '{path}': {e.Message}");
214+
logger.LogInfo($"Failed to read assets file '{path}': {e.Message}");
214215
content = null;
215216
return false;
216217
}
217218
}
218219

219-
public static DependencyContainer GetCompilationDependencies(ProgressMonitor progressMonitor, IEnumerable<string> assets)
220+
public static DependencyContainer GetCompilationDependencies(ILogger logger, IEnumerable<string> assets)
220221
{
221-
var parser = new Assets(progressMonitor);
222+
var parser = new Assets(logger);
222223
var dependencies = new DependencyContainer();
223224
assets.ForEach(asset =>
224225
{
225-
if (TryReadAllText(asset, progressMonitor, out var json))
226+
if (TryReadAllText(asset, logger, out var json))
226227
{
227228
parser.TryParse(json, dependencies);
228229
}

0 commit comments

Comments
 (0)