Skip to content

Commit

Permalink
Added Silent and Log parameters.
Browse files Browse the repository at this point in the history
Also a ton of documentation...
  • Loading branch information
VertigoRay committed Jan 12, 2023
1 parent 2c5eaf8 commit 53e6cdf
Show file tree
Hide file tree
Showing 10 changed files with 406 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ function Write-InvocationHeader {
$tmp.FullName | Remove-Item -ErrorAction 'SilentlyContinue' -Force

Write-Log -Message $header
$env:PSWriteLogIncludedInvocationHeader = $true
}
9 changes: 8 additions & 1 deletion PSWriteLog/Private/Write-Log.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,18 @@ function global:Write-Log {

[Parameter(Mandatory = $false)]
[switch]
$DisableLogging
$DisableLogging,

[Parameter(Mandatory = $false)]
[switch]
$IncludeInvocationHeader
)

begin {
# Microsoft.PowerShell.Utility\Write-Information "[Write-Log] BoundParameters: $($MyInvocation.BoundParameters | Out-String)" -Tags 'VertigoRay\PSWriteLog','Write-Log'
if ($IncludeInvocationHeader.IsPresent -and -Not $env:PSWriteLogIncludedInvocationHeader) {
Write-InvocationHeader
}

# Get the name of this function
[string] $CmdletName = $PSCmdlet.MyInvocation.MyCommand.Name
Expand Down
51 changes: 30 additions & 21 deletions PSWriteLog/Public/Write-Debug.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ function global:Write-Debug {
${Message},

[switch]
${NoLog}
$NoLog,

[switch]
$Silent
)

begin
Expand All @@ -25,19 +28,21 @@ function global:Write-Debug {
'Source' = "${invoFile}:$($MyInvocation.ScriptLineNumber)";
}

try {
$outBuffer = $null
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
{
$PSBoundParameters['OutBuffer'] = 1
if (-not $Silent) {
try {
$outBuffer = $null
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
{
$PSBoundParameters['OutBuffer'] = 1
}
$PSBoundParameters.Remove('NoLog') | Out-Null
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Utility\Write-Debug', [System.Management.Automation.CommandTypes]::Cmdlet)
$scriptCmd = { & $wrappedCmd @PSBoundParameters }
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
$steppablePipeline.Begin($PSCmdlet)
} catch {
throw
}
$PSBoundParameters.Remove('NoLog') | Out-Null
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Utility\Write-Debug', [System.Management.Automation.CommandTypes]::Cmdlet)
$scriptCmd = { & $wrappedCmd @PSBoundParameters }
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
$steppablePipeline.Begin($PSCmdlet)
} catch {
throw
}
}

Expand All @@ -49,19 +54,23 @@ function global:Write-Debug {
}
}

try {
$steppablePipeline.Process($_)
} catch {
throw
if (-not $Silent) {
try {
$steppablePipeline.Process($_)
} catch {
throw
}
}
}

end
{
try {
$steppablePipeline.End()
} catch {
throw
if (-not $Silent) {
try {
$steppablePipeline.End()
} catch {
throw
}
}
}
<#
Expand Down
52 changes: 30 additions & 22 deletions PSWriteLog/Public/Write-Error.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ function global:Write-Error {

[Alias('TargetType')]
[string]
${CategoryTargetType})
${CategoryTargetType},

[switch]
$Silent
)

begin
{
Expand All @@ -65,18 +69,20 @@ function global:Write-Error {
'Source' = "${invoFile}:$($MyInvocation.ScriptLineNumber)";
}

try {
$outBuffer = $null
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
{
$PSBoundParameters['OutBuffer'] = 1
if (-not $Silent) {
try {
$outBuffer = $null
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
{
$PSBoundParameters['OutBuffer'] = 1
}
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Utility\Write-Error', [System.Management.Automation.CommandTypes]::Cmdlet)
$scriptCmd = {& $wrappedCmd @PSBoundParameters }
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
$steppablePipeline.Begin($PSCmdlet)
} catch {
throw
}
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Utility\Write-Error', [System.Management.Automation.CommandTypes]::Cmdlet)
$scriptCmd = {& $wrappedCmd @PSBoundParameters }
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
$steppablePipeline.Begin($PSCmdlet)
} catch {
throw
}
}

Expand Down Expand Up @@ -111,22 +117,24 @@ function global:Write-Error {
Write-Log @writeLog -Message ($msg -join ' ') -ErrorAction 'Stop'
}

try {
$steppablePipeline.Process($_)
} catch {
throw
if (-not $Silent) {
try {
$steppablePipeline.Process($_)
} catch {
throw
}
}
}

end
{
try {
$steppablePipeline.End()
} catch {
throw
if (-not $Silent) {
try {
$steppablePipeline.End()
} catch {
throw
}
}

Write-Verbose "Write-Error Done!"
}
<#
Expand Down
50 changes: 30 additions & 20 deletions PSWriteLog/Public/Write-Host.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ function global:Write-Host {
${ForegroundColor},

[System.ConsoleColor]
${BackgroundColor})
${BackgroundColor},

[switch]
$Silent
)

begin
{
Expand All @@ -30,18 +34,20 @@ function global:Write-Host {
'Source' = "${invoFile}:$($MyInvocation.ScriptLineNumber)";
}

try {
$outBuffer = $null
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
{
$PSBoundParameters['OutBuffer'] = 1
if (-not $Silent) {
try {
$outBuffer = $null
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
{
$PSBoundParameters['OutBuffer'] = 1
}
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Utility\Write-Host', [System.Management.Automation.CommandTypes]::Cmdlet)
$scriptCmd = {& $wrappedCmd @PSBoundParameters }
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
$steppablePipeline.Begin($PSCmdlet)
} catch {
throw
}
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Utility\Write-Host', [System.Management.Automation.CommandTypes]::Cmdlet)
$scriptCmd = {& $wrappedCmd @PSBoundParameters }
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
$steppablePipeline.Begin($PSCmdlet)
} catch {
throw
}
}

Expand All @@ -51,19 +57,23 @@ function global:Write-Host {
Write-Log @writeLog -Message $Object
}

try {
$steppablePipeline.Process($_)
} catch {
throw
if (-not $Silent) {
try {
$steppablePipeline.Process($_)
} catch {
throw
}
}
}

end
{
try {
$steppablePipeline.End()
} catch {
throw
if (-not $Silent) {
try {
$steppablePipeline.End()
} catch {
throw
}
}
}
<#
Expand Down
50 changes: 30 additions & 20 deletions PSWriteLog/Public/Write-Information.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ function global:Write-Information {

[Parameter(Position=1)]
[string[]]
${Tags})
${Tags},

[switch]
$Silent
)

begin
{
Expand All @@ -23,18 +27,20 @@ function global:Write-Information {
'Source' = "${invoFile}:$($MyInvocation.ScriptLineNumber)";
}

try {
$outBuffer = $null
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
{
$PSBoundParameters['OutBuffer'] = 1
if (-not $Silent) {
try {
$outBuffer = $null
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
{
$PSBoundParameters['OutBuffer'] = 1
}
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Utility\Write-Information', [System.Management.Automation.CommandTypes]::Cmdlet)
$scriptCmd = {& $wrappedCmd @PSBoundParameters }
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
$steppablePipeline.Begin($PSCmdlet)
} catch {
throw
}
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Utility\Write-Information', [System.Management.Automation.CommandTypes]::Cmdlet)
$scriptCmd = {& $wrappedCmd @PSBoundParameters }
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
$steppablePipeline.Begin($PSCmdlet)
} catch {
throw
}
}

Expand All @@ -48,19 +54,23 @@ function global:Write-Information {
}
}

try {
$steppablePipeline.Process($_)
} catch {
throw
if (-not $Silent) {
try {
$steppablePipeline.Process($_)
} catch {
throw
}
}
}

end
{
try {
$steppablePipeline.End()
} catch {
throw
if (-not $Silent) {
try {
$steppablePipeline.End()
} catch {
throw
}
}
}
<#
Expand Down
8 changes: 6 additions & 2 deletions PSWriteLog/Public/Write-Output.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ function global:Write-Output {
${InputObject},

[switch]
${NoEnumerate})
${NoEnumerate},

[switch]
$Log
)

begin
{
Expand Down Expand Up @@ -41,7 +45,7 @@ function global:Write-Output {

process
{
if ((Get-Command 'Write-Log' -ErrorAction 'Ignore') -and ($VerbosePreference -ine 'SilentlyContinue')) {
if ((Get-Command 'Write-Log' -ErrorAction 'Ignore') -and $Log.IsPresent) {
Write-Log @writeLog -Message ($InputObject | Out-String)
}

Expand Down
Loading

0 comments on commit 53e6cdf

Please sign in to comment.