Skip to content

Commit d947861

Browse files
committed
C#: Minor refactoring.
1 parent 4973224 commit d947861

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using Semmle.BuildAnalyser;
45
using Semmle.Util.Logging;
56

@@ -56,7 +57,6 @@ public static int Main(string[] args)
5657

5758
var options = Options.Create(args);
5859
// options.CIL = true; // To do: Enable this
59-
using var output = new ConsoleLogger(options.Verbosity);
6060

6161
if (options.Help)
6262
{
@@ -67,31 +67,33 @@ public static int Main(string[] args)
6767
if (options.Errors)
6868
return 1;
6969

70-
var start = DateTime.Now;
70+
var stopwatch = new Stopwatch();
71+
stopwatch.Start();
7172

72-
output.Log(Severity.Info, "Running C# standalone extractor");
73-
using var a = new Analysis(output, options);
73+
using var logger = new ConsoleLogger(options.Verbosity);
74+
logger.Log(Severity.Info, "Running C# standalone extractor");
75+
using var a = new Analysis(logger, options);
7476
var sourceFileCount = a.Extraction.Sources.Count;
7577

7678
if (sourceFileCount == 0)
7779
{
78-
output.Log(Severity.Error, "No source files found");
80+
logger.Log(Severity.Error, "No source files found");
7981
return 1;
8082
}
8183

8284
if (!options.SkipExtraction)
8385
{
84-
using var fileLogger = new FileLogger(options.Verbosity, Extractor.GetCSharpLogPath());
86+
using var fileLogger = Extractor.MakeLogger(options.Verbosity, false);
8587

86-
output.Log(Severity.Info, "");
87-
output.Log(Severity.Info, "Extracting...");
88+
logger.Log(Severity.Info, "");
89+
logger.Log(Severity.Info, "Extracting...");
8890
Extractor.ExtractStandalone(
8991
a.Extraction.Sources,
9092
a.References,
91-
new ExtractionProgress(output),
93+
new ExtractionProgress(logger),
9294
fileLogger,
9395
options);
94-
output.Log(Severity.Info, $"Extraction completed in {DateTime.Now - start}");
96+
logger.Log(Severity.Info, $"Extraction completed in {stopwatch.Elapsed}");
9597
}
9698

9799
return 0;

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ public static void SetInvariantCulture()
6969
Thread.CurrentThread.CurrentUICulture = culture;
7070
}
7171

72+
public static ILogger MakeLogger(Verbosity verbosity, bool includeConsole)
73+
{
74+
var fileLogger = new FileLogger(verbosity, GetCSharpLogPath());
75+
return includeConsole
76+
? new CombinedLogger(new ConsoleLogger(verbosity), fileLogger)
77+
: (ILogger)fileLogger;
78+
}
79+
7280
/// <summary>
7381
/// Command-line driver for the extractor.
7482
/// </summary>
@@ -89,10 +97,7 @@ public static ExitCode Run(string[] args)
8997
var options = Options.CreateWithEnvironment(args);
9098
Entities.Compilation.Settings = (Directory.GetCurrentDirectory(), options.CompilerArguments.ToArray());
9199

92-
var fileLogger = new FileLogger(options.Verbosity, GetCSharpLogPath());
93-
using var logger = options.Console
94-
? new CombinedLogger(new ConsoleLogger(options.Verbosity), fileLogger)
95-
: (ILogger)fileLogger;
100+
using var logger = MakeLogger(options.Verbosity, options.Console);
96101

97102
if (Environment.GetEnvironmentVariable("SEMMLE_CLRTRACER") == "1" && !options.ClrTracer)
98103
{

0 commit comments

Comments
 (0)