File tree Expand file tree Collapse file tree 8 files changed +118
-6
lines changed
autobuilder/Semmle.Autobuild.Shared Expand file tree Collapse file tree 8 files changed +118
-6
lines changed Original file line number Diff line number Diff line change @@ -267,7 +267,11 @@ protected string RequireEnvironmentVariable(string name)
267
267
268
268
protected DiagnosticClassifier DiagnosticClassifier { get ; }
269
269
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 ) ;
271
275
272
276
private readonly IDiagnosticsWriter diagnostics ;
273
277
Original file line number Diff line number Diff line change @@ -48,3 +48,21 @@ options:
48
48
The default is 'true'.
49
49
type : string
50
50
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\\ +\\ +\\ +)$"
Original file line number Diff line number Diff line change @@ -38,7 +38,7 @@ public static void Main(string[] args)
38
38
}
39
39
40
40
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 ) ;
42
42
43
43
var actions = options . AssembliesToExtract
44
44
. Select ( asm => asm . Filename )
Original file line number Diff line number Diff line change @@ -136,7 +136,7 @@ public static ExitCode Run(Options options)
136
136
var stopwatch = new Stopwatch ( ) ;
137
137
stopwatch . Start ( ) ;
138
138
139
- using var logger = new ConsoleLogger ( options . LegacyVerbosity , logThreadId : true ) ;
139
+ using var logger = new ConsoleLogger ( options . Verbosity , logThreadId : true ) ;
140
140
logger . Log ( Severity . Info , "Running C# standalone extractor" ) ;
141
141
using var a = new Analysis ( logger , options ) ;
142
142
var sourceFileCount = a . Extraction . Sources . Count ;
@@ -147,7 +147,7 @@ public static ExitCode Run(Options options)
147
147
return ExitCode . Errors ;
148
148
}
149
149
150
- using var fileLogger = CSharp . Extractor . MakeLogger ( options . LegacyVerbosity , false ) ;
150
+ using var fileLogger = CSharp . Extractor . MakeLogger ( options . Verbosity , false ) ;
151
151
152
152
logger . Log ( Severity . Info , "" ) ;
153
153
logger . Log ( Severity . Info , "Extracting..." ) ;
Original file line number Diff line number Diff line change @@ -97,7 +97,7 @@ public static ExitCode Run(string[] args)
97
97
var options = Options . CreateWithEnvironment ( args ) ;
98
98
Entities . Compilation . Settings = ( Directory . GetCurrentDirectory ( ) , options . CompilerArguments . ToArray ( ) ) ;
99
99
100
- using var logger = MakeLogger ( options . LegacyVerbosity , options . Console ) ;
100
+ using var logger = MakeLogger ( options . Verbosity , options . Console ) ;
101
101
102
102
var canonicalPathCache = CanonicalPathCache . Create ( logger , 1000 ) ;
103
103
var pathTransformer = new PathTransformer ( canonicalPathCache ) ;
Original file line number Diff line number Diff line change @@ -103,6 +103,37 @@ public void VerbosityTests()
103
103
Assert . Throws < FormatException > ( ( ) => CSharp . Options . CreateWithEnvironment ( new string [ ] { "--verbosity" , "X" } ) ) ;
104
104
}
105
105
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
+
106
137
[ Fact ]
107
138
public void Console ( )
108
139
{
Original file line number Diff line number Diff line change @@ -20,6 +20,36 @@ public abstract class CommonOptions : ICommandLineOptions
20
20
/// </summary>
21
21
public Verbosity LegacyVerbosity { get ; protected set ; } = Verbosity . Info ;
22
22
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
+
23
53
/// <summary>
24
54
/// Whether to output to the console.
25
55
/// </summary>
Original file line number Diff line number Diff line change 2
2
3
3
namespace Semmle . Util . Logging
4
4
{
5
- internal static class VerbosityExtensions
5
+ public static class VerbosityExtensions
6
6
{
7
7
/// <summary>
8
8
/// Whether a message with the given severity must be included
@@ -26,5 +26,34 @@ public static bool Includes(this Verbosity v, Severity s)
26
26
throw new ArgumentOutOfRangeException ( nameof ( s ) ) ;
27
27
}
28
28
}
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
+ }
29
58
}
30
59
}
You can’t perform that action at this time.
0 commit comments