|
| 1 | + |
| 2 | +if ($env:GITHUB_ACTIONS -eq "true") { |
| 3 | + $runningLocal = $false |
| 4 | +} else { |
| 5 | + $runningLocal = $true |
| 6 | +} |
| 7 | + |
| 8 | +# Colors |
| 9 | +$colorCodeRed = '31' |
| 10 | +$colorCodeGreen = '32' |
| 11 | +$colorCodeYellow = '33' |
| 12 | +$colorCodeBlue = '34' |
| 13 | +$colorCodeMagenta = '35' |
| 14 | +$colorCodeCyan = '36' |
| 15 | + |
| 16 | +<# |
| 17 | + .SYNOPSIS |
| 18 | + Writes debug information about the function call and its parameters if extended debug logging is enabled or running locally. |
| 19 | + .DESCRIPTION |
| 20 | + Writes debug information about the function call and its parameters to the console if extended debug logging is enabled or running locally. |
| 21 | + Automatically retrieves the caller's name and arguments from the call stack. |
| 22 | +#> |
| 23 | +function OutputDebugFunctionCall { |
| 24 | + try { |
| 25 | + $caller = (Get-PSCallStack)[1] |
| 26 | + $callerName = $caller.Command |
| 27 | + $callerParameters = $caller.InvocationInfo.BoundParameters |
| 28 | + |
| 29 | + OutputDebug "Function '$callerName' called with parameters:" |
| 30 | + if ($callerParameters.Count -eq 0) { |
| 31 | + OutputDebug "None" |
| 32 | + } |
| 33 | + foreach ($key in $callerParameters.Keys) { |
| 34 | + $val = $callerParameters[$key] |
| 35 | + OutputDebug "-$($key): $val" |
| 36 | + } |
| 37 | + } catch { |
| 38 | + OutputDebug "Unable to retrieve function information from call stack." |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +<# |
| 43 | + .SYNOPSIS |
| 44 | + Starts a console log group. |
| 45 | + .DESCRIPTION |
| 46 | + Starts a console log group. All subsequent log messages will be grouped under this message until OutputGroupEnd is called. |
| 47 | + If running locally, it writes a simple message to the console. If running in GitHub Actions, it uses the `::group::` command to create a collapsible group in the log. |
| 48 | + .PARAMETER Message |
| 49 | + Name/Title of the group. |
| 50 | +#> |
| 51 | +function OutputGroupStart { |
| 52 | + param ( |
| 53 | + [Parameter(Mandatory = $true)] |
| 54 | + [string] $Message |
| 55 | + ) |
| 56 | + |
| 57 | + if ($runningLocal) { |
| 58 | + Write-Host "==== Group start: $Message ====" |
| 59 | + } else { |
| 60 | + Write-Host "::group::$Message" |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +<# |
| 65 | + .SYNOPSIS |
| 66 | + Ends a console log group. |
| 67 | + .DESCRIPTION |
| 68 | + Ends a console log group started with OutputGroupStart. All subsequent log messages will be outside of this group. |
| 69 | +#> |
| 70 | +function OutputGroupEnd { |
| 71 | + if ($runningLocal) { |
| 72 | + Write-Host "==== Group end ====" |
| 73 | + } else { |
| 74 | + Write-Host "::endgroup::" |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +<# |
| 79 | + .SYNOPSIS |
| 80 | + Writes to console with optional color. |
| 81 | + .DESCRIPTION |
| 82 | + Writes a message to the console with an optional color. If no color is specified, the message is written in the default console color. |
| 83 | + .PARAMETER Message |
| 84 | + Message to be written to console. |
| 85 | + .PARAMETER Color |
| 86 | + Optional color for the message. Valid values are 'Red', 'Green', 'Yellow', 'Blue', 'Magenta', 'Cyan'. |
| 87 | +#> |
| 88 | +function OutputColor { |
| 89 | + param ( |
| 90 | + [Parameter(Mandatory = $true)] |
| 91 | + [string] $Message, |
| 92 | + [Parameter(Mandatory = $false)] |
| 93 | + [ValidateSet('Red', 'Green', 'Yellow', 'Blue', 'Magenta', 'Cyan')] |
| 94 | + [string] $Color |
| 95 | + ) |
| 96 | + |
| 97 | + if ($Color) { |
| 98 | + $colorCode = 0 |
| 99 | + switch ($Color) { |
| 100 | + 'Red' { $colorCode = $colorCodeRed } |
| 101 | + 'Green' { $colorCode = $colorCodeGreen } |
| 102 | + 'Yellow' { $colorCode = $colorCodeYellow } |
| 103 | + 'Blue' { $colorCode = $colorCodeBlue } |
| 104 | + 'Magenta' { $colorCode = $colorCodeMagenta } |
| 105 | + 'Cyan' { $colorCode = $colorCodeCyan } |
| 106 | + } |
| 107 | + # char 27 is the escape character for ANSI codes which works in both PS 5 and 7. |
| 108 | + Write-Host "$([char] 27)[${colorCode}m$Message$([char] 27)[0m" |
| 109 | + } else { |
| 110 | + Write-Host $Message |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +<# |
| 115 | + .SYNOPSIS |
| 116 | + Write an error message to the console. |
| 117 | + .DESCRIPTION |
| 118 | + Writes an error message to the console. Throws an exception if running locally, otherwise formats the message for GitHub Actions. |
| 119 | + .PARAMETER Message |
| 120 | + Message to be written to console. |
| 121 | +#> |
| 122 | +function OutputError { |
| 123 | + Param( |
| 124 | + [string] $message |
| 125 | + ) |
| 126 | + |
| 127 | + if ($runningLocal) { |
| 128 | + throw $message |
| 129 | + } |
| 130 | + else { |
| 131 | + Write-Host "::Error::$($message.Replace("`r",'').Replace("`n",' '))" |
| 132 | + $host.SetShouldExit(1) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +<# |
| 137 | + .SYNOPSIS |
| 138 | + Write a warning message to the console. |
| 139 | + .DESCRIPTION |
| 140 | + Writes a warning message to the console. Uses Write-Warning if running locally, otherwise formats the message for GitHub Actions. |
| 141 | + .PARAMETER Message |
| 142 | + Message to be written to console. |
| 143 | +#> |
| 144 | +function OutputWarning { |
| 145 | + Param( |
| 146 | + [string] $message |
| 147 | + ) |
| 148 | + |
| 149 | + if ($runningLocal) { |
| 150 | + Write-Warning $message |
| 151 | + } |
| 152 | + else { |
| 153 | + Write-Host "::Warning::$message" |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +<# |
| 158 | + .SYNOPSIS |
| 159 | + Write a notice message to the console. |
| 160 | + .DESCRIPTION |
| 161 | + Writes a notice message to the console. Uses regular Write-Host if running locally, otherwise formats the message for GitHub Actions. |
| 162 | + .PARAMETER Message |
| 163 | + Message to be written to console. |
| 164 | +#> |
| 165 | +function OutputNotice { |
| 166 | + Param( |
| 167 | + [string] $message |
| 168 | + ) |
| 169 | + |
| 170 | + if ($runningLocal) { |
| 171 | + Write-Host $message |
| 172 | + } |
| 173 | + else { |
| 174 | + Write-Host "::Notice::$message" |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +<# |
| 179 | + .SYNOPSIS |
| 180 | + Mask a value in the log. |
| 181 | + .DESCRIPTION |
| 182 | + Masks a value in the log to prevent sensitive information from being displayed. If running locally, it writes the masked value to the console. |
| 183 | + .PARAMETER Value |
| 184 | + The value to be masked in the log. |
| 185 | +#> |
| 186 | +function MaskValueInLog { |
| 187 | + Param( |
| 188 | + [string] $value |
| 189 | + ) |
| 190 | + |
| 191 | + if (!$runningLocal) { |
| 192 | + Write-Host "`r::add-mask::$value" |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +<# |
| 197 | + .SYNOPSIS |
| 198 | + Write a debug message to the console. |
| 199 | + .DESCRIPTION |
| 200 | + Writes a debug message to the console. Uses Write-Debug if running locally, otherwise formats the message for GitHub Actions. |
| 201 | + .PARAMETER Message |
| 202 | + Message to be written to console. |
| 203 | +#> |
| 204 | +function OutputDebug { |
| 205 | + Param( |
| 206 | + [string] $message |
| 207 | + ) |
| 208 | + |
| 209 | + if ($runningLocal) { |
| 210 | + Write-Debug $message |
| 211 | + } |
| 212 | + else { |
| 213 | + Write-Host "::Debug::[AL-Go]$message" |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +Export-ModuleMember -Function OutputColor, OutputDebugFunctionCall, OutputGroupStart, OutputGroupEnd, OutputError, OutputWarning, OutputNotice, MaskValueInLog, OutputDebug |
| 218 | +Export-ModuleMember -Variable debugLoggingEnabled |
0 commit comments