summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Build.Tasks/Logger.cs
blob: b706e6100f139ef30704fb0b1709ba73daf3e2c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Xml;
using Microsoft.Build.Utilities;
using Xamarin.Forms.Xaml;

namespace Xamarin.Forms.Build.Tasks
{
	public class Logger {
		public TaskLoggingHelper Helper { get; }
		public int Verbosity { get; }

		public Logger(TaskLoggingHelper helper, int verbosity)
		{
			Verbosity = verbosity;
			Helper = helper;
		}

		string buffer = "";

		public void LogException(string subcategory, string errorCode, string helpKeyword, string file, Exception e)
		{
			var xpe = e as XamlParseException;
			var xe = e as XmlException;
			if (xpe != null)
				LogError(subcategory, errorCode, helpKeyword, file, xpe.XmlInfo.LineNumber, xpe.XmlInfo.LinePosition, 0, 0, xpe.Message, xpe.HelpLink, xpe.Source);
			else if (xe != null)
				LogError(subcategory, errorCode, helpKeyword, file, xe.LineNumber, xe.LinePosition, 0, 0, xe.Message, xe.HelpLink, xe.Source);
			else
				LogError(subcategory, errorCode, helpKeyword, file, 0, 0, 0, 0, e.Message, e.HelpLink, e.Source);
		}

		public void LogError(string subcategory, string errorCode, string helpKeyword, string file, int lineNumber,
			int columnNumber, int endLineNumber, int endColumnNumber, string message, params object [] messageArgs)
		{
			if (!string.IsNullOrEmpty(buffer))
				LogLine(-1, null, null);
			if (Helper != null) {
				Helper.LogError(subcategory, errorCode, helpKeyword, file, lineNumber, columnNumber, endLineNumber,
					endColumnNumber, message, messageArgs);
			} else
				Console.Error.WriteLine($"{file} ({lineNumber}:{columnNumber}) : {message}");
		}

		public void LogLine(int level, string format, params object [] arg)
		{
			if (!string.IsNullOrEmpty(buffer)) {
				format = buffer + format;
				buffer = "";
			}

			if (level < 0) {
				if (Helper != null)
					Helper.LogError(format, arg);
				else
					Console.Error.WriteLine(format, arg);
			} else if (level <= Verbosity) {
				if (Helper != null)
					Helper.LogMessage(format, arg);
				else
					Console.WriteLine(format, arg);
			}
		}

		public void LogString(int level, string format, params object [] arg)
		{
			if (level <= 0)
				Console.Error.Write(format, arg);
			else if (level <= Verbosity) {
				if (Helper != null)
					buffer += String.Format(format, arg);
				else
					Console.Write(format, arg);
			}
		}
	}
}