PageRenderTime 68ms CodeModel.GetById 50ms RepoModel.GetById 1ms app.codeStats 0ms

/src/XMakeBuildEngine/BackEnd/Components/Logging/ProjectLoggingContext.cs

https://gitlab.com/Banul/la-project1
C# | 272 lines | 156 code | 27 blank | 89 comment | 17 complexity | 2ecc630b833aaa7775610e23c434a1f7 MD5 | raw file
  1. // Copyright (c) Microsoft. All rights reserved.
  2. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
  3. //-----------------------------------------------------------------------
  4. // </copyright>
  5. // <summary>A logging context for projects.</summary>
  6. //-----------------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Diagnostics;
  10. using System.Text;
  11. using Microsoft.Build.Framework;
  12. using Microsoft.Build.Execution;
  13. using System.Collections;
  14. using Microsoft.Build.Shared;
  15. using TaskItem = Microsoft.Build.Execution.ProjectItemInstance.TaskItem;
  16. using Microsoft.Build.Collections;
  17. namespace Microsoft.Build.BackEnd.Logging
  18. {
  19. /// <summary>
  20. /// A logging context for a project.
  21. /// </summary>
  22. internal class ProjectLoggingContext : BaseLoggingContext
  23. {
  24. /// <summary>
  25. /// The project's full path
  26. /// </summary>
  27. private string _projectFullPath;
  28. /// <summary>
  29. /// The parent node logging context this context was derived from.
  30. /// </summary>
  31. private NodeLoggingContext _nodeLoggingContext;
  32. /// <summary>
  33. /// Constructs a project logging context.
  34. /// </summary>
  35. internal ProjectLoggingContext(NodeLoggingContext nodeLoggingContext, BuildRequestEntry requestEntry, BuildEventContext parentBuildEventContext)
  36. : this
  37. (
  38. nodeLoggingContext,
  39. requestEntry.Request.SubmissionId,
  40. requestEntry.Request.ConfigurationId,
  41. requestEntry.RequestConfiguration.ProjectFullPath,
  42. requestEntry.Request.Targets,
  43. requestEntry.RequestConfiguration.ToolsVersion,
  44. requestEntry.RequestConfiguration.Project.PropertiesToBuildWith,
  45. requestEntry.RequestConfiguration.Project.ItemsToBuildWith,
  46. parentBuildEventContext
  47. )
  48. {
  49. }
  50. /// <summary>
  51. /// Constructs a project logging context.
  52. /// </summary>
  53. internal ProjectLoggingContext(NodeLoggingContext nodeLoggingContext, BuildRequest request, string projectFullPath, string toolsVersion, BuildEventContext parentBuildEventContext)
  54. : this
  55. (
  56. nodeLoggingContext,
  57. request.SubmissionId,
  58. request.ConfigurationId,
  59. projectFullPath,
  60. request.Targets,
  61. toolsVersion,
  62. null,
  63. null,
  64. parentBuildEventContext
  65. )
  66. {
  67. }
  68. /// <summary>
  69. /// Constructs a project logging contexts.
  70. /// </summary>
  71. private ProjectLoggingContext(NodeLoggingContext nodeLoggingContext, int submissionId, int configurationId, string projectFullPath, List<string> targets, string toolsVersion, PropertyDictionary<ProjectPropertyInstance> projectProperties, ItemDictionary<ProjectItemInstance> projectItems, BuildEventContext parentBuildEventContext)
  72. : base(nodeLoggingContext)
  73. {
  74. _nodeLoggingContext = nodeLoggingContext;
  75. _projectFullPath = projectFullPath;
  76. ProjectPropertyInstanceEnumeratorProxy properties = null;
  77. ProjectItemInstanceEnumeratorProxy items = null;
  78. IEnumerable<ProjectPropertyInstance> projectPropertiesEnumerator = projectProperties == null ? Collections.ReadOnlyEmptyList<ProjectPropertyInstance>.Instance : null;
  79. IEnumerable<ProjectItemInstance> projectItemsEnumerator = projectItems == null ? Collections.ReadOnlyEmptyList<ProjectItemInstance>.Instance : null;
  80. string[] propertiesToSerialize = LoggingService.PropertiesToSerialize;
  81. // If we are only logging critical events lets not pass back the items or properties
  82. if (!LoggingService.OnlyLogCriticalEvents && (!LoggingService.RunningOnRemoteNode || LoggingService.SerializeAllProperties))
  83. {
  84. if (projectProperties != null)
  85. {
  86. projectPropertiesEnumerator = projectProperties.GetCopyOnReadEnumerable();
  87. }
  88. if (projectItems != null)
  89. {
  90. projectItemsEnumerator = projectItems.GetCopyOnReadEnumerable();
  91. }
  92. properties = new ProjectPropertyInstanceEnumeratorProxy(projectPropertiesEnumerator);
  93. items = new ProjectItemInstanceEnumeratorProxy(projectItemsEnumerator);
  94. }
  95. if (projectProperties != null && propertiesToSerialize != null && propertiesToSerialize.Length > 0 && !LoggingService.SerializeAllProperties)
  96. {
  97. PropertyDictionary<ProjectPropertyInstance> projectPropertiesToSerialize = new PropertyDictionary<ProjectPropertyInstance>();
  98. foreach (string propertyToGet in propertiesToSerialize)
  99. {
  100. ProjectPropertyInstance instance = projectProperties[propertyToGet];
  101. {
  102. if (instance != null)
  103. {
  104. projectPropertiesToSerialize.Set(instance);
  105. }
  106. }
  107. }
  108. properties = new ProjectPropertyInstanceEnumeratorProxy(projectPropertiesToSerialize);
  109. }
  110. this.BuildEventContext = LoggingService.LogProjectStarted
  111. (
  112. nodeLoggingContext.BuildEventContext,
  113. submissionId,
  114. configurationId,
  115. parentBuildEventContext,
  116. projectFullPath,
  117. String.Join(";", targets.ToArray()),
  118. properties,
  119. items
  120. );
  121. LoggingService.LogComment(this.BuildEventContext, MessageImportance.Low, "ToolsVersionInEffectForBuild", toolsVersion);
  122. this.IsValid = true;
  123. }
  124. /// <summary>
  125. /// Retrieves the node logging context.
  126. /// </summary>
  127. internal NodeLoggingContext NodeLoggingContext
  128. {
  129. get
  130. {
  131. return _nodeLoggingContext;
  132. }
  133. }
  134. /// <summary>
  135. /// Log that the project has finished
  136. /// </summary>
  137. /// <param name="success">Did the build succeede or not</param>
  138. internal void LogProjectFinished(bool success)
  139. {
  140. ErrorUtilities.VerifyThrow(this.IsValid, "invalid");
  141. LoggingService.LogProjectFinished(BuildEventContext, _projectFullPath, success);
  142. this.IsValid = false;
  143. }
  144. /// <summary>
  145. /// Log that a target has started
  146. /// </summary>
  147. internal TargetLoggingContext LogTargetBatchStarted(string projectFullPath, ProjectTargetInstance target, string parentTargetName)
  148. {
  149. ErrorUtilities.VerifyThrow(this.IsValid, "invalid");
  150. return new TargetLoggingContext(this, projectFullPath, target, parentTargetName);
  151. }
  152. /// <summary>
  153. /// An enumerable wrapper for items that clones items as they are requested,
  154. /// so that writes have no effect on the items.
  155. /// </summary>
  156. /// <remarks>
  157. /// This class is designed to be passed to loggers.
  158. /// The expense of copying items is only incurred if and when
  159. /// a logger chooses to enumerate over it.
  160. /// The type of the items enumerated over is imposed by backwards compatibility for ProjectStartedEvent.
  161. /// </remarks>
  162. private class ProjectItemInstanceEnumeratorProxy : IEnumerable<DictionaryEntry>
  163. {
  164. /// <summary>
  165. /// Enumerable that this proxies
  166. /// </summary>
  167. private IEnumerable<ProjectItemInstance> _backingItems;
  168. /// <summary>
  169. /// Constructor
  170. /// </summary>
  171. /// <param name="backingItems">Enumerator this class should proxy</param>
  172. internal ProjectItemInstanceEnumeratorProxy(IEnumerable<ProjectItemInstance> backingItems)
  173. {
  174. _backingItems = backingItems;
  175. }
  176. /// <summary>
  177. /// Returns an enumerator that provides copies of the items
  178. /// in the backing store.
  179. /// Each dictionary entry has key of the item type and value of an ITaskItem.
  180. /// Type of the enumerator is imposed by backwards compatibility for ProjectStartedEvent.
  181. /// </summary>
  182. public IEnumerator<DictionaryEntry> GetEnumerator()
  183. {
  184. foreach (ProjectItemInstance item in _backingItems)
  185. {
  186. yield return new DictionaryEntry(item.ItemType, new TaskItem(item));
  187. }
  188. }
  189. /// <summary>
  190. /// Returns an enumerator that provides copies of the items
  191. /// in the backing store.
  192. /// </summary>
  193. IEnumerator IEnumerable.GetEnumerator()
  194. {
  195. return (IEnumerator)GetEnumerator();
  196. }
  197. }
  198. /// <summary>
  199. /// An enumerable wrapper for properties that clones properties as they are requested,
  200. /// so that writes have no effect on the properties.
  201. /// </summary>
  202. /// <remarks>
  203. /// This class is designed to be passed to loggers.
  204. /// The expense of copying items is only incurred if and when
  205. /// a logger chooses to enumerate over it.
  206. /// The type of the items enumerated over is imposed by backwards compatibility for ProjectStartedEvent.
  207. /// </remarks>
  208. private class ProjectPropertyInstanceEnumeratorProxy : IEnumerable<DictionaryEntry>
  209. {
  210. /// <summary>
  211. /// Enumerable that this proxies
  212. /// </summary>
  213. private IEnumerable<ProjectPropertyInstance> _backingProperties;
  214. /// <summary>
  215. /// Constructor
  216. /// </summary>
  217. /// <param name="backingProperties">Enumerator this class should proxy</param>
  218. internal ProjectPropertyInstanceEnumeratorProxy(IEnumerable<ProjectPropertyInstance> backingProperties)
  219. {
  220. _backingProperties = backingProperties;
  221. }
  222. /// <summary>
  223. /// Returns an enumerator that provides copies of the properties
  224. /// in the backing store.
  225. /// Each DictionaryEntry has key of the property name and value of the property value.
  226. /// Type of the enumerator is imposed by backwards compatibility for ProjectStartedEvent.
  227. /// </summary>
  228. public IEnumerator<DictionaryEntry> GetEnumerator()
  229. {
  230. foreach (ProjectPropertyInstance property in _backingProperties)
  231. {
  232. yield return new DictionaryEntry(property.Name, property.EvaluatedValue);
  233. }
  234. }
  235. /// <summary>
  236. /// Returns an enumerator that provides copies of the properties
  237. /// in the backing store.
  238. /// </summary>
  239. IEnumerator IEnumerable.GetEnumerator()
  240. {
  241. return (IEnumerator)GetEnumerator();
  242. }
  243. }
  244. }
  245. }