-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathFullTracking.cs
More file actions
167 lines (145 loc) · 5.92 KB
/
Copy pathFullTracking.cs
File metadata and controls
167 lines (145 loc) · 5.92 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#if FEATURE_FILE_TRACKER
using System;
using System.IO;
using Microsoft.Build.Collections;
using Microsoft.Build.Execution;
using Microsoft.Build.Shared;
#nullable disable
namespace Microsoft.Build.BackEnd
{
/// <summary>
/// Manages full tracking activation and suspension.
/// </summary>
internal class FullTracking : IDisposable
{
/// <summary>
/// The default name of the MSBuild property to read for the relative path to the full tracking .tlog files.
/// If this property isn't set in the project, full tracking is turned off.
/// </summary>
private const string FullTrackingDirectoryPropertyName = "MSBuildFullTrackingPath";
/// <summary>
/// The full path to where full tracking .tlog files should be written.
/// </summary>
private string _tlogDirectory;
/// <summary>
/// A value indicating whether this instance is tracking a full tracking suspension
/// (as opposed to activation).
/// </summary>
private TrackingMode _trackingMode;
/// <summary>
/// The name of the task as given to FileTracker.dll.
/// </summary>
private string _taskName;
/// <summary>
/// Initializes a new instance of the <see cref="FullTracking"/> class.
/// </summary>
private FullTracking()
{
_trackingMode = TrackingMode.None;
}
/// <summary>
/// The state of the <see cref="FullTracking"/> object regarding whether it is actively tracking or suspending tracking.
/// </summary>
private enum TrackingMode
{
/// <summary>
/// No tracking or suspension operation is in effect as a result of this instance.
/// </summary>
None,
/// <summary>
/// This instance has invoked full tracking.
/// </summary>
Active,
/// <summary>
/// This instance has suspended full tracking.
/// </summary>
Suspended,
}
/// <summary>
/// Disposes the FullTracking object, causing full tracking to end, or resume,
/// depending on how this object was created.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Starts full tracking.
/// </summary>
/// <param name="targetName">taskLoggingContext.TargetLoggingContext.Target.Name</param>
/// <param name="taskName">taskNode.Name</param>
/// <param name="projectRootDirectory">buildRequestEntry.ProjectRootDirectory</param>
/// <param name="projectProperties">buildRequestEntry.RequestConfiguration.Project.PropertiesToBuildWith</param>
/// <returns>
/// An object that will stop full tracking when disposed.
/// </returns>
internal static IDisposable Track(string targetName, string taskName, string projectRootDirectory, PropertyDictionary<ProjectPropertyInstance> projectProperties)
{
FullTracking tracking = new FullTracking();
ProjectPropertyInstance tlogRelativeDirectoryProperty = projectProperties[FullTrackingDirectoryPropertyName];
string tlogRelativeDirectoryValue = null;
if (tlogRelativeDirectoryProperty != null)
{
tlogRelativeDirectoryValue = tlogRelativeDirectoryProperty.EvaluatedValue;
}
if (!String.IsNullOrEmpty(tlogRelativeDirectoryValue))
{
tracking._taskName = GenerateUniqueTaskName(targetName, taskName);
tracking._tlogDirectory = Path.Combine(projectRootDirectory, tlogRelativeDirectoryValue);
InprocTrackingNativeMethods.StartTrackingContext(tracking._tlogDirectory, tracking._taskName);
tracking._trackingMode = TrackingMode.Active;
}
return tracking;
}
/// <summary>
/// Suspends full tracking.
/// </summary>
/// <returns>An object that will resume full tracking when disposed.</returns>
internal static IDisposable Suspend()
{
FullTracking tracking = new FullTracking();
if (tracking._trackingMode == TrackingMode.Active)
{
InprocTrackingNativeMethods.SuspendTracking();
tracking._trackingMode = TrackingMode.Suspended;
}
return tracking;
}
/// <summary>
/// Disposes the FullTracking object, causing full tracking to end, or resume,
/// depending on how this object was created.
/// </summary>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
switch (_trackingMode)
{
case TrackingMode.Active:
// Stop tracking
InprocTrackingNativeMethods.WriteContextTLogs(_tlogDirectory, _taskName);
InprocTrackingNativeMethods.EndTrackingContext();
break;
case TrackingMode.Suspended:
// Stop suspending tracking
InprocTrackingNativeMethods.ResumeTracking();
break;
default:
// nothing to do here if we weren't actively tracking or suspended.
break;
}
}
}
/// <summary>
/// Gets the task name to pass to Tracker.
/// </summary>
private static string GenerateUniqueTaskName(string targetName, string taskName)
{
return "__FT__" + targetName + "-" + taskName;
}
}
}
#endif