xamarin-macios/msbuild/Xamarin.MacDev.Tasks/LoggingExtensions.cs

107 строки
3.6 KiB
C#
Исходник Обычный вид История

using System;
using System.Collections.Generic;
using Microsoft.Build.Framework;
2016-04-21 16:40:25 +03:00
using Microsoft.Build.Utilities;
namespace Xamarin.MacDev.Tasks {
public static class LoggingExtensions {
2016-04-21 16:40:25 +03:00
const MessageImportance TaskPropertyImportance = MessageImportance.Normal;
internal static readonly string ErrorPrefix;
static LoggingExtensions ()
{
var name = typeof (LoggingExtensions).Assembly.GetName ();
switch (name.Name) {
case "Xamarin.Mac.Tasks":
ErrorPrefix = "MM";
break;
default:
ErrorPrefix = "MT";
break;
}
}
2016-04-21 16:40:25 +03:00
public static void LogTaskProperty (this TaskLoggingHelper log, string propertyName, ITaskItem [] items)
2016-04-21 16:40:25 +03:00
{
if (items == null) {
log.LogMessage (TaskPropertyImportance, " {0}: <null>", propertyName);
return;
}
log.LogMessage (TaskPropertyImportance, " {0}:", propertyName);
for (int i = 0; i < items.Length; i++)
log.LogMessage (TaskPropertyImportance, " {0}", items [i].ItemSpec);
2016-04-21 16:40:25 +03:00
}
public static void LogTaskProperty (this TaskLoggingHelper log, string propertyName, ITaskItem item)
{
if (item != null)
log.LogMessage (TaskPropertyImportance, " {0}: {1}", propertyName, item.ItemSpec);
else
log.LogMessage (TaskPropertyImportance, " {0}: ", propertyName);
}
public static void LogTaskProperty (this TaskLoggingHelper log, string propertyName, string [] items)
2016-04-21 16:40:25 +03:00
{
if (items == null) {
log.LogMessage (TaskPropertyImportance, " {0}: <null>", propertyName);
return;
}
log.LogMessage (TaskPropertyImportance, " {0}:", propertyName);
for (int i = 0; i < items.Length; i++)
log.LogMessage (TaskPropertyImportance, " {0}", items [i]);
2016-04-21 16:40:25 +03:00
}
public static void LogTaskProperty (this TaskLoggingHelper log, string propertyName, string value)
{
log.LogMessage (TaskPropertyImportance, " {0}: {1}", propertyName, value ?? "<null>");
}
public static void LogTaskProperty (this TaskLoggingHelper log, string propertyName, bool value)
{
log.LogMessage (TaskPropertyImportance, " {0}: {1}", propertyName, value);
}
public static void LogTaskProperty (this TaskLoggingHelper log, string propertyName, int value)
{
log.LogMessage (TaskPropertyImportance, " {0}: {1}", propertyName, value);
}
/// <summary>
/// Creates an MSBuild error following our MTErrors convention.</summary>
/// <remarks>
/// For every new error we need to update "docs/website/mtouch-errors.md" and "tools/mtouch/error.cs".</remarks>
/// <param name="errorCode">In the 7xxx range for MSBuild error.</param>
/// <param name="message">The error's message to be displayed in the error pad.</param>
/// <param name="fileName">Path to the known guilty file or null.</param>
public static void LogError (this TaskLoggingHelper log, int errorCode, string fileName, string message, params object [] args)
{
log.LogError (null, $"{ErrorPrefix}{errorCode}", null, fileName ?? "MSBuild", 0, 0, 0, 0, message, args);
}
public static void LogWarning (this TaskLoggingHelper log, int errorCode, string fileName, string message, params object [] args)
{
log.LogWarning (null, $"{ErrorPrefix}{errorCode}", null, fileName ?? "MSBuild", 0, 0, 0, 0, message, args);
}
public static bool LogErrorsFromException (this TaskLoggingHelper log, Exception exception, bool showStackTrace = true, bool showDetail = true)
{
var exceptions = new List<Exception> ();
if (exception is AggregateException ae) {
exceptions.AddRange (ae.InnerExceptions);
} else {
exceptions.Add (exception);
}
foreach (var e in exceptions) {
log.LogErrorFromException (e, showStackTrace, showDetail, null);
}
return false;
}
2016-04-21 16:40:25 +03:00
}
}