Skip to content

Commit 00a2fdf

Browse files
committed
Converted to environment variables
1 parent 8391063 commit 00a2fdf

File tree

11 files changed

+136
-160
lines changed

11 files changed

+136
-160
lines changed

PSWriteLog/Private/Write-InvocationHeader.ps1 renamed to PSWriteLog/Private/Get-InvocationHeader.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
SerializationVersion: 1.1.0.1
2222
**********************
2323
#>
24-
function Write-InvocationHeader {
24+
function Get-InvocationHeader {
2525
[CmdletBinding()]
2626
param()
2727

@@ -56,7 +56,7 @@ function Write-InvocationHeader {
5656
}
5757

5858
$tmp.FullName | Remove-Item -ErrorAction 'SilentlyContinue' -Force
59-
60-
Write-Log -Message $header
6159
$env:PSWriteLogIncludeInvocationHeader = $null
60+
61+
return $header
6262
}

PSWriteLog/Private/Write-Log.ps1

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,12 @@ function global:Write-Log {
9393
)
9494

9595
begin {
96-
# Microsoft.PowerShell.Utility\Write-Information "[Write-Log] BoundParameters: $($MyInvocation.BoundParameters | Out-String)" -Tags 'VertigoRay\PSWriteLog','Write-Log'
97-
if ($IncludeInvocationHeader) {
98-
Write-InvocationHeader
96+
Microsoft.PowerShell.Utility\Write-Debug ('[Write-Log] BoundParameters: {0}' -f $($MyInvocation.BoundParameters | Out-String))
97+
98+
if ($env:PSWriteLogDisableLogging) {
99+
# If logging is not currently disabled, get out now!
100+
Microsoft.PowerShell.Utility\Write-Debug ('[Write-Log] env:PSWriteLogDisableLogging: {0}' -f $env:PSWriteLogDisableLogging)
101+
return $null
99102
}
100103

101104
# Get the name of this function
@@ -119,11 +122,11 @@ function global:Write-Log {
119122

120123
[System.Collections.ArrayList] $legacyMessage = @()
121124

122-
$legacyMessage.Add("[$(& $logDate) $(& $logTime)]") | Out-Null
125+
$legacyMessage.Add(('[{0}]' -f (Get-Date -Format 'O'))) | Out-Null
123126
if ($Source) {
124127
$legacyMessage.Add("[${Source}]") | Out-Null
125128
}
126-
$legacyMessage.Add("[${Component}]") | Out-Null
129+
# $legacyMessage.Add("[${Component}]") | Out-Null
127130
$legacyMessage.Add("[${Severity}]") | Out-Null
128131
$legacyMessage.Add(($lMessage.Trim() | Out-String)) | Out-Null
129132

@@ -136,7 +139,7 @@ function global:Write-Log {
136139
[string]
137140
$lMessage
138141
)
139-
# Microsoft.PowerShell.Utility\Write-Information "[Write-Log] Source (sb): ${Source}" -Tags 'VertigoRay\PSWriteLog','Write-Log'
142+
Microsoft.PowerShell.Utility\Write-Debug "[Write-Log] Source (sb): ${Source}"
140143
$severityMap = @{ # Vaguely based on POSH stream numbers
141144
Debug = 5
142145
Error = 3
@@ -162,17 +165,19 @@ function global:Write-Log {
162165
))
163166
}
164167

165-
[scriptblock] $getLogLine = {
168+
[scriptblock] $logLine = {
166169
param(
167170
[string]
168171
$sMsg
169172
)
170173
## Choose which log type to write to file
171-
if ($LogType -ieq 'CMTrace') {
172-
return & $cmTraceLogString -lMessage ($sMsg | Out-String).Trim() -lSource $Source
174+
$line = if ($LogType -ieq 'CMTrace') {
175+
& $cmTraceLogString -lMessage ($sMsg | Out-String).Trim() -lSource $Source
173176
} else {
174-
return & $legacyLogString -lMessage ($sMsg | Out-String).Trim() -lSource $Source
177+
& $legacyLogString -lMessage ($sMsg | Out-String).Trim() -lSource $Source
175178
}
179+
180+
$line | Out-File -FilePath $FilePath.FullName -Append -NoClobber -Force -Encoding 'UTF8' -ErrorAction 'Stop'
176181
}
177182

178183
# Create the directory where the log file will be saved
@@ -182,28 +187,26 @@ function global:Write-Log {
182187
}
183188

184189
process {
185-
# Exit function if it is a debug message and 'LogDebugMessage' option is not $true, or if the log directory was not successfully created in 'Begin' block.
186-
if (($DebugMessage -and -not $LogDebugMessage)) { Return }
190+
if ($IncludeInvocationHeader) {
191+
& $logLine -sMsg ("{1}`n{0}`n{1}" -f (Get-InvocationHeader),('#' * 40))
192+
}
187193

188194
foreach ($msg in $Message) {
189-
# Microsoft.PowerShell.Utility\Write-Information "[Write-Log] Source: $Source" -Tags 'VertigoRay\PSWriteLog','Write-Log'
190-
# Write the log entry to the log file if logging is not currently disabled
191-
if (-not $DisableLogging) {
192-
try {
193-
& $getLogLine -sMsg $msg | Out-File -FilePath $FilePath.FullName -Append -NoClobber -Force -Encoding 'UTF8' -ErrorAction 'Stop'
194-
} catch {
195-
if (-not $ContinueOnError) {
196-
throw ('[{0} {1}] [{2}] [{3}] :: Failed to write message [{4}] to the log file [{5}].{6}{7}' -f @(
197-
& $logDate
198-
& $logTime
199-
$CmdletName
200-
$Component
201-
$Msg
202-
$FilePath.FullName
203-
"`n"
204-
Resolve-Error | Out-String
205-
))
206-
}
195+
Microsoft.PowerShell.Utility\Write-Debug ('[Write-Log] Source: {0}' -f $Source)
196+
try {
197+
& $logLine -sMsg $msg
198+
} catch {
199+
if (-not $ContinueOnError) {
200+
throw ('[{0} {1}] [{2}] [{3}] :: Failed to write message [{4}] to the log file [{5}].{6}{7}' -f @(
201+
& $logDate
202+
& $logTime
203+
$CmdletName
204+
$Component
205+
$Msg
206+
$FilePath.FullName
207+
"`n"
208+
Resolve-Error | Out-String
209+
))
207210
}
208211
}
209212
}
@@ -214,29 +217,29 @@ function global:Write-Log {
214217
if ($MaxLogFileSizeMB) {
215218
try {
216219
[decimal] $LogFileSizeMB = $FilePath.Length/1MB
217-
# Microsoft.PowerShell.Utility\Write-Information "[Write-Log] LogFileSizeMB: $LogFileSizeMB / $MaxLogFileSizeMB" -Tags 'VertigoRay\PSWriteLog','Write-Log'
220+
Microsoft.PowerShell.Utility\Write-Debug "[Write-Log] LogFileSizeMB: $LogFileSizeMB / $MaxLogFileSizeMB"
218221
if ($LogFileSizeMB -gt $MaxLogFileSizeMB) {
219-
# Microsoft.PowerShell.Utility\Write-Information "[Write-Log] Log File Needs to be archived ..." -Tags 'VertigoRay\PSWriteLog','Write-Log'
222+
Microsoft.PowerShell.Utility\Write-Debug "[Write-Log] Log File Needs to be archived ..."
220223
# Change the file extension to "lo_"
221224
[string] $archivedOutLogFile = [IO.Path]::ChangeExtension($FilePath.FullName, 'lo_')
222225

223226
# Log message about archiving the log file
224227
if ((Get-PSCallStack)[1].Command -ne 'Write-Log') {
225228
# Prevent Write-Log from looping more than once.
226-
& $getLogLine -sMsg "Maximum log file size [${MaxLogFileSizeMB} MB] reached. Rename log file to: ${archivedOutLogFile}" | Out-File -FilePath $FilePath.FullName -Append -NoClobber -Force -Encoding 'UTF8' -ErrorAction 'Stop'
229+
& $logLine -sMsg "Maximum log file size [${MaxLogFileSizeMB} MB] reached. Rename log file to: ${archivedOutLogFile}"
227230
}
228231

229232
# Archive existing log file from <filename>.log to <filename>.lo_. Overwrites any existing <filename>.lo_ file. This is the same method SCCM uses for log files.
230233
Move-Item -Path $FilePath.FullName -Destination $archivedOutLogFile -Force -ErrorAction 'Stop'
231234

232235
# Start new log file and Log message about archiving the old log file
233-
& $getLogLine -sMsg "Maximum log file size [${MaxLogFileSizeMB} MB] reached. Previous log file was renamed to: ${archivedOutLogFile}" | Out-File -FilePath $FilePath.FullName -Append -NoClobber -Force -Encoding 'UTF8' -ErrorAction 'Stop'
236+
& $logLine -sMsg "Maximum log file size [${MaxLogFileSizeMB} MB] reached. Previous log file was renamed to: ${archivedOutLogFile}"
234237
} else {
235-
# Microsoft.PowerShell.Utility\Write-Information "[Write-Log] Log File does not need to be archived." -Tags 'VertigoRay\PSWriteLog','Write-Log'
238+
Microsoft.PowerShell.Utility\Write-Debug "[Write-Log] Log File does not need to be archived."
236239
}
237240
} catch {
238241
# If renaming of file fails, script will continue writing to log file even if size goes over the max file size
239-
# Microsoft.PowerShell.Utility\Write-Information "[Write-Log] Archive Error: ${_}" -Tags 'VertigoRay\PSWriteLog','Write-Log'
242+
Microsoft.PowerShell.Utility\Write-Debug "[Write-Log] Archive Error: ${_}"
240243
}
241244
}
242245
}

PSWriteLog/Public/Write-Debug.ps1

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@ function global:Write-Debug {
55
[Alias('Msg')]
66
[AllowEmptyString()]
77
[string]
8-
${Message},
9-
10-
[switch]
11-
$NoLog,
12-
13-
[switch]
14-
$Silent
8+
${Message}
159
)
1610

1711
begin
@@ -28,14 +22,13 @@ function global:Write-Debug {
2822
'Source' = "${invoFile}:$($MyInvocation.ScriptLineNumber)";
2923
}
3024

31-
if (-not $Silent) {
25+
if (-not ($env:PSWriteLogDebugSilent -as [bool])) {
3226
try {
3327
$outBuffer = $null
3428
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
3529
{
3630
$PSBoundParameters['OutBuffer'] = 1
3731
}
38-
$PSBoundParameters.Remove('NoLog') | Out-Null
3932
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Utility\Write-Debug', [System.Management.Automation.CommandTypes]::Cmdlet)
4033
$scriptCmd = { & $wrappedCmd @PSBoundParameters }
4134
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
@@ -48,13 +41,13 @@ function global:Write-Debug {
4841

4942
process
5043
{
51-
if (-not $NoLog.isPresent) {
44+
if (-not ($env:PSWriteLogDebugNoLog -as [bool])) {
5245
if ((Get-Command 'Write-Log' -ErrorAction 'Ignore') -and ($DebugPreference -ine 'SilentlyContinue')) {
5346
Write-Log @writeLog -Message $Message
5447
}
5548
}
5649

57-
if (-not $Silent) {
50+
if (-not ($env:PSWriteLogDebugSilent -as [bool])) {
5851
try {
5952
$steppablePipeline.Process($_)
6053
} catch {
@@ -65,7 +58,7 @@ function global:Write-Debug {
6558

6659
end
6760
{
68-
if (-not $Silent) {
61+
if (-not ($env:PSWriteLogDebugSilent -as [bool])) {
6962
try {
7063
$steppablePipeline.End()
7164
} catch {

PSWriteLog/Public/Write-Error.ps1

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ function global:Write-Error {
4949

5050
[Alias('TargetType')]
5151
[string]
52-
${CategoryTargetType},
53-
54-
[switch]
55-
$Silent
52+
${CategoryTargetType}
5653
)
5754

5855
begin
@@ -69,7 +66,7 @@ function global:Write-Error {
6966
'Source' = "${invoFile}:$($MyInvocation.ScriptLineNumber)";
7067
}
7168

72-
if (-not $Silent) {
69+
if (-not ($env:PSWriteLogErrorSilent -as [bool])) {
7370
try {
7471
$outBuffer = $null
7572
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
@@ -117,7 +114,7 @@ function global:Write-Error {
117114
Write-Log @writeLog -Message ($msg -join ' ') -ErrorAction 'Stop'
118115
}
119116

120-
if (-not $Silent) {
117+
if (-not ($env:PSWriteLogErrorSilent -as [bool])) {
121118
try {
122119
$steppablePipeline.Process($_)
123120
} catch {
@@ -128,7 +125,7 @@ function global:Write-Error {
128125

129126
end
130127
{
131-
if (-not $Silent) {
128+
if (-not ($env:PSWriteLogErrorSilent -as [bool])) {
132129
try {
133130
$steppablePipeline.End()
134131
} catch {

PSWriteLog/Public/Write-Host.ps1

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ function global:Write-Host {
1515
${ForegroundColor},
1616

1717
[System.ConsoleColor]
18-
${BackgroundColor},
19-
20-
[switch]
21-
$Silent
18+
${BackgroundColor}
2219
)
2320

2421
begin
@@ -34,7 +31,7 @@ function global:Write-Host {
3431
'Source' = "${invoFile}:$($MyInvocation.ScriptLineNumber)";
3532
}
3633

37-
if (-not $Silent) {
34+
if (-not ($env:PSWriteLogHostSilent -as [bool])) {
3835
try {
3936
$outBuffer = $null
4037
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
@@ -57,7 +54,7 @@ function global:Write-Host {
5754
Write-Log @writeLog -Message $Object
5855
}
5956

60-
if (-not $Silent) {
57+
if (-not ($env:PSWriteLogHostSilent -as [bool])) {
6158
try {
6259
$steppablePipeline.Process($_)
6360
} catch {
@@ -68,7 +65,7 @@ function global:Write-Host {
6865

6966
end
7067
{
71-
if (-not $Silent) {
68+
if (-not ($env:PSWriteLogHostSilent -as [bool])) {
7269
try {
7370
$steppablePipeline.End()
7471
} catch {

PSWriteLog/Public/Write-Information.ps1

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ function global:Write-Information {
88

99
[Parameter(Position=1)]
1010
[string[]]
11-
${Tags},
12-
13-
[switch]
14-
$Silent
11+
${Tags}
1512
)
1613

1714
begin
@@ -27,7 +24,7 @@ function global:Write-Information {
2724
'Source' = "${invoFile}:$($MyInvocation.ScriptLineNumber)";
2825
}
2926

30-
if (-not $Silent) {
27+
if (-not ($env:PSWriteLogInformationSilent -as [bool])) {
3128
try {
3229
$outBuffer = $null
3330
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
@@ -48,13 +45,13 @@ function global:Write-Information {
4845
{
4946
if ((Get-Command 'Write-Log' -ErrorAction 'Ignore') -and ($InformationPreference -ine 'SilentlyContinue')) {
5047
if ($Tags.isPresent) {
51-
Write-Log @writeLog -Message "$MessageData {$($Tags -join ',')}"
48+
Write-Log @writeLog -Message "{$($Tags -join ',')} $MessageData"
5249
} else {
5350
Write-Log @writeLog -Message "$MessageData"
5451
}
5552
}
5653

57-
if (-not $Silent) {
54+
if (-not ($env:PSWriteLogInformationSilent -as [bool])) {
5855
try {
5956
$steppablePipeline.Process($_)
6057
} catch {
@@ -65,7 +62,7 @@ function global:Write-Information {
6562

6663
end
6764
{
68-
if (-not $Silent) {
65+
if (-not ($env:PSWriteLogInformationSilent -as [bool])) {
6966
try {
7067
$steppablePipeline.End()
7168
} catch {

PSWriteLog/Public/Write-Output.ps1

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ function global:Write-Output {
88
${InputObject},
99

1010
[switch]
11-
${NoEnumerate},
12-
13-
[switch]
14-
$Log
11+
${NoEnumerate}
1512
)
1613

1714
begin
@@ -45,7 +42,7 @@ function global:Write-Output {
4542

4643
process
4744
{
48-
if ((Get-Command 'Write-Log' -ErrorAction 'Ignore') -and $Log.IsPresent) {
45+
if ((Get-Command 'Write-Log' -ErrorAction 'Ignore') -and ($env:PSWriteLogOutputLog -as [bool])) {
4946
Write-Log @writeLog -Message ($InputObject | Out-String)
5047
}
5148

0 commit comments

Comments
 (0)