Skip to content

Commit bb0a20f

Browse files
authored
Merge pull request #353 from MarcoRossignoli/addsomelogs
Added some logs
2 parents f258333 + 19ac4f7 commit bb0a20f

File tree

6 files changed

+40
-24
lines changed

6 files changed

+40
-24
lines changed

build.proj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
</Target>
2626

2727
<Target Name="RunTests" AfterTargets="CopyMSBuildScripts">
28-
<Exec Command="dotnet test &quot;$(MSBuildThisFileDirectory)test\coverlet.core.tests\coverlet.core.tests.csproj&quot; -c $(Configuration) /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:Include=[coverlet.*]*"/>
28+
<Exec Command="dotnet test &quot;$(MSBuildThisFileDirectory)test\coverlet.core.tests\coverlet.core.tests.csproj&quot; -c $(Configuration) /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:Include=[coverlet.*]* -verbosity:minimal"/>
2929
</Target>
3030

3131
<Target Name="CreateNuGetPackage" AfterTargets="RunTests" Condition="$(Configuration) == 'Release'">

src/coverlet.core/Coverage.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ public string Identifier
3232
get { return _identifier; }
3333
}
3434

35-
public Coverage(string module,
36-
string[] includeFilters,
37-
string[] includeDirectories,
38-
string[] excludeFilters,
39-
string[] excludedSourceFiles,
40-
string[] excludeAttributes,
41-
bool singleHit,
42-
string mergeWith,
35+
public Coverage(string module,
36+
string[] includeFilters,
37+
string[] includeDirectories,
38+
string[] excludeFilters,
39+
string[] excludedSourceFiles,
40+
string[] excludeAttributes,
41+
bool singleHit,
42+
string mergeWith,
4343
bool useSourceLink,
4444
ILogger logger)
4545
{
@@ -62,16 +62,24 @@ public void PrepareModules()
6262
{
6363
string[] modules = InstrumentationHelper.GetCoverableModules(_module, _includeDirectories);
6464
string[] excludes = InstrumentationHelper.GetExcludedFiles(_excludedSourceFiles);
65+
66+
Array.ForEach(_excludeFilters ?? Array.Empty<string>(), filter => _logger.LogInformation($"Excluded module filter '{filter}'"));
67+
Array.ForEach(_includeFilters ?? Array.Empty<string>(), filter => _logger.LogInformation($"Included module filter '{filter}'"));
68+
Array.ForEach(excludes ?? Array.Empty<string>(), filter => _logger.LogInformation($"Excluded source files '{filter}'"));
69+
6570
_excludeFilters = _excludeFilters?.Where(f => InstrumentationHelper.IsValidFilterExpression(f)).ToArray();
6671
_includeFilters = _includeFilters?.Where(f => InstrumentationHelper.IsValidFilterExpression(f)).ToArray();
6772

6873
foreach (var module in modules)
6974
{
7075
if (InstrumentationHelper.IsModuleExcluded(module, _excludeFilters) ||
7176
!InstrumentationHelper.IsModuleIncluded(module, _includeFilters))
77+
{
78+
_logger.LogInformation($"Excluded module: '{module}'");
7279
continue;
80+
}
7381

74-
var instrumenter = new Instrumenter(module, _identifier, _excludeFilters, _includeFilters, excludes, _excludeAttributes, _singleHit);
82+
var instrumenter = new Instrumenter(module, _identifier, _excludeFilters, _includeFilters, excludes, _excludeAttributes, _singleHit, _logger);
7583
if (instrumenter.CanInstrument())
7684
{
7785
InstrumentationHelper.BackupOriginalModule(module, _identifier);
@@ -81,6 +89,7 @@ public void PrepareModules()
8189
{
8290
var result = instrumenter.Instrument();
8391
_results.Add(result);
92+
_logger.LogInformation($"Instrumented module: '{module}'");
8493
}
8594
catch (Exception ex)
8695
{
@@ -197,7 +206,7 @@ private void CalculateCoverage()
197206
{
198207
if (!File.Exists(result.HitsFilePath))
199208
{
200-
// File not instrumented, or nothing in it called. Warn about this?
209+
_logger.LogWarning($"Hits file:'{result.HitsFilePath}' not found for module: '{result.Module}'");
201210
continue;
202211
}
203212

src/coverlet.core/Instrumentation/Instrumenter.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
using Coverlet.Core.Attributes;
99
using Coverlet.Core.Helpers;
10+
using Coverlet.Core.Logging;
1011
using Coverlet.Core.Symbols;
1112

1213
using Mono.Cecil;
@@ -25,6 +26,7 @@ internal class Instrumenter
2526
private readonly string[] _excludedAttributes;
2627
private readonly bool _singleHit;
2728
private readonly bool _isCoreLibrary;
29+
private readonly ILogger _logger;
2830
private InstrumenterResult _result;
2931
private FieldDefinition _customTrackerHitsArray;
3032
private FieldDefinition _customTrackerHitsFilePath;
@@ -35,7 +37,7 @@ internal class Instrumenter
3537
private MethodReference _customTrackerRecordHitMethod;
3638
private List<string> _asyncMachineStateMethod;
3739

38-
public Instrumenter(string module, string identifier, string[] excludeFilters, string[] includeFilters, string[] excludedFiles, string[] excludedAttributes, bool singleHit)
40+
public Instrumenter(string module, string identifier, string[] excludeFilters, string[] includeFilters, string[] excludedFiles, string[] excludedAttributes, bool singleHit, ILogger logger)
3941
{
4042
_module = module;
4143
_identifier = identifier;
@@ -44,8 +46,8 @@ public Instrumenter(string module, string identifier, string[] excludeFilters, s
4446
_excludedFiles = excludedFiles ?? Array.Empty<string>();
4547
_excludedAttributes = excludedAttributes;
4648
_singleHit = singleHit;
47-
4849
_isCoreLibrary = Path.GetFileNameWithoutExtension(_module) == "System.Private.CoreLib";
50+
_logger = logger;
4951
}
5052

5153
public bool CanInstrument() => InstrumentationHelper.HasPdb(_module);
@@ -279,7 +281,10 @@ private void InstrumentMethod(MethodDefinition method)
279281
{
280282
var sourceFile = method.DebugInformation.SequencePoints.Select(s => s.Document.Url).FirstOrDefault();
281283
if (!string.IsNullOrEmpty(sourceFile) && _excludedFiles.Contains(sourceFile))
284+
{
285+
_logger.LogInformation($"Excluded source file: '{sourceFile}'");
282286
return;
287+
}
283288

284289
var methodBody = GetMethodBody(method);
285290
if (methodBody == null)

src/coverlet.msbuild.tasks/MSBuildLogger.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@ namespace Coverlet.MSbuild.Tasks
77
{
88
class MSBuildLogger : ILogger
99
{
10+
private const string LogPrefix = "[coverlet] ";
11+
1012
private readonly TaskLoggingHelper _log;
1113

1214
public MSBuildLogger(TaskLoggingHelper log) => _log = log;
1315

14-
public void LogVerbose(string message) => _log.LogMessage(MessageImportance.Low, message);
16+
public void LogVerbose(string message) => _log.LogMessage(MessageImportance.Low, $"{LogPrefix}{message}");
1517

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

19-
public void LogWarning(string message) => _log.LogWarning(message);
21+
public void LogWarning(string message) => _log.LogWarning($"{LogPrefix}{message}");
2022

21-
public void LogError(string message) => _log.LogError(message);
23+
public void LogError(string message) => _log.LogError($"{LogPrefix}{message}");
2224

23-
public void LogError(Exception exception) => _log.LogErrorFromException(exception);
25+
public void LogError(Exception exception) => _log.LogErrorFromException(exception, true);
2426
}
2527
}

test/coverlet.core.tests/CoverageTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@ public void TestCoverage()
2020
File.Copy(module, Path.Combine(directory.FullName, Path.GetFileName(module)), true);
2121
File.Copy(pdb, Path.Combine(directory.FullName, Path.GetFileName(pdb)), true);
2222

23-
var logger = Mock.Of<ILogger>();
24-
2523
// TODO: Find a way to mimick hits
2624

27-
var coverage = new Coverage(Path.Combine(directory.FullName, Path.GetFileName(module)), Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), false, string.Empty, false, logger);
25+
var coverage = new Coverage(Path.Combine(directory.FullName, Path.GetFileName(module)), Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), false, string.Empty, false, new Mock<ILogger>().Object);
2826
coverage.PrepareModules();
2927

3028
var result = coverage.GetCoverageResult();

test/coverlet.core.tests/Instrumentation/InstrumenterTests.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
using System.IO;
33
using System.Linq;
44
using Xunit;
5-
using Coverlet.Core.Instrumentation;
5+
66
using Coverlet.Core.Samples.Tests;
7+
using Moq;
8+
using Coverlet.Core.Logging;
79

810
namespace Coverlet.Core.Instrumentation.Tests
911
{
@@ -27,7 +29,7 @@ public void TestCoreLibInstrumentation()
2729
foreach (var file in files)
2830
File.Copy(Path.Combine(OriginalFilesDir, file), Path.Combine(TestFilesDir, file), overwrite: true);
2931

30-
Instrumenter instrumenter = new Instrumenter(Path.Combine(TestFilesDir, files[0]), "_coverlet_instrumented", Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), false);
32+
Instrumenter instrumenter = new Instrumenter(Path.Combine(TestFilesDir, files[0]), "_coverlet_instrumented", Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), false, new Mock<ILogger>().Object);
3133
Assert.True(instrumenter.CanInstrument());
3234
var result = instrumenter.Instrument();
3335
Assert.NotNull(result);
@@ -119,7 +121,7 @@ private InstrumenterTest CreateInstrumentor(bool fakeCoreLibModule = false, stri
119121
File.Copy(pdb, Path.Combine(directory.FullName, destPdb), true);
120122

121123
module = Path.Combine(directory.FullName, destModule);
122-
Instrumenter instrumenter = new Instrumenter(module, identifier, Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), attributesToIgnore, false);
124+
Instrumenter instrumenter = new Instrumenter(module, identifier, Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), attributesToIgnore, false, new Mock<ILogger>().Object);
123125
return new InstrumenterTest
124126
{
125127
Instrumenter = instrumenter,

0 commit comments

Comments
 (0)