Skip to content

Commit b8c8f52

Browse files
committed
C#: Introduce extractor option for logging verbosity
1 parent bb43272 commit b8c8f52

File tree

8 files changed

+118
-6
lines changed

8 files changed

+118
-6
lines changed

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

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

268268
protected DiagnosticClassifier DiagnosticClassifier { get; }
269269

270-
private readonly ILogger logger = new ConsoleLogger(Verbosity.Info, logThreadId: false);
270+
private readonly ILogger logger = new ConsoleLogger(
271+
VerbosityExtensions.ParseVerbosity(
272+
Environment.GetEnvironmentVariable("CODEQL_VERBOSITY"),
273+
logThreadId: false) ?? Verbosity.Info,
274+
logThreadId: false);
271275

272276
private readonly IDiagnosticsWriter diagnostics;
273277

csharp/codeql-extractor.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,21 @@ options:
4848
The default is 'true'.
4949
type: string
5050
pattern: "^(false|true)$"
51+
logging:
52+
title: Options pertaining to logging.
53+
type: object
54+
properties:
55+
verbosity:
56+
title: Extractor logging verbosity level.
57+
description: >
58+
Controls the level of verbosity of the extractor.
59+
The supported levels are (in order of increasing verbosity):
60+
- off
61+
- errors
62+
- warnings
63+
- info or progress
64+
- debug or progress+
65+
- trace or progress++
66+
- progress+++
67+
type: string
68+
pattern: "^(off|errors|warnings|(info|progress)|(debug|progress\\+)|(trace|progress\\+\\+)|progress\\+\\+\\+)$"

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 ILogger logger = new ConsoleLogger(options.LegacyVerbosity, logThreadId: false);
41+
using ILogger logger = new ConsoleLogger(options.Verbosity, logThreadId: false);
4242

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public static ExitCode Run(Options options)
136136
var stopwatch = new Stopwatch();
137137
stopwatch.Start();
138138

139-
using var logger = new ConsoleLogger(options.LegacyVerbosity, logThreadId: true);
139+
using var logger = new ConsoleLogger(options.Verbosity, logThreadId: true);
140140
logger.Log(Severity.Info, "Running C# standalone extractor");
141141
using var a = new Analysis(logger, options);
142142
var sourceFileCount = a.Extraction.Sources.Count;
@@ -147,7 +147,7 @@ public static ExitCode Run(Options options)
147147
return ExitCode.Errors;
148148
}
149149

150-
using var fileLogger = CSharp.Extractor.MakeLogger(options.LegacyVerbosity, false);
150+
using var fileLogger = CSharp.Extractor.MakeLogger(options.Verbosity, false);
151151

152152
logger.Log(Severity.Info, "");
153153
logger.Log(Severity.Info, "Extracting...");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public static ExitCode Run(string[] args)
9797
var options = Options.CreateWithEnvironment(args);
9898
Entities.Compilation.Settings = (Directory.GetCurrentDirectory(), options.CompilerArguments.ToArray());
9999

100-
using var logger = MakeLogger(options.LegacyVerbosity, options.Console);
100+
using var logger = MakeLogger(options.Verbosity, options.Console);
101101

102102
var canonicalPathCache = CanonicalPathCache.Create(logger, 1000);
103103
var pathTransformer = new PathTransformer(canonicalPathCache);

csharp/extractor/Semmle.Extraction.Tests/Options.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,37 @@ public void VerbosityTests()
103103
Assert.Throws<FormatException>(() => CSharp.Options.CreateWithEnvironment(new string[] { "--verbosity", "X" }));
104104
}
105105

106+
107+
private const string extractorVariableName = "CODEQL_EXTRACTOR_CSHARP_OPTION_LOGGING_VERBOSITY";
108+
private const string cliVariableName = "CODEQL_VERBOSITY";
109+
110+
private void CheckVerbosity(string? extractor, string? cli, Verbosity expected)
111+
{
112+
var currentExtractorVerbosity = Environment.GetEnvironmentVariable(extractorVariableName);
113+
var currentCliVerbosity = Environment.GetEnvironmentVariable(cliVariableName);
114+
try
115+
{
116+
Environment.SetEnvironmentVariable(extractorVariableName, extractor);
117+
Environment.SetEnvironmentVariable(cliVariableName, cli);
118+
119+
options = CSharp.Options.CreateWithEnvironment(new string[] { "--verbose" });
120+
Assert.Equal(expected, options.Verbosity);
121+
}
122+
finally
123+
{
124+
Environment.SetEnvironmentVariable(extractorVariableName, currentExtractorVerbosity);
125+
Environment.SetEnvironmentVariable(cliVariableName, currentCliVerbosity);
126+
}
127+
}
128+
129+
[Fact]
130+
public void VerbosityTests_WithExtractorOption()
131+
{
132+
CheckVerbosity("progress+++", "progress++", Verbosity.All);
133+
CheckVerbosity(null, "progress++", Verbosity.Trace);
134+
CheckVerbosity(null, null, Verbosity.Debug);
135+
}
136+
106137
[Fact]
107138
public void Console()
108139
{

csharp/extractor/Semmle.Extraction/Options.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,36 @@ public abstract class CommonOptions : ICommandLineOptions
2020
/// </summary>
2121
public Verbosity LegacyVerbosity { get; protected set; } = Verbosity.Info;
2222

23+
private Verbosity? verbosity = null;
24+
public Verbosity Verbosity
25+
{
26+
get
27+
{
28+
if (verbosity != null)
29+
{
30+
return verbosity.Value;
31+
}
32+
33+
var envVarValue = EnvironmentVariables.GetExtractorOption("LOGGING_VERBOSITY");
34+
verbosity = VerbosityExtensions.ParseVerbosity(envVarValue, logThreadId: true);
35+
if (verbosity != null)
36+
{
37+
return verbosity.Value;
38+
}
39+
40+
envVarValue = Environment.GetEnvironmentVariable("CODEQL_VERBOSITY");
41+
verbosity = VerbosityExtensions.ParseVerbosity(envVarValue, logThreadId: true);
42+
if (verbosity != null)
43+
{
44+
return verbosity.Value;
45+
}
46+
47+
// This only works, because we already parsed the provided options, so `LegacyVerbosity` is already set (or it still has the default value).
48+
verbosity = LegacyVerbosity;
49+
return verbosity.Value;
50+
}
51+
}
52+
2353
/// <summary>
2454
/// Whether to output to the console.
2555
/// </summary>

csharp/extractor/Semmle.Util/Logging/VerbosityExtensions.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Semmle.Util.Logging
44
{
5-
internal static class VerbosityExtensions
5+
public static class VerbosityExtensions
66
{
77
/// <summary>
88
/// Whether a message with the given severity must be included
@@ -26,5 +26,34 @@ public static bool Includes(this Verbosity v, Severity s)
2626
throw new ArgumentOutOfRangeException(nameof(s));
2727
}
2828
}
29+
30+
public static Verbosity? ParseVerbosity(string? str, bool logThreadId)
31+
{
32+
if (str == null)
33+
{
34+
return null;
35+
}
36+
37+
Verbosity? verbosity = str.ToLowerInvariant() switch
38+
{
39+
"off" => Verbosity.Off,
40+
"errors" => Verbosity.Error,
41+
"warnings" => Verbosity.Warning,
42+
"info" or "progress" => Verbosity.Info,
43+
"debug" or "progress+" => Verbosity.Debug,
44+
"trace" or "progress++" => Verbosity.Trace,
45+
"progress+++" => Verbosity.All,
46+
_ => null
47+
};
48+
49+
if (verbosity == null && str != null)
50+
{
51+
// We don't have a logger when this setting is parsed, so writing it to the console:
52+
var prefix = logThreadId ? $"[{Environment.CurrentManagedThreadId:D3}] " : "";
53+
Console.WriteLine($"{prefix}Error: Invalid verbosity level: '{str}'");
54+
}
55+
56+
return verbosity;
57+
}
2958
}
3059
}

0 commit comments

Comments
 (0)