[master] Update dependencies from dotnet/arcade dotnet/core-setup dotnet/corefx ...
[mono-project.git] / eng / common / pipeline-logging-functions.ps1
blobaf5f48aacebc6856a5fbd752a50524d8139067ab
1 # Source for this file was taken from https://github.com/microsoft/azure-pipelines-task-lib/blob/11c9439d4af17e6475d9fe058e6b2e03914d17e6/powershell/VstsTaskSdk/LoggingCommandFunctions.ps1 and modified.
3 # NOTE: You should not be calling these method directly as they are likely to change. Instead you should be calling the Write-Pipeline* functions defined in tools.ps1
5 $script:loggingCommandPrefix = '##vso['
6 $script:loggingCommandEscapeMappings = @( # TODO: WHAT ABOUT "="? WHAT ABOUT "%"?
7 New-Object psobject -Property @{ Token = ';' ; Replacement = '%3B' }
8 New-Object psobject -Property @{ Token = "`r" ; Replacement = '%0D' }
9 New-Object psobject -Property @{ Token = "`n" ; Replacement = '%0A' }
10 New-Object psobject -Property @{ Token = "]" ; Replacement = '%5D' }
12 # TODO: BUG: Escape % ???
13 # TODO: Add test to verify don't need to escape "=".
15 function Write-PipelineTelemetryError {
16 [CmdletBinding()]
17 param(
18 [Parameter(Mandatory = $true)]
19 [string]$Category,
20 [Parameter(Mandatory = $true)]
21 [string]$Message,
22 [Parameter(Mandatory = $false)]
23 [string]$Type = 'error',
24 [string]$ErrCode,
25 [string]$SourcePath,
26 [string]$LineNumber,
27 [string]$ColumnNumber,
28 [switch]$AsOutput)
30 $PSBoundParameters.Remove("Category") | Out-Null
32 $Message = "(NETCORE_ENGINEERING_TELEMETRY=$Category) $Message"
33 $PSBoundParameters.Remove("Message") | Out-Null
34 $PSBoundParameters.Add("Message", $Message)
36 Write-PipelineTaskError @PSBoundParameters
39 function Write-PipelineTaskError {
40 [CmdletBinding()]
41 param(
42 [Parameter(Mandatory = $true)]
43 [string]$Message,
44 [Parameter(Mandatory = $false)]
45 [string]$Type = 'error',
46 [string]$ErrCode,
47 [string]$SourcePath,
48 [string]$LineNumber,
49 [string]$ColumnNumber,
50 [switch]$AsOutput)
52 if(!$ci) {
53 if($Type -eq 'error') {
54 Write-Host $Message -ForegroundColor Red
55 return
57 elseif ($Type -eq 'warning') {
58 Write-Host $Message -ForegroundColor Yellow
59 return
63 if(($Type -ne 'error') -and ($Type -ne 'warning')) {
64 Write-Host $Message
65 return
67 if(-not $PSBoundParameters.ContainsKey('Type')) {
68 $PSBoundParameters.Add('Type', 'error')
70 Write-LogIssue @PSBoundParameters
73 function Write-PipelineSetVariable {
74 [CmdletBinding()]
75 param(
76 [Parameter(Mandatory = $true)]
77 [string]$Name,
78 [string]$Value,
79 [switch]$Secret,
80 [switch]$AsOutput,
81 [bool]$IsMultiJobVariable=$true)
83 if($ci) {
84 Write-LoggingCommand -Area 'task' -Event 'setvariable' -Data $Value -Properties @{
85 'variable' = $Name
86 'isSecret' = $Secret
87 'isOutput' = $IsMultiJobVariable
88 } -AsOutput:$AsOutput
92 function Write-PipelinePrependPath {
93 [CmdletBinding()]
94 param(
95 [Parameter(Mandatory=$true)]
96 [string]$Path,
97 [switch]$AsOutput)
98 if($ci) {
99 Write-LoggingCommand -Area 'task' -Event 'prependpath' -Data $Path -AsOutput:$AsOutput
103 <########################################
104 # Private functions.
105 ########################################>
106 function Format-LoggingCommandData {
107 [CmdletBinding()]
108 param([string]$Value, [switch]$Reverse)
110 if (!$Value) {
111 return ''
114 if (!$Reverse) {
115 foreach ($mapping in $script:loggingCommandEscapeMappings) {
116 $Value = $Value.Replace($mapping.Token, $mapping.Replacement)
118 } else {
119 for ($i = $script:loggingCommandEscapeMappings.Length - 1 ; $i -ge 0 ; $i--) {
120 $mapping = $script:loggingCommandEscapeMappings[$i]
121 $Value = $Value.Replace($mapping.Replacement, $mapping.Token)
125 return $Value
128 function Format-LoggingCommand {
129 [CmdletBinding()]
130 param(
131 [Parameter(Mandatory = $true)]
132 [string]$Area,
133 [Parameter(Mandatory = $true)]
134 [string]$Event,
135 [string]$Data,
136 [hashtable]$Properties)
138 # Append the preamble.
139 [System.Text.StringBuilder]$sb = New-Object -TypeName System.Text.StringBuilder
140 $null = $sb.Append($script:loggingCommandPrefix).Append($Area).Append('.').Append($Event)
142 # Append the properties.
143 if ($Properties) {
144 $first = $true
145 foreach ($key in $Properties.Keys) {
146 [string]$value = Format-LoggingCommandData $Properties[$key]
147 if ($value) {
148 if ($first) {
149 $null = $sb.Append(' ')
150 $first = $false
151 } else {
152 $null = $sb.Append(';')
155 $null = $sb.Append("$key=$value")
160 # Append the tail and output the value.
161 $Data = Format-LoggingCommandData $Data
162 $sb.Append(']').Append($Data).ToString()
165 function Write-LoggingCommand {
166 [CmdletBinding(DefaultParameterSetName = 'Parameters')]
167 param(
168 [Parameter(Mandatory = $true, ParameterSetName = 'Parameters')]
169 [string]$Area,
170 [Parameter(Mandatory = $true, ParameterSetName = 'Parameters')]
171 [string]$Event,
172 [Parameter(ParameterSetName = 'Parameters')]
173 [string]$Data,
174 [Parameter(ParameterSetName = 'Parameters')]
175 [hashtable]$Properties,
176 [Parameter(Mandatory = $true, ParameterSetName = 'Object')]
177 $Command,
178 [switch]$AsOutput)
180 if ($PSCmdlet.ParameterSetName -eq 'Object') {
181 Write-LoggingCommand -Area $Command.Area -Event $Command.Event -Data $Command.Data -Properties $Command.Properties -AsOutput:$AsOutput
182 return
185 $command = Format-LoggingCommand -Area $Area -Event $Event -Data $Data -Properties $Properties
186 if ($AsOutput) {
187 $command
188 } else {
189 Write-Host $command
193 function Write-LogIssue {
194 [CmdletBinding()]
195 param(
196 [ValidateSet('warning', 'error')]
197 [Parameter(Mandatory = $true)]
198 [string]$Type,
199 [string]$Message,
200 [string]$ErrCode,
201 [string]$SourcePath,
202 [string]$LineNumber,
203 [string]$ColumnNumber,
204 [switch]$AsOutput)
206 $command = Format-LoggingCommand -Area 'task' -Event 'logissue' -Data $Message -Properties @{
207 'type' = $Type
208 'code' = $ErrCode
209 'sourcepath' = $SourcePath
210 'linenumber' = $LineNumber
211 'columnnumber' = $ColumnNumber
213 if ($AsOutput) {
214 return $command
217 if ($Type -eq 'error') {
218 $foregroundColor = $host.PrivateData.ErrorForegroundColor
219 $backgroundColor = $host.PrivateData.ErrorBackgroundColor
220 if ($foregroundColor -isnot [System.ConsoleColor] -or $backgroundColor -isnot [System.ConsoleColor]) {
221 $foregroundColor = [System.ConsoleColor]::Red
222 $backgroundColor = [System.ConsoleColor]::Black
224 } else {
225 $foregroundColor = $host.PrivateData.WarningForegroundColor
226 $backgroundColor = $host.PrivateData.WarningBackgroundColor
227 if ($foregroundColor -isnot [System.ConsoleColor] -or $backgroundColor -isnot [System.ConsoleColor]) {
228 $foregroundColor = [System.ConsoleColor]::Yellow
229 $backgroundColor = [System.ConsoleColor]::Black
233 Write-Host $command -ForegroundColor $foregroundColor -BackgroundColor $backgroundColor