Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions src/coverlet.console/Logging/ConsoleLogger.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,46 @@
using System;
using Coverlet.Core.Logging;
using static System.Console;

namespace Coverlet.Console.Logging
{
class ConsoleLogger : ILogger
{
private static readonly object _sync = new object();

public void LogError(string message)
{
ForegroundColor = ConsoleColor.Red;
WriteLine(message);
ForegroundColor = ConsoleColor.White;
lock (_sync)
{
var previous = ForegroundColor;
ForegroundColor = ConsoleColor.Red;
WriteLine(message);
ForegroundColor = previous;
}
}

public void LogError(string message, Exception ex)
{
WriteLine($"{message}{Environment.NewLine}{ex}");
}

public void LogInformation(string message)
{
WriteLine(message);
lock (_sync)
{
WriteLine(message);
}
}

public void LogSuccess(string message)
{
ForegroundColor = ConsoleColor.Green;
WriteLine(message);
ForegroundColor = ConsoleColor.White;
lock (_sync)
{
var previous = ForegroundColor;
ForegroundColor = ConsoleColor.Green;
WriteLine(message);
ForegroundColor = previous;
}
}

public void LogVerbose(string message)
Expand All @@ -31,9 +50,13 @@ public void LogVerbose(string message)

public void LogWarning(string message)
{
ForegroundColor = ConsoleColor.Yellow;
WriteLine(message);
ForegroundColor = ConsoleColor.White;
lock (_sync)
{
var previous = ForegroundColor;
ForegroundColor = ConsoleColor.Yellow;
WriteLine(message);
ForegroundColor = previous;
}
}
}
}
9 changes: 6 additions & 3 deletions src/coverlet.console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using ConsoleTables;
using Coverlet.Console.Logging;
using Coverlet.Core;
Expand Down Expand Up @@ -49,7 +50,7 @@ static int Main(string[] args)
if (!target.HasValue())
throw new CommandParsingException(app, "Target must be specified.");

Coverage coverage = new Coverage(module.Value, includeFilters.Values.ToArray(), includeDirectories.Values.ToArray(), excludeFilters.Values.ToArray(), excludedSourceFiles.Values.ToArray(), excludeAttributes.Values.ToArray(), singleHit.HasValue(), mergeWith.Value(), useSourceLink.HasValue());
Coverage coverage = new Coverage(module.Value, includeFilters.Values.ToArray(), includeDirectories.Values.ToArray(), excludeFilters.Values.ToArray(), excludedSourceFiles.Values.ToArray(), excludeAttributes.Values.ToArray(), singleHit.HasValue(), mergeWith.Value(), useSourceLink.HasValue(), logger);
coverage.PrepareModules();

Process process = new Process();
Expand All @@ -59,8 +60,10 @@ static int Main(string[] args)
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
logger.LogInformation(process.StandardOutput.ReadToEnd());
logger.LogError(process.StandardError.ReadToEnd());

process.OutputDataReceived += (sender, eventArgs) => logger.LogInformation(eventArgs.Data);
process.ErrorDataReceived += (sender, eventArgs) => logger.LogError(eventArgs.Data);

process.WaitForExit();

var dOutput = output.HasValue() ? output.Value() : Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar.ToString();
Expand Down
35 changes: 23 additions & 12 deletions src/coverlet.core/Coverage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
using System.IO;
using System.Linq;

using Coverlet.Core.Enums;
using Coverlet.Core.Helpers;
using Coverlet.Core.Instrumentation;
using Coverlet.Core.Symbols;
using Coverlet.Core.Logging;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand All @@ -15,24 +14,34 @@ namespace Coverlet.Core
{
public class Coverage
{
private string _module;
private string _identifier;
private readonly string _module;
private readonly string _identifier;
private string[] _includeFilters;
private string[] _includeDirectories;
private readonly string[] _includeDirectories;
private string[] _excludeFilters;
private string[] _excludedSourceFiles;
private string[] _excludeAttributes;
private bool _singleHit;
private string _mergeWith;
private bool _useSourceLink;
private readonly string[] _excludedSourceFiles;
private readonly string[] _excludeAttributes;
private readonly bool _singleHit;
private readonly string _mergeWith;
private readonly bool _useSourceLink;
private readonly ILogger _logger;
private List<InstrumenterResult> _results;

public string Identifier
{
get { return _identifier; }
}

public Coverage(string module, string[] includeFilters, string[] includeDirectories, string[] excludeFilters, string[] excludedSourceFiles, string[] excludeAttributes, bool singleHit, string mergeWith, bool useSourceLink)
public Coverage(string module,
string[] includeFilters,
string[] includeDirectories,
string[] excludeFilters,
string[] excludedSourceFiles,
string[] excludeAttributes,
bool singleHit,
string mergeWith,
bool useSourceLink,
ILogger logger)
{
_module = module;
_includeFilters = includeFilters;
Expand All @@ -43,6 +52,7 @@ public Coverage(string module, string[] includeFilters, string[] includeDirector
_singleHit = singleHit;
_mergeWith = mergeWith;
_useSourceLink = useSourceLink;
_logger = logger;

_identifier = Guid.NewGuid().ToString();
_results = new List<InstrumenterResult>();
Expand Down Expand Up @@ -72,9 +82,10 @@ public void PrepareModules()
var result = instrumenter.Instrument();
_results.Add(result);
}
catch (Exception)
catch (Exception ex)
{
// TODO: With verbose logging we should note that instrumentation failed.
_logger.LogWarning($"Unable to instrument module: {module} because : {ex.Message}");
InstrumentationHelper.RestoreOriginalModule(module, _identifier);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
namespace Coverlet.Console.Logging
using System;

namespace Coverlet.Core.Logging
{
interface ILogger
public interface ILogger
{
void LogSuccess(string message);

void LogVerbose(string message);

void LogInformation(string message);

void LogWarning(string message);

void LogError(string message);

void LogError(string message, Exception ex);
}
}
}
10 changes: 8 additions & 2 deletions src/coverlet.msbuild.tasks/InstrumentationTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class InstrumentationTask : Task
private bool _singleHit;
private string _mergeWith;
private bool _useSourceLink;
private readonly MSBuildLogger _logger;

internal static Coverage Coverage
{
Expand Down Expand Up @@ -78,6 +79,11 @@ public bool UseSourceLink
set { _useSourceLink = value; }
}

public InstrumentationTask()
{
_logger = new MSBuildLogger(Log);
}

public override bool Execute()
{
try
Expand All @@ -88,12 +94,12 @@ public override bool Execute()
var excludedSourceFiles = _excludeByFile?.Split(',');
var excludeAttributes = _excludeByAttribute?.Split(',');

_coverage = new Coverage(_path, includeFilters, includeDirectories, excludeFilters, excludedSourceFiles, excludeAttributes, _singleHit, _mergeWith, _useSourceLink);
_coverage = new Coverage(_path, includeFilters, includeDirectories, excludeFilters, excludedSourceFiles, excludeAttributes, _singleHit, _mergeWith, _useSourceLink, _logger);
_coverage.PrepareModules();
}
catch (Exception ex)
{
Log.LogErrorFromException(ex);
_logger.LogError(string.Empty, ex);
return false;
}

Expand Down
29 changes: 29 additions & 0 deletions src/coverlet.msbuild.tasks/MSBuildLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using ILogger = Coverlet.Core.Logging.ILogger;

namespace Coverlet.MSbuild.Tasks
{
class MSBuildLogger : ILogger
{
private readonly TaskLoggingHelper _log;

public MSBuildLogger(TaskLoggingHelper log)
{
_log = log;
}

public void LogSuccess(string message) => LogInformation(message);

public void LogVerbose(string message) => _log.LogMessageFromText(message, MessageImportance.Low);

public void LogInformation(string message)=> _log.LogMessage(message);

public void LogWarning(string message) => _log.LogWarning(message);

public void LogError(string message) => _log.LogError(message);

public void LogError(string message, Exception exception) => _log.LogErrorFromException(exception);
}
}
8 changes: 4 additions & 4 deletions test/coverlet.core.tests/CoverageTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.IO;

using Coverlet.Core.Logging;
using Xunit;
using Moq;

using Coverlet.Core;
using System.Collections.Generic;

namespace Coverlet.Core.Tests
{
Expand All @@ -22,9 +20,11 @@ public void TestCoverage()
File.Copy(module, Path.Combine(directory.FullName, Path.GetFileName(module)), true);
File.Copy(pdb, Path.Combine(directory.FullName, Path.GetFileName(pdb)), true);

var logger = Mock.Of<ILogger>();

// TODO: Find a way to mimick hits

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);
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);
coverage.PrepareModules();

var result = coverage.GetCoverageResult();
Expand Down