diff options
Diffstat (limited to 'src/WixToolset.Data/Messaging.cs')
-rw-r--r-- | src/WixToolset.Data/Messaging.cs | 173 |
1 files changed, 0 insertions, 173 deletions
diff --git a/src/WixToolset.Data/Messaging.cs b/src/WixToolset.Data/Messaging.cs deleted file mode 100644 index de2cdcfe..00000000 --- a/src/WixToolset.Data/Messaging.cs +++ /dev/null | |||
@@ -1,173 +0,0 @@ | |||
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.Data | ||
4 | { | ||
5 | using System; | ||
6 | using System.Collections.Generic; | ||
7 | |||
8 | public class Messaging : IMessageHandler | ||
9 | { | ||
10 | private static readonly Messaging instance = new Messaging(); | ||
11 | |||
12 | private HashSet<int> suppressedWarnings = new HashSet<int>(); | ||
13 | private HashSet<int> warningsAsErrors = new HashSet<int>(); | ||
14 | private string longAppName; | ||
15 | private string shortAppName; | ||
16 | |||
17 | static Messaging() | ||
18 | { | ||
19 | } | ||
20 | |||
21 | private Messaging() | ||
22 | { | ||
23 | } | ||
24 | |||
25 | public static Messaging Instance { get { return Messaging.instance; } } | ||
26 | |||
27 | /// <summary> | ||
28 | /// Event fired when messages are to be displayed. | ||
29 | /// </summary> | ||
30 | public event DisplayEventHandler Display; | ||
31 | |||
32 | /// <summary> | ||
33 | /// Gets a bool indicating whether an error has been found. | ||
34 | /// </summary> | ||
35 | /// <value>A bool indicating whether an error has been found.</value> | ||
36 | public bool EncounteredError { get; private set; } | ||
37 | |||
38 | /// <summary> | ||
39 | /// Gets the last error code encountered during messaging. | ||
40 | /// </summary> | ||
41 | /// <value>The exit code for the process.</value> | ||
42 | public int LastErrorNumber { get; private set; } | ||
43 | |||
44 | /// <summary> | ||
45 | /// Gets or sets the option to show verbose messages. | ||
46 | /// </summary> | ||
47 | /// <value>The option to show verbose messages.</value> | ||
48 | public bool ShowVerboseMessages { get; set; } | ||
49 | |||
50 | /// <summary> | ||
51 | /// Gets or sets the option to suppress all warning messages. | ||
52 | /// </summary> | ||
53 | /// <value>The option to suppress all warning messages.</value> | ||
54 | public bool SuppressAllWarnings { get; set; } | ||
55 | |||
56 | /// <summary> | ||
57 | /// Gets and sets the option to treat warnings as errors. | ||
58 | /// </summary> | ||
59 | /// <value>The option to treat warnings as errors.</value> | ||
60 | public bool WarningsAsError { get; set; } | ||
61 | |||
62 | /// <summary> | ||
63 | /// Implements IMessageHandler to display error messages. | ||
64 | /// </summary> | ||
65 | /// <param name="mea">Message event arguments.</param> | ||
66 | public void OnMessage(MessageEventArgs mea) | ||
67 | { | ||
68 | MessageLevel messageLevel = this.CalculateMessageLevel(mea); | ||
69 | |||
70 | if (MessageLevel.Nothing == messageLevel) | ||
71 | { | ||
72 | return; | ||
73 | } | ||
74 | else if (MessageLevel.Error == messageLevel) | ||
75 | { | ||
76 | this.EncounteredError = true; | ||
77 | this.LastErrorNumber = mea.Id; | ||
78 | } | ||
79 | |||
80 | if (null != this.Display) | ||
81 | { | ||
82 | string message = mea.GenerateMessageString(this.shortAppName, this.longAppName, messageLevel); | ||
83 | if (!String.IsNullOrEmpty(message)) | ||
84 | { | ||
85 | this.Display(this, new DisplayEventArgs() { Level = messageLevel, Message = message }); | ||
86 | } | ||
87 | } | ||
88 | else if (MessageLevel.Error == mea.Level) | ||
89 | { | ||
90 | throw new WixException(mea); | ||
91 | } | ||
92 | } | ||
93 | |||
94 | /// <summary> | ||
95 | /// Sets the app names. | ||
96 | /// </summary> | ||
97 | /// <param name="shortName">Short application name; usually 4 uppercase characters.</param> | ||
98 | /// <param name="longName">Long application name; usually the executable name.</param> | ||
99 | public Messaging InitializeAppName(string shortName, string longName) | ||
100 | { | ||
101 | this.EncounteredError = false; | ||
102 | this.LastErrorNumber = 0; | ||
103 | |||
104 | this.Display = null; | ||
105 | this.ShowVerboseMessages = false; | ||
106 | this.SuppressAllWarnings = false; | ||
107 | this.WarningsAsError = false; | ||
108 | this.suppressedWarnings.Clear(); | ||
109 | this.warningsAsErrors.Clear(); | ||
110 | |||
111 | this.shortAppName = shortName; | ||
112 | this.longAppName = longName; | ||
113 | |||
114 | return this; | ||
115 | } | ||
116 | |||
117 | /// <summary> | ||
118 | /// Adds a warning message id to be elevated to an error message. | ||
119 | /// </summary> | ||
120 | /// <param name="warningNumber">Id of the message to elevate.</param> | ||
121 | /// <remarks> | ||
122 | /// Suppressed warnings will not be elevated as errors. | ||
123 | /// </remarks> | ||
124 | public void ElevateWarningMessage(int warningNumber) | ||
125 | { | ||
126 | this.warningsAsErrors.Add(warningNumber); | ||
127 | } | ||
128 | |||
129 | /// <summary> | ||
130 | /// Adds a warning message id to be suppressed in message output. | ||
131 | /// </summary> | ||
132 | /// <param name="warningNumber">Id of the message to suppress.</param> | ||
133 | /// <remarks> | ||
134 | /// Suppressed warnings will not be elevated as errors. | ||
135 | /// </remarks> | ||
136 | public void SuppressWarningMessage(int warningNumber) | ||
137 | { | ||
138 | this.suppressedWarnings.Add(warningNumber); | ||
139 | } | ||
140 | |||
141 | /// <summary> | ||
142 | /// Determines the level of this message, when taking into account warning-as-error, | ||
143 | /// warning level, verbosity level and message suppressed by the caller. | ||
144 | /// </summary> | ||
145 | /// <param name="mea">Event arguments for the message.</param> | ||
146 | /// <returns>MessageLevel representing the level of this message.</returns> | ||
147 | private MessageLevel CalculateMessageLevel(MessageEventArgs mea) | ||
148 | { | ||
149 | MessageLevel messageLevel = mea.Level; | ||
150 | |||
151 | if (MessageLevel.Verbose == messageLevel) | ||
152 | { | ||
153 | if (!this.ShowVerboseMessages) | ||
154 | { | ||
155 | messageLevel = MessageLevel.Nothing; | ||
156 | } | ||
157 | } | ||
158 | else if (MessageLevel.Warning == messageLevel) | ||
159 | { | ||
160 | if (this.SuppressAllWarnings || this.suppressedWarnings.Contains(mea.Id)) | ||
161 | { | ||
162 | messageLevel = MessageLevel.Nothing; | ||
163 | } | ||
164 | else if (this.WarningsAsError || this.warningsAsErrors.Contains(mea.Id)) | ||
165 | { | ||
166 | messageLevel = MessageLevel.Error; | ||
167 | } | ||
168 | } | ||
169 | |||
170 | return messageLevel; | ||
171 | } | ||
172 | } | ||
173 | } | ||