Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only use Write-Debug in catch clauses #986

Open
DennisL68 opened this issue Jan 27, 2025 · 0 comments
Open

Only use Write-Debug in catch clauses #986

DennisL68 opened this issue Jan 27, 2025 · 0 comments

Comments

@DennisL68
Copy link

DennisL68 commented Jan 27, 2025

I wrote a command that catches the use of Write-Debug and dumps the output using PSFramework to quickly being able to debug all my legacy code.

function Write-MyDebug {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory, Position = 0)]
        [String]$Message
    )
    $Caller = (Get-PSCallStack)[1].Command

    Write-PSFMessage -Level Debug -Message $Message -Function $Caller

<#

.ForwardHelpTargetName Microsoft.PowerShell.Utility\Write-Debug
.ForwardHelpCategory Cmdlet

#>

}

Set-Alias Write-Debug -Value Write-MyDebug -Scope Global

But the logs now gets dirty as soon as I enter a command in the PowerShell console on systems that has posh-git installed.

Command         Arguments                                                               Location
-------         ---------                                                               --------
Write-MyDebug   {Message=Setting WindowTitle: C:\Users\public - PowerShell 5.1 (22116)} PSFRedirect.psm1: line 138
Set-WindowTitle {GitStatus=, IsAdmin=}                                                  WindowTitle.ps1: line 58
prompt          {}                                                                      posh-git.psm1: line 101
<ScriptBlock>   {}                                                                      <No file>

Apparently, the script WindowTitle.ps1 in posh-git calls Write-Debug at line 58 in the released code.

Write-Debug "Setting WindowTitle: $windowTitleText"

This is where Write-Debug is used throughout the project.

  • Line 78; AnsiUtils.ps1
  • Line 95: GitTabExpansion.ps1
  • Line 18: WindowTitle.ps1
  • Line 19: WindowTitle.ps1
  • Line 24: WindowTitle.ps1
  • Line 37: WindowTitle.ps1
  • Line 58: WindowTitle.ps1
  • Line 62: WindowTitle.ps1

I could of course filter my log output/input each time I'm using this for logging the usage of Write-Debug on my systems. And I know I asked for issues like this by writing such a function.

But it would help me a lot if Write-Debug was only used in catch-clauses when there really is an error to handle.
(This will also enhance the performance some what 👍)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant