Skip to content

Commit 35eccd9

Browse files
authored
Merge pull request #384 from ViktorHofer/LogLevel
Add log level option
2 parents 53fbc49 + b3207e4 commit 35eccd9

File tree

9 files changed

+61
-21
lines changed

9 files changed

+61
-21
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Options:
6161
-t|--target Path to the test runner application.
6262
-a|--targetargs Arguments to be passed to the test runner.
6363
-o|--output Output of the generated coverage report
64+
-v|--verbosity Sets the verbosity level of the command. Allowed values are quiet, minimal, normal, detailed.
6465
-f|--format Format of the generated coverage report.
6566
--threshold Exits with error if the coverage % is below value.
6667
--threshold-type Coverage type to apply the threshold to.
@@ -69,10 +70,10 @@ Options:
6970
--include Filter expressions to include specific modules and types.
7071
--include-directory Include directories containing additional assemblies to be instrumented.
7172
--exclude-by-file Glob patterns specifying source files to exclude.
72-
--include-directory Include directories containing additional assemblies to be instrumented.
7373
--exclude-by-attribute Attributes to exclude from code coverage.
7474
--merge-with Path to existing coverage result to merge.
7575
--use-source-link Specifies whether to use SourceLink URIs in place of file system paths.
76+
--single-hit Specifies whether to limit code coverage hit reporting to a single hit for each location.
7677
```
7778
7879
#### Code Coverage

src/coverlet.console/Logging/ConsoleLogger.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@ namespace Coverlet.Console.Logging
77
class ConsoleLogger : ILogger
88
{
99
private static readonly object _sync = new object();
10+
public LogLevel Level { get; set; } = LogLevel.Normal;
1011

11-
public void LogError(string message) => Log(message, ConsoleColor.Red);
12+
public void LogError(string message) => Log(LogLevel.Quiet, message, ConsoleColor.Red);
1213

1314
public void LogError(Exception exception) => LogError(exception.ToString());
1415

15-
public void LogInformation(string message) => Log(message, ForegroundColor);
16+
public void LogInformation(string message, bool important = false) => Log(important ? LogLevel.Minimal : LogLevel.Normal, message, ForegroundColor);
1617

17-
public void LogVerbose(string message) => throw new NotImplementedException();
18+
public void LogVerbose(string message) => Log(LogLevel.Detailed, message, ForegroundColor);
1819

19-
public void LogWarning(string message) => Log(message, ConsoleColor.Yellow);
20+
public void LogWarning(string message) => Log(LogLevel.Quiet, message, ConsoleColor.Yellow);
2021

21-
private void Log(string message, ConsoleColor color)
22+
private void Log(LogLevel level, string message, ConsoleColor color)
2223
{
24+
if (level < Level) return;
25+
2326
lock (_sync)
2427
{
2528
ConsoleColor currentForegroundColor;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace Coverlet.Console.Logging
2+
{
3+
/// <summary>
4+
/// Defines logging severity levels.
5+
/// </summary>
6+
enum LogLevel
7+
{
8+
/// <summary>
9+
/// Logs that track the general flow of the application. These logs should have long-term value.
10+
/// </summary>
11+
Detailed = 0,
12+
13+
/// <summary>
14+
/// Logs that highlight an abnormal or unexpected event in the application flow, but do not otherwise cause the
15+
/// application execution to stop.
16+
/// </summary>
17+
Normal = 1,
18+
19+
/// <summary>
20+
/// Logs that highlight when the current flow of execution is stopped due to a failure. These should indicate a
21+
/// failure in the current activity, not an application-wide failure.
22+
/// </summary>
23+
Minimal = 2,
24+
25+
/// <summary>
26+
/// Not used for writing log messages. Specifies that a logging category should not write any messages except warning and errors.
27+
/// </summary>
28+
Quiet = 3
29+
}
30+
}

src/coverlet.console/Program.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
using Coverlet.Core;
1010
using Coverlet.Core.Enums;
1111
using Coverlet.Core.Reporters;
12-
13-
using Microsoft.Extensions.CommandLineUtils;
12+
using McMaster.Extensions.CommandLineUtils;
1413

1514
namespace Coverlet.Console
1615
{
@@ -29,6 +28,7 @@ static int Main(string[] args)
2928
CommandOption target = app.Option("-t|--target", "Path to the test runner application.", CommandOptionType.SingleValue);
3029
CommandOption targs = app.Option("-a|--targetargs", "Arguments to be passed to the test runner.", CommandOptionType.SingleValue);
3130
CommandOption output = app.Option("-o|--output", "Output of the generated coverage report", CommandOptionType.SingleValue);
31+
CommandOption<LogLevel> verbosity = app.Option<LogLevel>("-v|--verbosity", "Sets the verbosity level of the command. Allowed values are quiet, minimal, normal, detailed.", CommandOptionType.SingleValue);
3232
CommandOption formats = app.Option("-f|--format", "Format of the generated coverage report.", CommandOptionType.MultipleValue);
3333
CommandOption threshold = app.Option("--threshold", "Exits with error if the coverage % is below value.", CommandOptionType.SingleValue);
3434
CommandOption thresholdTypes = app.Option("--threshold-type", "Coverage type to apply the threshold to.", CommandOptionType.MultipleValue);
@@ -51,6 +51,12 @@ static int Main(string[] args)
5151
if (!target.HasValue())
5252
throw new CommandParsingException(app, "Target must be specified.");
5353

54+
if (verbosity.HasValue())
55+
{
56+
// Adjust log level based on user input.
57+
logger.Level = verbosity.ParsedValue;
58+
}
59+
5460
Coverage coverage = new Coverage(module.Value,
5561
includeFilters.Values.ToArray(),
5662
includeDirectories.Values.ToArray(),
@@ -73,7 +79,7 @@ static int Main(string[] args)
7379
process.OutputDataReceived += (sender, eventArgs) =>
7480
{
7581
if (!string.IsNullOrEmpty(eventArgs.Data))
76-
logger.LogInformation(eventArgs.Data);
82+
logger.LogInformation(eventArgs.Data, important: true);
7783
};
7884

7985
process.ErrorDataReceived += (sender, eventArgs) =>
@@ -118,8 +124,8 @@ static int Main(string[] args)
118124
if (reporter.OutputType == ReporterOutputType.Console)
119125
{
120126
// Output to console
121-
logger.LogInformation(" Outputting results to console");
122-
logger.LogInformation(reporter.Report(result));
127+
logger.LogInformation(" Outputting results to console", important: true);
128+
logger.LogInformation(reporter.Report(result), important: true);
123129
}
124130
else
125131
{
@@ -129,7 +135,7 @@ static int Main(string[] args)
129135
filename = Path.HasExtension(filename) ? filename : $"{filename}.{reporter.Extension}";
130136

131137
var report = Path.Combine(directory, filename);
132-
logger.LogInformation($" Generating report '{report}'");
138+
logger.LogInformation($" Generating report '{report}'", important: true);
133139
File.WriteAllText(report, reporter.Report(result));
134140
}
135141
}

src/coverlet.console/coverlet.console.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
</PropertyGroup>
2222

2323
<ItemGroup>
24-
<PackageReference Include="Microsoft.Extensions.CommandLineUtils" Version="1.1.1" />
24+
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.3.4" />
2525
</ItemGroup>
2626

2727
<ItemGroup>

src/coverlet.core/Coverage.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ public void PrepareModules()
6666
string[] modules = InstrumentationHelper.GetCoverableModules(_module, _includeDirectories, _includeTestAssembly);
6767
string[] excludes = InstrumentationHelper.GetExcludedFiles(_excludedSourceFiles);
6868

69-
Array.ForEach(_excludeFilters ?? Array.Empty<string>(), filter => _logger.LogInformation($"Excluded module filter '{filter}'"));
70-
Array.ForEach(_includeFilters ?? Array.Empty<string>(), filter => _logger.LogInformation($"Included module filter '{filter}'"));
71-
Array.ForEach(excludes ?? Array.Empty<string>(), filter => _logger.LogInformation($"Excluded source files '{filter}'"));
69+
Array.ForEach(_excludeFilters ?? Array.Empty<string>(), filter => _logger.LogVerbose($"Excluded module filter '{filter}'"));
70+
Array.ForEach(_includeFilters ?? Array.Empty<string>(), filter => _logger.LogVerbose($"Included module filter '{filter}'"));
71+
Array.ForEach(excludes ?? Array.Empty<string>(), filter => _logger.LogVerbose($"Excluded source files '{filter}'"));
7272

7373
_excludeFilters = _excludeFilters?.Where(f => InstrumentationHelper.IsValidFilterExpression(f)).ToArray();
7474
_includeFilters = _includeFilters?.Where(f => InstrumentationHelper.IsValidFilterExpression(f)).ToArray();
@@ -78,7 +78,7 @@ public void PrepareModules()
7878
if (InstrumentationHelper.IsModuleExcluded(module, _excludeFilters) ||
7979
!InstrumentationHelper.IsModuleIncluded(module, _includeFilters))
8080
{
81-
_logger.LogInformation($"Excluded module: '{module}'");
81+
_logger.LogVerbose($"Excluded module: '{module}'");
8282
continue;
8383
}
8484

@@ -92,7 +92,7 @@ public void PrepareModules()
9292
{
9393
var result = instrumenter.Instrument();
9494
_results.Add(result);
95-
_logger.LogInformation($"Instrumented module: '{module}'");
95+
_logger.LogVerbose($"Instrumented module: '{module}'");
9696
}
9797
catch (Exception ex)
9898
{

src/coverlet.core/Instrumentation/Instrumenter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ private void InstrumentMethod(MethodDefinition method)
293293
var sourceFile = method.DebugInformation.SequencePoints.Select(s => s.Document.Url).FirstOrDefault();
294294
if (!string.IsNullOrEmpty(sourceFile) && _excludedFiles.Contains(sourceFile))
295295
{
296-
_logger.LogInformation($"Excluded source file: '{sourceFile}'");
296+
_logger.LogVerbose($"Excluded source file: '{sourceFile}'");
297297
return;
298298
}
299299

src/coverlet.core/Logging/ILogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Coverlet.Core.Logging
55
public interface ILogger
66
{
77
void LogVerbose(string message);
8-
void LogInformation(string message);
8+
void LogInformation(string message, bool important = false);
99
void LogWarning(string message);
1010
void LogError(string message);
1111
void LogError(Exception exception);

src/coverlet.msbuild.tasks/MSBuildLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class MSBuildLogger : ILogger
1616
public void LogVerbose(string message) => _log.LogMessage(MessageImportance.Low, $"{LogPrefix}{message}");
1717

1818
// We use `MessageImportance.High` because with `MessageImportance.Normal` doesn't show anything
19-
public void LogInformation(string message) => _log.LogMessage(MessageImportance.High, $"{LogPrefix}{message}");
19+
public void LogInformation(string message, bool important = false) => _log.LogMessage(MessageImportance.High, $"{LogPrefix}{message}");
2020

2121
public void LogWarning(string message) => _log.LogWarning($"{LogPrefix}{message}");
2222

0 commit comments

Comments
 (0)