Skip to content

Commit

Permalink
Fixed Invocation Header
Browse files Browse the repository at this point in the history
  • Loading branch information
VertigoRay committed Jan 13, 2023
1 parent 00a2fdf commit 442d0b8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
24 changes: 16 additions & 8 deletions PSWriteLog/Private/Get-InvocationHeader.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,42 @@ function Get-InvocationHeader {
Start-Transcript -LiteralPath $tmp.FullName -IncludeInvocationHeader -Force | Out-Null
Stop-Transcript | Out-Null

# Microsoft.PowerShell.Utility\Write-Debug ('[Get-InvocationHeader] Getting Header From: {0}' -f $tmp.FullName)
$inHeader = $false
$header = Get-Content $tmp.FullName | ForEach-Object {
if ($_.StartsWith('*') -and $inHeader) {
[System.Collections.ArrayList] $header = @()
foreach ($line in (Get-Content $tmp.FullName)) {
if ($line.StartsWith('*') -and $inHeader) {
# Reached end of header
# Microsoft.PowerShell.Utility\Write-Debug ('[Get-InvocationHeader] Reached end of header.')
break
} elseif ($_.StartsWith('*')) {
} elseif ($line.StartsWith('*')) {
# Reached start of header
# Microsoft.PowerShell.Utility\Write-Debug ('[Get-InvocationHeader] Reached start of header.')
$inHeader = $true
} else {
# In header
switch -regex ($_.Trim()) {
# Microsoft.PowerShell.Utility\Write-Debug ('[Get-InvocationHeader] Handling: {0}' -f $line)
switch -regex ($line.Trim()) {
'^Windows PowerShell transcript start' {
Write-Output ('PSWriteLog v{0} Invocation Header' -f (Get-Module 'PSWriteLog' | Select-Object -First 1).Version)
$headerAdd = '# PSWriteLog v{0} Invocation Header' -f (Get-Module 'PSWriteLog' | Select-Object -First 1).Version
break
}
'^Start time\:\s+' {
Write-Output ('Start time: {0}' -f (Get-Date -Format 'O'))
$headerAdd = '# Start time: {0}' -f (Get-Date -Format 'O')
break
}
default {
Write-Output $_
$headerAdd = '# {0}' -f $line
}
}
# Microsoft.PowerShell.Utility\Write-Debug ('[Get-InvocationHeader] Adding: {0}' -f $headerAdd)
$header.Add($headerAdd) | Out-Null
}
}

# Microsoft.PowerShell.Utility\Write-Debug ('[Get-InvocationHeader] Removing: {0}' -f $tmp.FullName)
$tmp.FullName | Remove-Item -ErrorAction 'SilentlyContinue' -Force
$env:PSWriteLogIncludeInvocationHeader = $null

return $header
return ("{1}`n{0}`n{1}" -f ($header.Trim() | Out-String).Trim(),('#' * 40))
}
18 changes: 9 additions & 9 deletions PSWriteLog/Private/Write-Log.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ function global:Write-Log {
)

begin {
Microsoft.PowerShell.Utility\Write-Debug ('[Write-Log] BoundParameters: {0}' -f $($MyInvocation.BoundParameters | Out-String))
# Microsoft.PowerShell.Utility\Write-Debug ('[Write-Log] BoundParameters: {0}' -f $($MyInvocation.BoundParameters | Out-String))

if ($env:PSWriteLogDisableLogging) {
# If logging is not currently disabled, get out now!
Microsoft.PowerShell.Utility\Write-Debug ('[Write-Log] env:PSWriteLogDisableLogging: {0}' -f $env:PSWriteLogDisableLogging)
# Microsoft.PowerShell.Utility\Write-Debug ('[Write-Log] env:PSWriteLogDisableLogging: {0}' -f $env:PSWriteLogDisableLogging)
return $null
}

Expand Down Expand Up @@ -139,7 +139,7 @@ function global:Write-Log {
[string]
$lMessage
)
Microsoft.PowerShell.Utility\Write-Debug "[Write-Log] Source (sb): ${Source}"
# Microsoft.PowerShell.Utility\Write-Debug "[Write-Log] Source (sb): ${Source}"
$severityMap = @{ # Vaguely based on POSH stream numbers
Debug = 5
Error = 3
Expand Down Expand Up @@ -188,11 +188,11 @@ function global:Write-Log {

process {
if ($IncludeInvocationHeader) {
& $logLine -sMsg ("{1}`n{0}`n{1}" -f (Get-InvocationHeader),('#' * 40))
& $logLine -sMsg (Get-InvocationHeader)
}

foreach ($msg in $Message) {
Microsoft.PowerShell.Utility\Write-Debug ('[Write-Log] Source: {0}' -f $Source)
# Microsoft.PowerShell.Utility\Write-Debug ('[Write-Log] Source: {0}' -f $Source)
try {
& $logLine -sMsg $msg
} catch {
Expand All @@ -217,9 +217,9 @@ function global:Write-Log {
if ($MaxLogFileSizeMB) {
try {
[decimal] $LogFileSizeMB = $FilePath.Length/1MB
Microsoft.PowerShell.Utility\Write-Debug "[Write-Log] LogFileSizeMB: $LogFileSizeMB / $MaxLogFileSizeMB"
# Microsoft.PowerShell.Utility\Write-Debug "[Write-Log] LogFileSizeMB: $LogFileSizeMB / $MaxLogFileSizeMB"
if ($LogFileSizeMB -gt $MaxLogFileSizeMB) {
Microsoft.PowerShell.Utility\Write-Debug "[Write-Log] Log File Needs to be archived ..."
# Microsoft.PowerShell.Utility\Write-Debug "[Write-Log] Log File Needs to be archived ..."
# Change the file extension to "lo_"
[string] $archivedOutLogFile = [IO.Path]::ChangeExtension($FilePath.FullName, 'lo_')

Expand All @@ -235,11 +235,11 @@ function global:Write-Log {
# Start new log file and Log message about archiving the old log file
& $logLine -sMsg "Maximum log file size [${MaxLogFileSizeMB} MB] reached. Previous log file was renamed to: ${archivedOutLogFile}"
} else {
Microsoft.PowerShell.Utility\Write-Debug "[Write-Log] Log File does not need to be archived."
# Microsoft.PowerShell.Utility\Write-Debug "[Write-Log] Log File does not need to be archived."
}
} catch {
# If renaming of file fails, script will continue writing to log file even if size goes over the max file size
Microsoft.PowerShell.Utility\Write-Debug "[Write-Log] Archive Error: ${_}"
# Microsoft.PowerShell.Utility\Write-Debug "[Write-Log] Archive Error: ${_}"
}
}
}
Expand Down

0 comments on commit 442d0b8

Please sign in to comment.