Files
winutil/functions/private/Write-WinUtilLog.ps1
2026-07-01 19:48:43 -05:00

57 lines
1.6 KiB
PowerShell

function Write-WinUtilLog {
<#
.SYNOPSIS
Writes a timestamped WinUtil log entry directly to the active session log file.
.PARAMETER Message
The message to write.
.PARAMETER Level
The severity level for the log entry.
.PARAMETER Component
The WinUtil component producing the log entry.
#>
param (
[Parameter(Mandatory = $true)]
[string]$Message,
[ValidateSet("INFO", "WARN", "ERROR", "DEBUG")]
[string]$Level = "INFO",
[string]$Component = "WinUtil"
)
try {
$logPath = $null
if ($null -ne $sync -and $sync.ContainsKey("logPath")) {
$logPath = $sync.logPath
}
if ([string]::IsNullOrWhiteSpace($logPath) -and $null -ne $sync -and $sync.ContainsKey("winutildir")) {
$logPath = Join-Path $sync.winutildir "winutil.log"
}
if ([string]::IsNullOrWhiteSpace($logPath) -and -not [string]::IsNullOrWhiteSpace($env:LocalAppData)) {
$logPath = Join-Path (Join-Path $env:LocalAppData "winutil") "winutil.log"
}
if ([string]::IsNullOrWhiteSpace($logPath)) {
return
}
$logDirectory = Split-Path -Path $logPath -Parent
if (-not (Test-Path $logDirectory)) {
New-Item -Path $logDirectory -ItemType Directory -Force | Out-Null
}
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff"
$line = "[$timestamp] [$Level] [$Component] $Message"
Add-Content -Path $logPath -Value $line -Encoding UTF8
} catch {
Write-Warning "Unable to write WinUtil log entry: $($_.Exception.Message)"
}
}