diff options
| author | Rob Mensching <rob@firegiant.com> | 2017-12-19 12:25:40 -0800 |
|---|---|---|
| committer | Rob Mensching <rob@firegiant.com> | 2017-12-19 12:25:40 -0800 |
| commit | 155a6e96346e0cb3d9ab6f5372fa29b46ebaee89 (patch) | |
| tree | 59d1f151bfde8068b6014b05b5c8cfea3402c974 /src/WixToolset.Core/ExtensibilityServices/Messaging.cs | |
| parent | 6f1665ed759b31bd095f186f9239232c653597cd (diff) | |
| download | wix-155a6e96346e0cb3d9ab6f5372fa29b46ebaee89.tar.gz wix-155a6e96346e0cb3d9ab6f5372fa29b46ebaee89.tar.bz2 wix-155a6e96346e0cb3d9ab6f5372fa29b46ebaee89.zip | |
Integrate simplified message handling
Diffstat (limited to 'src/WixToolset.Core/ExtensibilityServices/Messaging.cs')
| -rw-r--r-- | src/WixToolset.Core/ExtensibilityServices/Messaging.cs | 194 |
1 files changed, 194 insertions, 0 deletions
diff --git a/src/WixToolset.Core/ExtensibilityServices/Messaging.cs b/src/WixToolset.Core/ExtensibilityServices/Messaging.cs new file mode 100644 index 00000000..4510f264 --- /dev/null +++ b/src/WixToolset.Core/ExtensibilityServices/Messaging.cs | |||
| @@ -0,0 +1,194 @@ | |||
| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. | ||
| 2 | |||
| 3 | namespace WixToolset.Core.ExtensibilityServices | ||
| 4 | { | ||
| 5 | using System; | ||
| 6 | using System.Collections.Generic; | ||
| 7 | using System.Globalization; | ||
| 8 | using System.Text; | ||
| 9 | using WixToolset.Data; | ||
| 10 | using WixToolset.Extensibility; | ||
| 11 | using WixToolset.Extensibility.Services; | ||
| 12 | |||
| 13 | internal class Messaging : IMessaging | ||
| 14 | { | ||
| 15 | private IMessageListener listener; | ||
| 16 | private HashSet<int> suppressedWarnings = new HashSet<int>(); | ||
| 17 | private HashSet<int> warningsAsErrors = new HashSet<int>(); | ||
| 18 | |||
| 19 | public bool EncounteredError { get; private set; } | ||
| 20 | |||
| 21 | public int LastErrorNumber { get; private set; } | ||
| 22 | |||
| 23 | public bool ShowVerboseMessages { get; set; } | ||
| 24 | |||
| 25 | public bool SuppressAllWarnings { get; set; } | ||
| 26 | |||
| 27 | public bool WarningsAsError { get; set; } | ||
| 28 | |||
| 29 | public void ElevateWarningMessage(int warningNumber) | ||
| 30 | { | ||
| 31 | this.warningsAsErrors.Add(warningNumber); | ||
| 32 | } | ||
| 33 | |||
| 34 | public void SetListener(IMessageListener listener) | ||
| 35 | { | ||
| 36 | this.listener = listener; | ||
| 37 | } | ||
| 38 | |||
| 39 | public void SuppressWarningMessage(int warningNumber) | ||
| 40 | { | ||
| 41 | this.suppressedWarnings.Add(warningNumber); | ||
| 42 | } | ||
| 43 | |||
| 44 | public string FormatMessage(Message message) | ||
| 45 | { | ||
| 46 | var level = CalculateMessageLevel(message); | ||
| 47 | |||
| 48 | if (level == MessageLevel.Nothing) | ||
| 49 | { | ||
| 50 | return String.Empty; | ||
| 51 | } | ||
| 52 | |||
| 53 | var shortAppName = String.IsNullOrEmpty(this.listener?.ShortAppName) ? "WIX" : this.listener.ShortAppName; | ||
| 54 | var longAppName = String.IsNullOrEmpty(this.listener?.LongAppName) ? "WIX" : this.listener.LongAppName; | ||
| 55 | |||
| 56 | var fileNames = new List<string>(); | ||
| 57 | var errorFileName = longAppName; | ||
| 58 | for (var sln = message.SourceLineNumbers; null != sln; sln = sln.Parent) | ||
| 59 | { | ||
| 60 | if (String.IsNullOrEmpty(sln.FileName)) | ||
| 61 | { | ||
| 62 | continue; | ||
| 63 | } | ||
| 64 | else if (sln.LineNumber.HasValue) | ||
| 65 | { | ||
| 66 | if (fileNames.Count == 0) | ||
| 67 | { | ||
| 68 | errorFileName = String.Format(CultureInfo.CurrentUICulture, WixStrings.Format_FirstLineNumber, sln.FileName, sln.LineNumber); | ||
| 69 | } | ||
| 70 | |||
| 71 | fileNames.Add(String.Format(CultureInfo.CurrentUICulture, WixStrings.Format_LineNumber, sln.FileName, sln.LineNumber)); | ||
| 72 | } | ||
| 73 | else | ||
| 74 | { | ||
| 75 | if (fileNames.Count == 0) | ||
| 76 | { | ||
| 77 | errorFileName = sln.FileName; | ||
| 78 | } | ||
| 79 | |||
| 80 | fileNames.Add(sln.FileName); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | var levelString = String.Empty; | ||
| 85 | if (MessageLevel.Warning == level) | ||
| 86 | { | ||
| 87 | levelString = WixStrings.MessageType_Warning; | ||
| 88 | } | ||
| 89 | else if (MessageLevel.Error == level) | ||
| 90 | { | ||
| 91 | levelString = WixStrings.MessageType_Error; | ||
| 92 | } | ||
| 93 | |||
| 94 | string formatted; | ||
| 95 | if (message.ResourceManager == null) | ||
| 96 | { | ||
| 97 | formatted = String.Format(CultureInfo.InvariantCulture, message.ResourceNameOrFormat, message.MessageArgs); | ||
| 98 | } | ||
| 99 | else | ||
| 100 | { | ||
| 101 | formatted = String.Format(CultureInfo.InvariantCulture, message.ResourceManager.GetString(message.ResourceNameOrFormat), message.MessageArgs); | ||
| 102 | } | ||
| 103 | |||
| 104 | var builder = new StringBuilder(); | ||
| 105 | if (level == MessageLevel.Information || level == MessageLevel.Verbose) | ||
| 106 | { | ||
| 107 | builder.AppendFormat(WixStrings.Format_InfoMessage, formatted); | ||
| 108 | } | ||
| 109 | else | ||
| 110 | { | ||
| 111 | builder.AppendFormat(WixStrings.Format_NonInfoMessage, errorFileName, levelString, shortAppName, message.Id, formatted); | ||
| 112 | } | ||
| 113 | |||
| 114 | if (fileNames.Count > 1) | ||
| 115 | { | ||
| 116 | builder.AppendFormat(WixStrings.INF_SourceTrace, Environment.NewLine); | ||
| 117 | |||
| 118 | foreach (var fileName in fileNames) | ||
| 119 | { | ||
| 120 | builder.AppendFormat(WixStrings.INF_SourceTraceLocation, fileName, Environment.NewLine); | ||
| 121 | } | ||
| 122 | |||
| 123 | builder.AppendLine(); | ||
| 124 | } | ||
| 125 | |||
| 126 | return builder.ToString(); | ||
| 127 | } | ||
| 128 | |||
| 129 | public void Write(Message message) | ||
| 130 | { | ||
| 131 | var level = CalculateMessageLevel(message); | ||
| 132 | |||
| 133 | if (level == MessageLevel.Nothing) | ||
| 134 | { | ||
| 135 | return; | ||
| 136 | } | ||
| 137 | |||
| 138 | if (level == MessageLevel.Error) | ||
| 139 | { | ||
| 140 | this.EncounteredError = true; | ||
| 141 | this.LastErrorNumber = message.Id; | ||
| 142 | } | ||
| 143 | |||
| 144 | if (this.listener != null) | ||
| 145 | { | ||
| 146 | this.listener.Write(message); | ||
| 147 | } | ||
| 148 | else if (level == MessageLevel.Error) | ||
| 149 | { | ||
| 150 | throw new WixException(message); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | public void Write(string message, bool verbose = false) | ||
| 155 | { | ||
| 156 | if (!verbose || this.ShowVerboseMessages) | ||
| 157 | { | ||
| 158 | this.listener?.Write(message); | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | /// <summary> | ||
| 163 | /// Determines the level of this message, when taking into account warning-as-error, | ||
| 164 | /// warning level, verbosity level and message suppressed by the caller. | ||
| 165 | /// </summary> | ||
| 166 | /// <param name="message">Event arguments for the message.</param> | ||
| 167 | /// <returns>MessageLevel representing the level of this message.</returns> | ||
| 168 | private MessageLevel CalculateMessageLevel(Message message) | ||
| 169 | { | ||
| 170 | var level = message.Level; | ||
| 171 | |||
| 172 | if (level == MessageLevel.Verbose) | ||
| 173 | { | ||
| 174 | if (!this.ShowVerboseMessages) | ||
| 175 | { | ||
| 176 | level = MessageLevel.Nothing; | ||
| 177 | } | ||
| 178 | } | ||
| 179 | else if (level == MessageLevel.Warning) | ||
| 180 | { | ||
| 181 | if (this.SuppressAllWarnings || this.suppressedWarnings.Contains(message.Id)) | ||
| 182 | { | ||
| 183 | level = MessageLevel.Nothing; | ||
| 184 | } | ||
| 185 | else if (this.WarningsAsError || this.warningsAsErrors.Contains(message.Id)) | ||
| 186 | { | ||
| 187 | level = MessageLevel.Error; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | return level; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | } | ||
