diff --git a/README.md b/README.md index 3cbbd8b..4026dee 100644 --- a/README.md +++ b/README.md @@ -143,10 +143,10 @@ We'll decide to use a `Log-Rotate` *state* file in *C:\var\Log-Rotate\Log-Rotate We'll decide to log the *Task* to *C:\logs\Log-Rotate.log*. This file will capture all the Powershell output streams. -Run the Command line with the `-Debug` parameter to make sure everything is working. +Run the Command line with the `-WhatIf` parameter to make sure everything is working. ```powershell -Powershell 'Import-Module Log-Rotate; Log-Rotate -Config "C:\configs\Log-Rotate\Log-Rotate.conf" -State "C:\var\Log-Rotate\Log-Rotate.status" -Verbose -Debug' +Powershell 'Import-Module Log-Rotate; Log-Rotate -Config "C:\configs\Log-Rotate\Log-Rotate.conf" -State "C:\var\Log-Rotate\Log-Rotate.status" -Verbose -WhatIf' ``` Task Command line: @@ -176,10 +176,10 @@ We'll decide to use a `Log-Rotate` *state* file in */var/lib/Log-Rotate/Log-Rota We'll decide to log the *cron* to */var/log/Log-Rotate.log*. This file will capture all the Powershell output streams. -Run the Command line with the `-Debug` parameter to make sure everything is working. +Run the Command line with the `-WhatIf` parameter to make sure everything is working. ```powershell -Powershell 'Import-Module Log-Rotate; Log-Rotate -Config "/etc/Log-Rotate.conf" -State "/var/lib/Log-Rotate/Log-Rotate.status" -Verbose -Debug' +Powershell 'Import-Module Log-Rotate; Log-Rotate -Config "/etc/Log-Rotate.conf" -State "/var/lib/Log-Rotate/Log-Rotate.status" -Verbose -WhatIf' ``` Cron command line: @@ -208,7 +208,7 @@ PARAMETERS Later config files will override earlier ones. The best method is to use a single config file that includes other config files by using the 'include' directive. - -Debug [] + -WhatIf [] In debug mode, no logs are rotated. Use this to validate your configs or observe rotation logic. -Force [] diff --git a/src/Log-Rotate/classes/BlockFactory.ps1 b/src/Log-Rotate/classes/BlockFactory.ps1 index 780903a..452d00d 100644 --- a/src/Log-Rotate/classes/BlockFactory.ps1 +++ b/src/Log-Rotate/classes/BlockFactory.ps1 @@ -113,7 +113,6 @@ $BlockFactory = [PSCustomObject]@{ $yes = $_.Value if ( $child.ContainsKey($yes) -and (!$child.ContainsKey($no)) -and $parent.ContainsKey($no) ) { - if ($g_debugFlag -band 4) { Write-Verbose "I said $yes, I didn't say $no, although my parent said $no, I'll still go ahead." } $my_options.Remove($no) } } diff --git a/src/Log-Rotate/classes/LogFactory.ps1 b/src/Log-Rotate/classes/LogFactory.ps1 index e44e2bb..a0d9677 100644 --- a/src/Log-Rotate/classes/LogFactory.ps1 +++ b/src/Log-Rotate/classes/LogFactory.ps1 @@ -67,7 +67,7 @@ $LogFactory | Add-Member -Name 'InitStatus' -MemberType ScriptMethod -Value { # The reason for using the following code is only because the cmdlets such as Convert-Path, Resolve-Path must point to an existing item. # If debugging didn't create the file, we would have to manually normalize the status file path (i.e. get it's absolute path). <# - if ($g_debugFlag) { + if ($WhatIf) { $is_home = $statusfile_path -match '^~' if ($is_home) { # It's an absolute path @@ -127,7 +127,7 @@ $LogFactory | Add-Member -Name 'InitStatus' -MemberType ScriptMethod -Value { # Always test for write permissions on the status file try { '' | Out-File $this.StatusFile_FullName -Append -Force - if (!$status -and $g_debugFlag) { + if (!$status -and $WhatIf) { # We're running Log-Rotate the first time in debug mode. Remove-Item $this.StatusFile_FullName } @@ -161,7 +161,7 @@ $LogFactory | Add-Member -Name 'GetAll' -MemberType ScriptMethod -Value { $LogFactory | Add-Member -Name 'DumpStatus' -MemberType ScriptMethod -Value { try { - if (!$g_debugFlag) { + if (!$WhatIf) { # Update my state with each logs rotation status $this.GetAll() | Where-Object { $_.Status['rotation_datetime'] } | ForEach-Object { $rotationDateISO = $_.Status['rotation_datetime'].ToString('s') diff --git a/src/Log-Rotate/classes/LogObject.ps1 b/src/Log-Rotate/classes/LogObject.ps1 index e722cc1..7d90ed7 100644 --- a/src/Log-Rotate/classes/LogObject.ps1 +++ b/src/Log-Rotate/classes/LogObject.ps1 @@ -22,12 +22,12 @@ $LogObject = [PSCustomObject]@{ # File exists. Write-Verbose "Error creating output file $my_previous_fullname`: File exists" }else { - if (!$g_debugFlag) { + if (!$WhatIf) { Copy-Item $my_fullname $my_previous_fullname -ErrorAction Stop } if ($copytruncate) { Write-Verbose "Truncating $my_fullname" - if (!$g_debugFlag) { + if (!$WhatIf) { Clear-Content $my_fullname } }else { @@ -41,12 +41,12 @@ $LogObject = [PSCustomObject]@{ # File exists. Write-Verbose "Error creating output file $my_previous_fullname`: File exists" }else { - if (!$g_debugFlag) { + if (!$WhatIf) { Move-Item $my_fullname $my_previous_fullname -Force } if ($create) { Write-Verbose "Creating new log file $my_fullname" - if (!$g_debugFlag) { + if (!$WhatIf) { $newitem = New-Item $my_fullname -ItemType File | Out-Null if ($newitem) { @@ -102,7 +102,7 @@ $LogObject = [PSCustomObject]@{ # Rename old logs Write-Verbose "Renaming $source_fullName to $destination_fullName (rotatecount $rotate, logstart $start, i $i)" - if ($g_debugFlag) { continue } + if ($WhatIf) { continue } if (Test-Path $source_fullName) { Try { Move-Item -Path $source_fullName -Destination $destination_fullName -Force @@ -135,7 +135,7 @@ $LogObject = [PSCustomObject]@{ $params = @( 'rn', $fullName, '*', $baseName ) try { Write-Verbose "Rename log inside compressed archive $fullName to $baseName" - if ($g_debugFlag) { + if ($WhatIf) { continue } @@ -189,7 +189,7 @@ $LogObject = [PSCustomObject]@{ try { Write-Verbose "Compressing log with: $compresscmd" Write-Verbose "Compress command line: $compresscmd $( $params -join ' ' )" - if ($g_debugFlag) { + if ($WhatIf) { return } @@ -342,7 +342,7 @@ $LogObject = [PSCustomObject]@{ } # Delete file - if (!$g_debugFlag) { + if (!$WhatIf) { Remove-Item $file_fullname }else { # For debugging to simulate deleted file @@ -1047,8 +1047,8 @@ $LogObject | Add-Member -Name 'PostPostRotate' -MemberType ScriptMethod -Value { if ($rotate -gt 0) { $keep_prev_count = $rotate $prev_files = Get-Files $my_previous_noncompressed_regex $my_previous_directory | Where-Object { - !$g_debugFlag -or - ($g_debugFlag -and $_.FullName -notin $debug_my_prevfilespurged_fullnames ) + !$WhatIf -or + ($WhatIf -and $_.FullName -notin $debug_my_prevfilespurged_fullnames ) } Remove-Old-Files $prev_files $keep_prev_count 1 @@ -1091,8 +1091,8 @@ $LogObject | Add-Member -Name 'PostPostRotate' -MemberType ScriptMethod -Value { # Delete old compressed logs if ($rotate -gt 0) { $prev_files = Get-Files $my_previous_compressed_regex $my_previous_directory | Where-Object { - !$g_debugFlag -or - ($g_debugFlag -and $_.FullName -notin $debug_my_prevfilespurged_fullnames ) + !$WhatIf -or + ($WhatIf -and $_.FullName -notin $debug_my_prevfilespurged_fullnames ) } Remove-Old-Files $prev_files $keep_prev_compressed_count 1 } diff --git a/src/Log-Rotate/classes/New-BlockFactory.ps1 b/src/Log-Rotate/classes/New-BlockFactory.ps1 index 0cad34a..f828e67 100644 --- a/src/Log-Rotate/classes/New-BlockFactory.ps1 +++ b/src/Log-Rotate/classes/New-BlockFactory.ps1 @@ -113,7 +113,6 @@ function New-BlockFactory { $yes = $_.Value if ( $child.ContainsKey($yes) -and (!$child.ContainsKey($no)) -and $parent.ContainsKey($no) ) { - if ($g_debugFlag -band 4) { Write-Verbose "I said $yes, I didn't say $no, although my parent said $no, I'll still go ahead." } $my_options.Remove($no) } } diff --git a/src/Log-Rotate/classes/New-LogFactory.ps1 b/src/Log-Rotate/classes/New-LogFactory.ps1 index ab76c67..1a00c2e 100644 --- a/src/Log-Rotate/classes/New-LogFactory.ps1 +++ b/src/Log-Rotate/classes/New-LogFactory.ps1 @@ -68,7 +68,7 @@ function New-LogFactory { # The reason for using the following code is only because the cmdlets such as Convert-Path, Resolve-Path must point to an existing item. # If debugging didn't create the file, we would have to manually normalize the status file path (i.e. get it's absolute path). <# - if ($g_debugFlag) { + if ($WhatIf) { $is_home = $statusfile_path -match '^~' if ($is_home) { # It's an absolute path @@ -128,7 +128,7 @@ function New-LogFactory { # Always test for write permissions on the status file try { '' | Out-File $this.StatusFile_FullName -Append -Force - if (!$status -and $g_debugFlag) { + if (!$status -and $WhatIf) { # We're running Log-Rotate the first time in debug mode. Remove-Item $this.StatusFile_FullName } @@ -162,7 +162,7 @@ function New-LogFactory { $LogFactory | Add-Member -Name 'DumpStatus' -MemberType ScriptMethod -Value { try { - if (!$g_debugFlag) { + if (!$WhatIf) { # Update my state with each logs rotation status $this.GetAll() | Where-Object { $_.Status['rotation_datetime'] } | ForEach-Object { $rotationDateISO = $_.Status['rotation_datetime'].ToString('s') diff --git a/src/Log-Rotate/classes/New-LogObject.ps1 b/src/Log-Rotate/classes/New-LogObject.ps1 index d31d73b..2752012 100644 --- a/src/Log-Rotate/classes/New-LogObject.ps1 +++ b/src/Log-Rotate/classes/New-LogObject.ps1 @@ -23,12 +23,12 @@ function New-LogObject { # File exists. Write-Verbose "Error creating output file $my_previous_fullname`: File exists" }else { - if (!$g_debugFlag) { + if (!$WhatIf) { Copy-Item $my_fullname $my_previous_fullname -ErrorAction Stop } if ($copytruncate) { Write-Verbose "Truncating $my_fullname" - if (!$g_debugFlag) { + if (!$WhatIf) { Clear-Content $my_fullname } }else { @@ -42,12 +42,12 @@ function New-LogObject { # File exists. Write-Verbose "Error creating output file $my_previous_fullname`: File exists" }else { - if (!$g_debugFlag) { + if (!$WhatIf) { Move-Item $my_fullname $my_previous_fullname -Force } if ($create) { Write-Verbose "Creating new log file $my_fullname" - if (!$g_debugFlag) { + if (!$WhatIf) { $newitem = New-Item $my_fullname -ItemType File | Out-Null if ($newitem) { @@ -103,7 +103,7 @@ function New-LogObject { # Rename old logs Write-Verbose "Renaming $source_fullName to $destination_fullName (rotatecount $rotate, logstart $start, i $i)" - if ($g_debugFlag) { continue } + if ($WhatIf) { continue } if (Test-Path $source_fullName) { Try { Move-Item -Path $source_fullName -Destination $destination_fullName -Force @@ -136,7 +136,7 @@ function New-LogObject { $params = @( 'rn', $fullName, '*', $baseName ) try { Write-Verbose "Rename log inside compressed archive $fullName to $baseName" - if ($g_debugFlag) { + if ($WhatIf) { continue } @@ -190,7 +190,7 @@ function New-LogObject { try { Write-Verbose "Compressing log with: $compresscmd" Write-Verbose "Compress command line: $compresscmd $( $params -join ' ' )" - if ($g_debugFlag) { + if ($WhatIf) { return } @@ -343,7 +343,7 @@ function New-LogObject { } # Delete file - if (!$g_debugFlag) { + if (!$WhatIf) { Remove-Item $file_fullname }else { # For debugging to simulate deleted file @@ -1047,8 +1047,8 @@ function New-LogObject { if ($rotate -gt 0) { $keep_prev_count = $rotate $prev_files = Get-Files $my_previous_noncompressed_regex $my_previous_directory | Where-Object { - !$g_debugFlag -or - ($g_debugFlag -and $_.FullName -notin $debug_my_prevfilespurged_fullnames ) + !$WhatIf -or + ($WhatIf -and $_.FullName -notin $debug_my_prevfilespurged_fullnames ) } Remove-Old-Files $prev_files $keep_prev_count 1 @@ -1091,8 +1091,8 @@ function New-LogObject { # Delete old compressed logs if ($rotate -gt 0) { $prev_files = Get-Files $my_previous_compressed_regex $my_previous_directory | Where-Object { - !$g_debugFlag -or - ($g_debugFlag -and $_.FullName -notin $debug_my_prevfilespurged_fullnames ) + !$WhatIf -or + ($WhatIf -and $_.FullName -notin $debug_my_prevfilespurged_fullnames ) } Remove-Old-Files $prev_files $keep_prev_compressed_count 1 } diff --git a/src/Log-Rotate/helpers/Get-Exception-Message.ps1 b/src/Log-Rotate/helpers/Get-Exception-Message.ps1 index 0fc128c..3a82d57 100644 --- a/src/Log-Rotate/helpers/Get-Exception-Message.ps1 +++ b/src/Log-Rotate/helpers/Get-Exception-Message.ps1 @@ -8,7 +8,7 @@ function Get-Exception-Message ($ErrorRecord) { } } $Message = Get-InnerExceptionMessage $ErrorRecord.Exception - if ($g_debugFlag -band 2) { + if ($WhatIf) { $Message = $Message + "`nStacktrace:`n" + $ErrorRecord.Exception.ErrorRecord.ScriptStackTrace } $Message diff --git a/src/Log-Rotate/helpers/Start-Script.ps1 b/src/Log-Rotate/helpers/Start-Script.ps1 index 145856a..b943254 100644 --- a/src/Log-Rotate/helpers/Start-Script.ps1 +++ b/src/Log-Rotate/helpers/Start-Script.ps1 @@ -10,15 +10,13 @@ function Start-Script { # Save the caller's ErrorAction $callerEA = $ErrorActionPreference $ErrorActionPreference = 'Stop' - if ($g_debugFlag -band 4) { Write-Verbose "[Start-Script] callerEA: $callerEA" } - if ($g_debugFlag -band 4) { Write-Verbose "[Start-Script] ErrorActionPreference: $ErrorActionPreference" } } process { try { Write-Verbose "Running script with arg $file_FullName : `n$script" $OS = $ENV:OS - if (!$g_debugFlag) { + if (!$WhatIf) { if ($OS -eq "Windows_NT") { # E.g. & Powershell -Command { echo $Args[0] } -Args @('D:/console.log') diff --git a/src/Log-Rotate/private/rotate/Process-Local-Block.ps1 b/src/Log-Rotate/private/rotate/Process-Local-Block.ps1 index c61c680..1dc0474 100644 --- a/src/Log-Rotate/private/rotate/Process-Local-Block.ps1 +++ b/src/Log-Rotate/private/rotate/Process-Local-Block.ps1 @@ -304,7 +304,7 @@ function Process-Local-Block { } } }else { - if ($g_debugFlag) { + if ($WhatIf) { Write-Verbose "Not running first action script, since no logs will be rotated" Write-Verbose "Not running prerotate script, since no logs will be rotated" Write-Verbose "Not running postrotate script, since no logs will be rotated" diff --git a/src/Log-Rotate/public/Log-Rotate.ps1 b/src/Log-Rotate/public/Log-Rotate.ps1 index 4408b32..8c4f8b1 100644 --- a/src/Log-Rotate/public/Log-Rotate.ps1 +++ b/src/Log-Rotate/public/Log-Rotate.ps1 @@ -70,6 +70,9 @@ function Log-Rotate { , [alias("c")] [string[]]$Config + , + [alias("d")] + [switch]$WhatIf , [alias("f")] [switch]$Force @@ -90,8 +93,9 @@ function Log-Rotate { [switch]$Version ) - if ($debug) { + if ($WhatIf) { Write-Warning "We are in Debug mode. No logs will be rotated." + $VerbosePreference = 'Continue' } if ($Force) { Write-Warning "We are in Forced-Rotation mode." @@ -104,8 +108,8 @@ function Log-Rotate { # 1 - On, script does not change files. Calling Log-Rotate with -Debug will switch this to 1. # 2 - Output Stacktrace in error messages # 4 - On, verbose mode. Implies (1). This is NOT related to calling Log-Rotate with -Verbose, but strictly for debugging messages. - $g_debugFlag = 0 - if ($g_debugFlag) { + $WhatIf = 0 + if ($WhatIf) { Write-Warning "Developer's debug flag is on." } @@ -122,7 +126,7 @@ function Log-Rotate { # If we're using the -debug flag, always use -verbose mode. $DebugPreference = 'Continue' # Preserve our set debug flag for testing - $g_debugFlag = if ($g_debugFlag) { $g_debugFlag } else { 1 } + $WhatIf = if ($WhatIf) { $WhatIf } else { 1 } }else { # If we're not using the -debug flag, debug should stay silent instead of prompting. $DebugPreference = 'SilentlyContinue' @@ -159,9 +163,6 @@ function Log-Rotate { #Write-Verbose "Current working directory: $( Convert-Path . )" Write-Verbose "Current working directory: $( $(Get-Location).Path )" - # Debug - if ($g_debugFlag -band 4) { Write-Verbose "Verbose stream: $VerbosePreference" } - if ($g_debugFlag -band 4) { Write-Verbose "Debug stream: $DebugPreference" } # Get the configuration as a string if ($ConfigAsString) {