Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 28 additions & 10 deletions src/coverlet.console/Logging/ConsoleLogger.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
using System;
using Coverlet.Core;
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 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 +45,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;
}
}
}
}
11 changes: 8 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,12 @@ 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.StandardOutput.ReadToEndAsync()
.ContinueWith(task => logger.LogInformation(task.Result), TaskContinuationOptions.OnlyOnRanToCompletion);
process.StandardError.ReadToEndAsync()
.ContinueWith(task => logger.LogError(task.Result), TaskContinuationOptions.OnlyOnRanToCompletion);

process.WaitForExit();

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

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

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand All @@ -25,14 +23,24 @@ public class Coverage
private bool _singleHit;
private string _mergeWith;
private 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 +51,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 +81,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,15 @@
namespace Coverlet.Console.Logging
namespace Coverlet.Core
{
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);
}
}
}
29 changes: 29 additions & 0 deletions src/coverlet.core/Logging/NullLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Coverlet.Core;

namespace coverlet.console.Logging
{
public sealed class NullLogger :ILogger
{
public static ILogger Instance {get; } = new NullLogger();

public void LogSuccess(string message)
{
}

public void LogVerbose(string message)
{
}

public void LogInformation(string message)
{
}

public void LogWarning(string message)
{
}

public void LogError(string message)
{
}
}
}
2 changes: 1 addition & 1 deletion src/coverlet.msbuild.tasks/InstrumentationTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ 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, new MSBuildLogger(Log));
_coverage.PrepareModules();
}
catch (Exception ex)
Expand Down
41 changes: 41 additions & 0 deletions src/coverlet.msbuild.tasks/MSBuildLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using ILogger = Coverlet.Core.ILogger;

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

public MSBuildLogger(TaskLoggingHelper _log)
{
this._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.LogMessageFromText(message, MessageImportance.Normal);
}

public void LogWarning(string message)
{
_log.LogWarning(message);
}

public void LogError(string message)
{
_log.LogError(message);
}
}
}
3 changes: 2 additions & 1 deletion test/coverlet.core.tests/CoverageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using Coverlet.Core;
using System.Collections.Generic;
using coverlet.console.Logging;

namespace Coverlet.Core.Tests
{
Expand All @@ -24,7 +25,7 @@ public void TestCoverage()

// 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, NullLogger.Instance);
coverage.PrepareModules();

var result = coverage.GetCoverageResult();
Expand Down