Skip to content

Commit

Permalink
Merge pull request #15437 from tamasvajk/feature/log-verbosity
Browse files Browse the repository at this point in the history
C#: Introduce log verbosity extractor option
  • Loading branch information
tamasvajk authored Jan 26, 2024
2 parents 52d7bd9 + c4849f9 commit 7a50d7a
Show file tree
Hide file tree
Showing 16 changed files with 362 additions and 238 deletions.
6 changes: 5 additions & 1 deletion csharp/autobuilder/Semmle.Autobuild.Shared/Autobuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@ protected string RequireEnvironmentVariable(string name)

protected DiagnosticClassifier DiagnosticClassifier { get; }

private readonly ILogger logger = new ConsoleLogger(Verbosity.Info, logThreadId: false);
private readonly ILogger logger = new ConsoleLogger(
VerbosityExtensions.ParseVerbosity(
Environment.GetEnvironmentVariable("CODEQL_VERBOSITY"),
logThreadId: false) ?? Verbosity.Info,
logThreadId: false);

private readonly IDiagnosticsWriter diagnostics;

Expand Down
18 changes: 18 additions & 0 deletions csharp/codeql-extractor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,21 @@ options:
The default is 'true'.
type: string
pattern: "^(false|true)$"
logging:
title: Options pertaining to logging.
type: object
properties:
verbosity:
title: Extractor logging verbosity level.
description: >
Controls the level of verbosity of the extractor.
The supported levels are (in order of increasing verbosity):
- off
- errors
- warnings
- info or progress
- debug or progress+
- trace or progress++
- progress+++
type: string
pattern: "^(off|errors|warnings|(info|progress)|(debug|progress\\+)|(trace|progress\\+\\+)|progress\\+\\+\\+)$"
2 changes: 1 addition & 1 deletion csharp/extractor/Semmle.Extraction.CIL.Driver/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static void Main(string[] args)
}

var options = new ExtractorOptions(args);
using var logger = new ConsoleLogger(options.Verbosity, logThreadId: false);
using ILogger logger = new ConsoleLogger(options.Verbosity, logThreadId: false);

var actions = options.AssembliesToExtract
.Select(asm => asm.Filename)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ public override bool HandleFlag(string key, bool value)
{
switch (key)
{
case "silent":
Verbosity = value ? Verbosity.Off : Verbosity.Info;
return true;
case "help":
Help = true;
return true;
Expand Down
49 changes: 40 additions & 9 deletions csharp/extractor/Semmle.Extraction.Tests/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void DefaultOptions()
Assert.Null(options.CompilerName);
Assert.Empty(options.CompilerArguments);
Assert.True(options.Threads >= 1);
Assert.Equal(Verbosity.Info, options.Verbosity);
Assert.Equal(Verbosity.Info, options.LegacyVerbosity);
Assert.False(options.Console);
Assert.False(options.PDB);
Assert.False(options.Fast);
Expand Down Expand Up @@ -80,29 +80,60 @@ public void CompilerArguments()
public void VerbosityTests()
{
options = CSharp.Options.CreateWithEnvironment(new string[] { "--verbose" });
Assert.Equal(Verbosity.Debug, options.Verbosity);
Assert.Equal(Verbosity.Debug, options.LegacyVerbosity);

options = CSharp.Options.CreateWithEnvironment(new string[] { "--verbosity", "0" });
Assert.Equal(Verbosity.Off, options.Verbosity);
Assert.Equal(Verbosity.Off, options.LegacyVerbosity);

options = CSharp.Options.CreateWithEnvironment(new string[] { "--verbosity", "1" });
Assert.Equal(Verbosity.Error, options.Verbosity);
Assert.Equal(Verbosity.Error, options.LegacyVerbosity);

options = CSharp.Options.CreateWithEnvironment(new string[] { "--verbosity", "2" });
Assert.Equal(Verbosity.Warning, options.Verbosity);
Assert.Equal(Verbosity.Warning, options.LegacyVerbosity);

options = CSharp.Options.CreateWithEnvironment(new string[] { "--verbosity", "3" });
Assert.Equal(Verbosity.Info, options.Verbosity);
Assert.Equal(Verbosity.Info, options.LegacyVerbosity);

options = CSharp.Options.CreateWithEnvironment(new string[] { "--verbosity", "4" });
Assert.Equal(Verbosity.Debug, options.Verbosity);
Assert.Equal(Verbosity.Debug, options.LegacyVerbosity);

options = CSharp.Options.CreateWithEnvironment(new string[] { "--verbosity", "5" });
Assert.Equal(Verbosity.Trace, options.Verbosity);
Assert.Equal(Verbosity.Trace, options.LegacyVerbosity);

Assert.Throws<FormatException>(() => CSharp.Options.CreateWithEnvironment(new string[] { "--verbosity", "X" }));
}


private const string extractorVariableName = "CODEQL_EXTRACTOR_CSHARP_OPTION_LOGGING_VERBOSITY";
private const string cliVariableName = "CODEQL_VERBOSITY";

private void CheckVerbosity(string? extractor, string? cli, Verbosity expected)
{
var currentExtractorVerbosity = Environment.GetEnvironmentVariable(extractorVariableName);
var currentCliVerbosity = Environment.GetEnvironmentVariable(cliVariableName);
try
{
Environment.SetEnvironmentVariable(extractorVariableName, extractor);
Environment.SetEnvironmentVariable(cliVariableName, cli);

options = CSharp.Options.CreateWithEnvironment(new string[] { "--verbose" });
Assert.Equal(expected, options.Verbosity);
}
finally
{
Environment.SetEnvironmentVariable(extractorVariableName, currentExtractorVerbosity);
Environment.SetEnvironmentVariable(cliVariableName, currentCliVerbosity);
}
}

[Fact]
public void VerbosityTests_WithExtractorOption()
{
CheckVerbosity("progress+++", "progress++", Verbosity.All);
CheckVerbosity(null, "progress++", Verbosity.Trace);
CheckVerbosity(null, null, Verbosity.Debug);
}

[Fact]
public void Console()
{
Expand Down Expand Up @@ -142,7 +173,7 @@ public void StandaloneDefaults()
public void StandaloneOptions()
{
standaloneOptions = CSharp.Standalone.Options.Create(new string[] { "--silent" });
Assert.Equal(Verbosity.Off, standaloneOptions.Verbosity);
Assert.Equal(Verbosity.Off, standaloneOptions.LegacyVerbosity);
Assert.False(standaloneOptions.Errors);
Assert.False(standaloneOptions.Help);
}
Expand Down
41 changes: 35 additions & 6 deletions csharp/extractor/Semmle.Extraction/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,39 @@ public abstract class CommonOptions : ICommandLineOptions
public int Threads { get; private set; } = EnvironmentVariables.GetDefaultNumberOfThreads();

/// <summary>
/// The verbosity used in output and logging.
/// The verbosity used specified by the '--silent' or '--verbose' flags or the '--verbosity' option.
/// </summary>
public Verbosity Verbosity { get; protected set; } = Verbosity.Info;
public Verbosity LegacyVerbosity { get; protected set; } = Verbosity.Info;

private Verbosity? verbosity = null;
public Verbosity Verbosity
{
get
{
if (verbosity != null)
{
return verbosity.Value;
}

var envVarValue = EnvironmentVariables.GetExtractorOption("LOGGING_VERBOSITY");
verbosity = VerbosityExtensions.ParseVerbosity(envVarValue, logThreadId: true);
if (verbosity != null)
{
return verbosity.Value;
}

envVarValue = Environment.GetEnvironmentVariable("CODEQL_VERBOSITY");
verbosity = VerbosityExtensions.ParseVerbosity(envVarValue, logThreadId: true);
if (verbosity != null)
{
return verbosity.Value;
}

// This only works, because we already parsed the provided options, so `LegacyVerbosity` is already set (or it still has the default value).
verbosity = LegacyVerbosity;
return verbosity.Value;
}
}

/// <summary>
/// Whether to output to the console.
Expand Down Expand Up @@ -63,7 +93,7 @@ public virtual bool HandleOption(string key, string value)
Threads = int.Parse(value);
return true;
case "verbosity":
Verbosity = (Verbosity)int.Parse(value);
LegacyVerbosity = (Verbosity)int.Parse(value);
return true;
case "trap_compression":
if (Enum.TryParse<TrapWriter.CompressionMode>(value, true, out var mode))
Expand All @@ -87,11 +117,10 @@ public virtual bool HandleFlag(string flag, bool value)
switch (flag)
{
case "silent":
if (value)
Verbosity = Verbosity.Off;
LegacyVerbosity = value ? Verbosity.Off : Verbosity.Info;
return true;
case "verbose":
Verbosity = value ? Verbosity.Debug : Verbosity.Error;
LegacyVerbosity = value ? Verbosity.Debug : Verbosity.Error;
return true;
case "console":
Console = value;
Expand Down
Loading

0 comments on commit 7a50d7a

Please sign in to comment.