Skip to content

Commit 17cf079

Browse files
committed
Test and manage logrotation
1 parent c0845f7 commit 17cf079

File tree

2 files changed

+86
-10
lines changed

2 files changed

+86
-10
lines changed

Sources/Winget-AutoUpdate/Winget-Install.ps1

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ else {
7171
. "$realPath\functions\Write-ToLog.ps1"
7272
. "$realPath\functions\Confirm-Installation.ps1"
7373
. "$realPath\functions\Compare-SemVer.ps1"
74+
. "$realPath\functions\Invoke-LogRotation.ps1"
7475

7576
#Check if App exists in Winget Repository
7677
function Confirm-Exist ($AppID) {
@@ -268,6 +269,33 @@ function Remove-WAUWhiteList ($AppID) {
268269
}
269270
}
270271

272+
# Function to check log file status
273+
function Test-LogFileStatus {
274+
# Maximum number of log files to keep. Default is 3. Setting MaxLogFiles to 0 will keep all log files.
275+
$MaxLogFiles = $WAUConfig.WAU_MaxLogFiles
276+
if ($null -eq $MaxLogFiles) {
277+
[int32]$MaxLogFiles = 3;
278+
}
279+
else {
280+
[int32]$MaxLogFiles = $MaxLogFiles;
281+
}
282+
283+
# Maximum size of log file.
284+
$MaxLogSize = $WAUConfig.WAU_MaxLogSize;
285+
if (!$MaxLogSize) {
286+
[int64]$MaxLogSize = [int64]1MB; # in bytes, default is 1 MB = 1048576
287+
}
288+
else {
289+
[int64]$MaxLogSize = $MaxLogSize;
290+
}
291+
292+
#LogRotation
293+
[bool]$LogRotate = Invoke-LogRotation $LogFile $MaxLogFiles $MaxLogSize;
294+
if ($false -eq $LogRotate) {
295+
Write-ToLog "An Exception occurred during Log Rotation..." -Component "WinGet-Install"
296+
}
297+
}
298+
271299
<# MAIN #>
272300

273301
#If running as a 32-bit process on an x64 system, re-launch as a 64-bit process
@@ -319,12 +347,15 @@ if (!(Test-Path $LogPath)) {
319347
New-Item -ItemType Directory -Force -Path $LogPath | Out-Null
320348
}
321349

350+
# Test the status of the log files
351+
Test-LogFileStatus
352+
322353
#Log Header
323354
if ($Uninstall) {
324-
Write-ToLog -LogMsg "### NEW UNINSTALL REQUEST ###" -LogColor "Magenta" -IsHeader -Component "WinGet-Install"
355+
Write-ToLog "### NEW UNINSTALL REQUEST ###" -LogColor "Magenta" -IsHeader -Component "WinGet-Install"
325356
}
326357
else {
327-
Write-ToLog -LogMsg "### NEW INSTALL REQUEST ###" -LogColor "Magenta" -IsHeader -Component "WinGet-Install"
358+
Write-ToLog "### NEW INSTALL REQUEST ###" -LogColor "Magenta" -IsHeader -Component "WinGet-Install"
328359
}
329360

330361
if ($IsElevated -eq $True) {

Sources/Winget-AutoUpdate/functions/Invoke-LogRotation.ps1

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,23 @@ function Invoke-LogRotation {
1111
# if MaxLogFiles is 1 just keep the original one and let it grow
1212
if (-not($MaxLogFiles -eq 1)) {
1313
try {
14-
# get current size of log file
14+
# get current size of standard log file
1515
$currentSize = (Get-Item $LogFile).Length
1616

17-
# get log name
17+
# get standard log name
1818
$logFileName = Split-Path $LogFile -Leaf
1919
$logFilePath = Split-Path $LogFile
2020
$logFileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($logFileName)
2121
$logFileNameExtension = [System.IO.Path]::GetExtension($logFileName)
2222

2323
if ($currentSize -ge $MaxLogSize) {
24+
$logrotate = $true
2425

2526
# construct name of archived log file
2627
$newLogFileName = $logFileNameWithoutExtension + (Get-Date -Format 'yyyyMMddHHmmss').ToString() + $logFileNameExtension
2728
# rename old log file
2829
Rename-Item -Path $LogFile -NewName $newLogFileName -Force -Confirm:$false
2930

30-
# create new file
31-
Write-ToLog "New log file created"
32-
3331
# if MaxLogFiles is 0 don't delete any old archived log files
3432
if (-not($MaxLogFiles -eq 0)) {
3533

@@ -48,11 +46,58 @@ function Invoke-LogRotation {
4846
}
4947
}
5048
}
49+
}
50+
51+
# CM log file name
52+
$CMLogFile = $LogFile -replace "\.log$", "_CM.log"
53+
54+
# get current size of CM log file if it exists
55+
if (Test-Path $CMLogFile) {
56+
$currentCMSize = (Get-Item $CMLogFile).Length
57+
58+
# get CM log name
59+
$logFileName = Split-Path $CMLogFile -Leaf
60+
$logFilePath = Split-Path $CMLogFile
61+
$logFileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($logFileName)
62+
$logFileNameExtension = [System.IO.Path]::GetExtension($logFileName)
63+
64+
if ($currentCMSize -ge $MaxLogSize) {
65+
$CM_logrotate = $true
66+
67+
# construct name of archived log file
68+
$newLogFileName = $logFileNameWithoutExtension + (Get-Date -Format 'yyyyMMddHHmmss').ToString() + $logFileNameExtension
69+
# rename old log file
70+
Rename-Item -Path $CMLogFile -NewName $newLogFileName -Force -Confirm:$false
71+
72+
# if MaxLogFiles is 0 don't delete any old archived log files
73+
if (-not($MaxLogFiles -eq 0)) {
74+
75+
# set filter to search for archived log files
76+
$archivedLogFileFilter = $logFileNameWithoutExtension + '??????????????' + $logFileNameExtension
77+
78+
# get archived log files
79+
$oldLogFiles = Get-Item -Path "$(Join-Path -Path $logFilePath -ChildPath $archivedLogFileFilter)"
80+
81+
if ([bool]$oldLogFiles) {
82+
# compare found log files to MaxLogFiles parameter of the log object, and delete oldest until we are
83+
# back to the correct number
84+
if (($oldLogFiles.Count + 1) -gt $MaxLogFiles) {
85+
[int]$numTooMany = (($oldLogFiles.Count) + 1) - $MaxLogFiles
86+
$oldLogFiles | Sort-Object 'LastWriteTime' | Select-Object -First $numTooMany | Remove-Item
87+
}
88+
}
89+
}
90+
}
91+
}
5192

52-
#Log Header
53-
Write-ToLog -LogMsg "CHECK FOR APP UPDATES (System context)" -IsHeader
54-
Write-ToLog -LogMsg "Max Log Size reached: $MaxLogSize bytes - Rotated Logs"
93+
# Log actions
94+
if ($logrotate) {
95+
Write-ToLog "### Max Standard log size reached: $MaxLogSize bytes - Rotated Logs ###"
5596
}
97+
if ($CM_logrotate) {
98+
Write-ToLog "### Max CM log size reached: $MaxLogSize bytes - Rotated CM Logs ###"
99+
}
100+
56101
# end of try block
57102
Return $true;
58103
}

0 commit comments

Comments
 (0)